Hello everybody,

I was trying to make a telephone directory, when I ran in to several problems with the compiling. When I fixed these, I saw that the programm wasn't working as I want it to work :)
Can anyone tell me what I did wrong, and try to help me out of this?

#include <cstdio>
#include <cstring>
int main ()
{
	char name[10][10], temporary_name[10];
	char option[6];
	__int64 number[10];
	int i,j,k;
	char quit[10];
	do	{
		printf ("This is a telephone directory.\n");
		printf ("What do you want to do?\n");
		printf ("Typ 'enter' to enter a number, typ 'view' to view a number.\n");
		scanf ("%s\n", &option);
		if (option == "enter")	{
			printf ("What is the name of the person?\n");
			for (j = 0; j < 10; j++)	{
			gets (name[j]);
			}
			printf ("What is the number of the person?\n");
			for (k = 0; k <10; k++)	{
			scanf ("%I64d", &number[k]);
			}
			printf ("Person stored in memory.\n");
		}	
		else	{
			printf ("Enter the name of the person.\n");
			gets (temporary_name);
			for (i = 0; i < 10; i++)	{
				if (strcmp (name[i],temporary_name) != 0)	{
					printf ("%I64d\n", number[i]);
				}
			}
		}
		printf ("What do you want to do now?\n");
		printf ("Typ continue if you want to go to the menu, typ exit to exit.\n");
		scanf ("%s", &quit);
	}	while (quit != "exit");
	return 0;
}

Recommended Answers

All 35 Replies

If you are writing a C++ program why are you only using the C libraries?

See this about gets(). It may not solve all the problems with your code but it will be a step in the right direction. Use something like fgets for all your char array inputs.

commented: Moved to C +12

Ok, will try that.
But I'm not sure it will work, because compiling went well, but when I ran the program, I got trouble here:

