Program to store 10 contacts having info. Of phonenumber,name and email.

I am doing a program store 10 contacts with memberdata phonenumber as cno[10][10], name as cname[10][20] and email as [10][15].
I am having trouble while giving input in the arrays.

1
2
3
4
5
  For(i=1;i<=10;i++)
   { For(j=1;j<=20;j++)
      { Cin>>cname[I][j];
       }
    }
C and C++ are case-sensitive. So i is not the same as I, and For is not a legal keyword.

Also, without seeing how you've defined cname, it's hard to know what other problems there might be.

Please provide a minimal, compilable codeset that exhibits the problem. Also, a clear, consise, complete description of what problem you're actually seeing.
In addition, array indices start at 0, so your indices should be
for (int i = 0; i < 10; i++) (and likewise, j < 20).
Last edited on
From what is said, it looks like you have:

1
2
3
char cno[10][10];
char cname[10][20];
char email[10][15];


as the definitions.

Have you used std::string yet - as this would be much better than using c-style strings.

For c-style strings, consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

int main()
{
	constexpr int noContact {10};

	char cno[noContact][10];
	char cname[noContact][20];
	char email[noContact][15];

	for (int c = 0; c < noContact; ++c) {

		std::cout << "Name: ";
		std::cin.getline(cname[c], 20);
		std::cout << "Tel: ";
		std::cin.getline(cno[c], 10);
		std::cout << "Email: ";
		std::cin.getline(email[c], 15);
	}

	for (int c = 0; c < noContact; ++c)
		std::cout << cname[c] << "  " << cno[c] << "  " << email[c] << '\n';
}


Note that this doesn't check if more than the allowed chars are entered.
Last edited on
The problem I face with this is that after giving input of one name how can I go to next input of another name without filling the total memory locations

Here, if I give the name as xavier with 6 characters then the remaining 14 locations should be avoided and I should fill the next name of another 20 locations likewise by using enough memory locations that are required.

(int cno[10][10]; char cname[10][20],email[10][15])
Last edited on
The string class type should be Standard C++.
Are you saying that you don't like how typing in "xavier" to an array of length 20 leaves 13 characters unused? If so, your two options are to index with offsets into one big 1-dimensional array, or to use dynamic memory.

The string class type should be Standard C++.
I'm not sure if that's a question, but yes, std::string is standard C++.
https://www.geeksforgeeks.org/c-string-class-and-its-applications/
Last edited on
Okay and thanks 😊 for your reply
Topic archived. No new replies allowed.