How can you follow that program? The formatting is inconsistent making it very difficult to read. See this

I can't be certain if this is the cause because of your formatting but with all the fgets() in your program, why are you using that single scanf() ? See this too.

How can you follow that program? The formatting is inconsistent making it very difficult to read. See this

I can't be certain if this is the cause because of your formatting but with all the fgets() in your program, why are you using that single scanf() ? See this too.

I thought I was scanning a number, but it's character, you're true...:S

for (j = 0; j < 10 && strcmp (choice,"yes") != 0; j++) This is part of the problem, it should be strcmp == 0 as when the condition of the for loop goes false the loop stops.

It still is skipping over those fgets. I'm looking into that.

In the meantime you should look into rearranging things a bit so you're taking a name, then taking a number, that way the name and number arrays line up. Otherwise you are asking the user to enter 10 names and THEN enter 10 numbers.

I corrected them, and now I really don't get why it doesn't work:

#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++)	{
			fgets (number[k], 10, stdin);
			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 ("%s\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 (strcmp(quit,"exit") == 0);
	return 0;
}

I think these fgets doesn't work, because it doesn't give me the time to enter something. It scans it directly.

http://img51.imageshack.us/i/naamlooscj.jpg/

char option[7]; //at the top
....
fgets (option, 7, stdin);
for (m = 0;m <sizeof(option);m++)
    if(option[m] == '\n')
    {
         option[m] = '\0';
	 break;
     }

What we overlooked is that fgets may take in a newline from when we hit enter. You need to have the above construct to get rid of it and put a '\0' (for null termination) in the right place. That way the longer string will be truncated to the same length as the one you are using for strcmp. See this for details.

You'll have to do this for all the strings you read in.

You still didn't change the flow of your program to be like:
"Enter a name:"
"Enter a number: "
"Do you want to enter more numbers?"
(which I think is the most logical way of doing it -- if you were running this program to use as an actual telephone directory you wouldn't want to remember which name you entered first, third etc.)

I don't really know what the data looks like, but jonsca hit on something I feel a need to expand on.

Assume you are at:

printf ("What is the name of the person?\n");
fgets (name[j], 10, stdin);

and the you enter the name
Montgomery
you just entered 11 characters and the program read 9. It read
MONTGOMER and added the trailing \0.
The next fgets() will read the y[enter] and immediately continue. You won't get a chance to type anything.

You need to be very careful when programming input for the keyboard, taking into account slightly irregular input like this.

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.