if (option == "enter") {

Instead of saying ' What is the name of the person ', it says ' What is the name of the person? '; so it enters the else syntax.
P.S. Can you tell me what header I have to use for 'fgets'?

>>if (option == "enter") {
You can not compare strings like that in C programs. use if( stramp(option, "enter") == 0) { >> tell me what header I have to use for 'fgets'?
stdio.h

Ok, will try that.
But I'm not sure it will work, because compiling went well, but when I ran the program, I got trouble here:

if (option == "enter") {

Instead of saying ' What is the name of the person ', it says ' What the name of the person? '; so it enters the else syntax.
P.S. Can you tell me what header I have to use for 'fgets'?

I mean: 'It says, Enter the name of the person :$

Ok, will try all that stuff and tell you whether it works or not :)

Ok, I changed this in my source code :

#include <cstdio>
#include <cstring>
int main ()
{
	char name[10][10], temporary_name[10];
	char option[6];
	__int64 number[10];
	int i,j,k;
	char quit[10];
	do	{
		printf ("This is a telephone directory.\n");
		printf ("What do you want to do?\n");
		printf ("Typ 'enter' to enter a number, typ 'view' to view a number.\n");
		scanf ("%s\n", &option);
		if	(stramp (option, "enter") == 0)	{
			printf ("What is the name of the person?\n");
			for (j = 0; j < 10; j++)	{
			fgets (name[j]);
			}
			printf ("What is the number of the person?\n");
			for (k = 0; k <10; k++)	{
			scanf ("%I64d", &number[k]);
			}
			printf ("Person stored in memory.\n");
		}	
		else	{
			printf ("Enter the name of the person.\n");
			fgets (temporary_name);
			for (i = 0; i < 10; i++)	{
				if (strcmp (name[i],temporary_name) != 0)	{
					printf ("%I64d\n", number[i]);
				}
			}
		}
		printf ("What do you want to do now?\n");
		printf ("Typ continue if you want to go to the menu, typ exit to exit.\n");
		scanf ("%s", &quit);
	}	while (quit != "exit");
	return 0;
}

But when compiling I get three errors:
http://img10.imageshack.us/i/compiling.jpg/

See below:

Ok, I changed this in my source code :

#include <cstdio>
#include <cstring>
int main ()
{
	char name[10][10], temporary_name[10];
	char option[6];
	__int64 number[10];

Just make this a char number[10][10]; since you're not doing any computations with the digits anyway

int i,j,k;
	char quit[10];
	do	{
		printf ("This is a telephone directory.\n");
		printf ("What do you want to do?\n");
		printf ("Typ 'enter' to enter a number, typ 'view' to view a number.\n");
		scanf ("%s\n", &option);
		if	(stramp (option, "enter") == 0)	{

Typo on strcmp.
For scanf, just use fgets all around, for example fgets(option,sizeof(option),stdin)

printf ("What is the name of the person?\n");
			for (j = 0; j < 10; j++)	{
			fgets (name[j]);

Not the right syntax for fgets, but better approach!

}
			printf ("What is the number of the person?\n");
			for (k = 0; k <10; k++)	{
			scanf ("%I64d", &number[k]);   //see above
			}
			printf ("Person stored in memory.\n");
		}	
		else	{
			printf ("Enter the name of the person.\n");
			fgets (temporary_name);  //syntax
			for (i = 0; i < 10; i++)	{
				if (strcmp (name[i],temporary_name) != 0)	{
					printf ("%I64d\n", number[i]);
				}
			}
		}
		printf ("What do you want to do now?\n");
		printf ("Typ continue if you want to go to the menu, typ exit to exit.\n");
		scanf ("%s", &quit);  //again fgets
	}	while (quit != "exit");
	return 0;
}

Ok, I'll do that. Just a question, is this a correct fgets syntax?

fgets (name[j], 20, stdin);

And, why do I always have to use fgets, and not only instead of gets?
Do you want me to replace all the scanf with fgets?

Except that the length of name[j] is 10, but everything else seems to be ok.

And, why do I always have to use fgets, and not only instead of gets?
Do you want me to replace all the scanf with fgets?

both scanf("%s" ...) and gets() will let you enter more characters into the buffer than the buffer will hold. Also, if the name you want to enter contains a space then neither function will allow that.

Ok, I've made it, and compiling went well:

#include <cstdio>
#include <cstring>
int main ()
{
	char name[10][10], temporary_name[10];
	char option[6];
	char number[10][10];
	int i,j,k;
	char quit[10];
	do	{
		printf ("This is a telephone directory.\n");
		printf ("What do you want to do?\n");
		printf ("Typ 'enter' to enter a number, typ 'view' to view a number.\n");
		scanf ("%s\n", &option);
		if	(strcmp (option, "enter") == 0)	{
			printf ("What is the name of the person?\n");
			for (j = 0; j < 10; j++)	{
			fgets (name[j], 10, stdin);
			}
			printf ("What is the number of the person?\n");
			for (k = 0; k <10; k++)	{
			scanf ("%s\n", &number[k]);
			}
			printf ("Person stored in memory.\n");
		}	
		else	{
			printf ("Enter the name of the person.\n");
			fgets (temporary_name, 10, stdin);
			for (i = 0; i < 10; i++)	{
				if (strcmp (name[i],temporary_name) != 0)	{
					printf ("%I64d\n", number[i]);
				}
			}
		}
		printf ("What do you want to do now?\n");
		printf ("Typ continue if you want to go to the menu, typ exit to exit.\n");
		fgets (quit, 10, stdin);
	}	while (quit != "exit");
	return 0;
}

But when it asks for a name, and I type in example, i have to push 3 times enter before it asks for a number, which happens because it thinks that the name is 10 characters long, how do change this?

both scanf("%s" ...) and gets() will let you enter more characters into the buffer than the buffer will hold. Also, if the name you want to enter contains a space then neither function will allow that.

ok, I thought the same.

You're having it ask for 10 names in a row before a number. You could combine your for loops into one and take in name[0], number[0], name[1], number[1] etc. or do it in a while loop that asks if you want to keep entering more names and numbers each cycle.

Also, you've still got some scanfs in there. You should ideally transition all of those into fgets.

Forgot these scanf's. :)
Will do that later.
But I don't understand what you want to do with the for loops, can you explain it again please? :|

Forgot these scanf's. :)
Will do that later.
But I don't understand what you want to do with the for loops, can you explain it again please? :|

Edit: Do you mean something like:

#
do {
for (j = 0; j < 10; j++) {
fgets (name[j], 10, stdin);
}
printf ("Do you want to enter more numbers? (yes or no)");
fgets (choice, 3, stdin); 
}
while (choice == "yes")

Don't forget that choice == "yes" doesn't work for char arrays (or any type of strings in C for that matter).

Yes, something along those lines will do just fine. Except make j part of your while loop condition too while(strcmp(choice,"yes") == 0 && j <10) and increment j.

But, Instead of the for loop, get one name and number then prompt for more. So wrap up your prompting for one name, number pair in that do/while loop.

Ok, but can I also do this?

for (j = 0 && choice = "yes"; j < 10 && strcmp (choice,"yes") != 0; j++) {
fgets (name[j], 10, stdin);
printf ("Do you want to enter more numbers? (yes or no)");
fgets (choice, 3, stdin); 
}

j = 0 && choice = "yes" Is not going to work at all. You can't initialize choice after it's been declared, and using an && between those two could make sense in an obscure case but, assuming it would work, would only get you 0 in j anyway. Other than that, the way you have it is fine, I believe, except that you only ask for a name and not a telephone number.

j = 0 && choice = "yes" Is not going to work at all. You can't initialize choice after it's been declared, and using an && between those two could make sense in an obscure case but, assuming it would work, would only get you 0 in j anyway. Other than that, the way you have it is fine, I believe, except that you only ask for a name and not a telephone number.

Ok, I think

j = 0 && choice = "yes"

will be the error of the compiling (I got one error). I'll make some kind like the same for loop for the numbers.
How do I solve the

j = 0 && choice = "yes"

Do I just leave out the

&& choice = "yes

and initiialize it before the for loop? Thanks for all your work :)

Leave out the second part like you said. You'll have to go up to the top where choice was declared and put char choice[] = "yes";

Ok, one question still:
someone told me that you can initialize the choice = yes in the for loop by putting a ' ,' after the initialization of j = 0. Is that true?

Nope, not in this context. Char arrays can only be given a constant value when they are declared (I suppose you could have a great big chain of statements with choice[0] = 'y',choice[1] = 'e',choice[2] = 's',choice[3] = '\0' but that would be a little absurd.

You can use the comma operator for other things so if you had an int k, you could have j = 0,k = 0 and it would work fine.

There's a sure fire way to see if anything is going to work or not hehe. Test it out.

strcpy(choice, "yes"); Anywhere you need it initialized.
:icon_rolleyes:

commented: This round's all yours ;) +2

Oops, didn't think about putting that after the comma. I'm on a roll today.

commented: Hey, sometimes it's better to just go to bed :D +9

Oops, didn't think about putting that after the comma. I'm on a roll today.

I wouldn't put it after the comma. It should be the statement before.

IMAO, comma'd initialization is bad form unless it's absolutely necessary.

Ok, but can I also do this?

for (j = 0 && choice = "yes"; j < 10 && strcmp (choice,"yes") != 0; j++) {
fgets (name[j], 10, stdin);
printf ("Do you want to enter more numbers? (yes or no)");
fgets (choice, 3, stdin); 
}

You cannot assign choice the value yes in the for loop. Either do it during initialization

char choice []="yes";

or use strcpy

char choice[10];

 for (j = 0 && strcpy(choice, "yes"); j < 10 && strcmp (choice,"yes")

So now I have this:

#include <cstdio>
#include <cstring>
int main ()
{
	char name[10][10], temporary_name[10];
	char option[6];
	char choice[] = "yes";
	char choice2[] = "yes";
	char number[10][10];
	int i,j,k;
	char quit[10];
	do	{
		printf ("This is a telephone directory.\n");
		printf ("What do you want to do?\n");
		printf ("Typ 'enter' to enter a number, typ 'view' to view a number.\n");
		fgets (option, 6, stdin);
		if	(strcmp (option, "enter") == 0)	{
			printf ("What is the name of the person?\n");
			for (j = 0; j < 10 && strcmp (choice,"yes") != 0; j++)	{
				fgets (name[j], 10, stdin);
				printf ("Do you want to enter more numbers? (yes or no)");
				fgets (choice, 3, stdin); 
			}
			printf ("What is the number of the person?\n");
			for (k = 0; k <10 && strcmp (choice2,"yes") != 0; k++)	{
			scanf ("%s\n", &number[k]);
			printf ("Do you want to enter more numbers? (yes or no)");
			fgets (choice, 3, stdin); 
			}
			printf ("Person stored in memory.\n");
		}	
		else	{
			printf ("Enter the name of the person.\n");
			fgets (temporary_name, 10, stdin);
			for (i = 0; i < 10; i++)	{
				if (strcmp (name[i],temporary_name) != 0)	{
					printf ("%d\n", number[i]);
				}
			}
		}
		printf ("What do you want to do now?\n");
		printf ("Typ continue if you want to go to the menu, typ exit to exit.\n");
		fgets (quit, 10, stdin);
	}	while (quit != "exit");
	return 0;
}

But it still works strange; it doesn't give me the time to enter a name of a person in the if loop, it immediately prompts enter the number.... Any help? Thanks! :$

http://img85.imageshack.us/i/naamloosqw.jpg/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.