stuck in do loop

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

36 Replies - 828 Views - Last Post: 19 October 2012 - 11:51 AM Rate Topic: -----

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

stuck in do loop

Posted 09 October 2012 - 05:24 PM

I am having trouble with the do loop that is used to ensure the user is entering a correct gender. They either need to enter m, f or leave in blank. I think the problem is the while condition statement at the end of the loop. It just keeps prompting for a gender because it is obviously stuck in the loop. I think it is because I have:

while(clients[x].gender != 'm' || clients[x].gender != 'M' || clients[x].gender != 'f' || clients[x].gender != 'F' || clients[x].gender != ' ')


The if statements in the loop just check for equivalency because it gives me errors if I try to assign m or f to the char variable for some reason. So is there way to check if it is not equivalent to m, f, or blank rather than if it is not equal to it because I think that is what the issue is? Here is the whole program:

int main()
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_clients;
	int x, i;
	
	/* Information struct declaration */
	
	typedef struct 
	{
		char name[35];
		char address[50];
		char city[25];
		char state[3];
		long int zip_code;
		int age;
		char gender;
		char client_gender[50];
	}Information;
	Information clients[10];
	
	printf("Welcome to the Client Information Program!\n\n");
	
	/* Begin do loop */	
	
	do
	{
		printf("Enter the number of clients(1-10): "); /* Prompts for the number of employees */
		scanf("%i", &number_of_clients);
		fflush(stdin);
		
		if(number_of_clients < 1 || number_of_clients > 10) /* Ensures the number of employees is between 1 and 10 */
		{														/* or else displays error and re-prompts */		
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}	
	}while(number_of_clients < 1 || number_of_clients > 10); /* End do loop */
	
	for(x = 0; x < number_of_clients; x++)
	{
		printf("Enter name: ");
		gets(clients[x].name);
		
		printf("Enter street address: ");
		gets(clients[x].address);
		
		printf("Enter city: ");
		gets(clients[x].city);
		
		printf("Enter state: ");
		scanf("%s", &clients[x].state);
		fflush(stdin);
		
		do
		{
			printf("Enter zip code: ");
			scanf("%ld", &clients[x].zip_code);
			fflush(stdin);
			
			if(clients[x].zip_code < 00001 || clients[x].zip_code > 99999)
			{
				printf("Error: Zip code must be between 00001 and 99999, please re-enter!");
			}
		}while(clients[x].zip_code < 00001 || clients[x].zip_code > 99999);
		
		do
		{
			printf("Enter age: ");
			scanf("%i", &clients[x].age);
			fflush(stdin);
			
			if(clients[x].age < 1 || clients[x].age > 120)
			{
				printf("Error: Age must be between 1 and 120, please re-enter!");
			}
		}while(clients[x].age < 1 || clients[x].age > 120);
		
		do
		{
			printf("Enter gender (M or F): ");
			scanf("%s", &clients[x].gender);
			fflush(stdin);
			
			if(clients[x].gender == 'm' || clients[x].gender == 'M')
			{
				clients[x].client_gender == "He is %i years old.", clients[x].age;
			}
			else if(clients[x].gender == 'f' || clients[x].gender == 'F')
			{
				clients[x].client_gender == "She is %i years old.", clients[x].age;
			}
			else if(clients[x].gender == ' ')
			{
				clients[x].client_gender == "Age is %i years old.", clients[x].age;
			}
			else
			{
				printf("Error: Gender must be M, F, or blank, please re-enter!");
			}
		}while(clients[x].gender != 'm' || clients[x].gender != 'M' || clients[x].gender != 'f' || clients[x].gender != 'F' || clients[x].gender != ' ');
	}
	
	printf("The information you entered is:\n");
	
	for(i = 0; i < number_of_clients; i++)
	{
		printf(clients[x].name);
		printf(clients[x].address);
		printf("%s, %s %.5ld", clients[x].city, clients[x].state, clients[x].zip_code);
		printf(clients[x].client_gender);
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: stuck in do loop

#2 PixelStation  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 44
  • Joined: 12-November 09

Re: stuck in do loop

Posted 09 October 2012 - 05:51 PM

Your condition should read like this;
while(clients[x].gender != 'm' && clients[x].gender != 'M' && clients[x].gender != 'f' && clients[x].gender != 'F' && clients[x].gender != ' ')


Read it out like English; While gender is not M AND gender is not F.
Using OR will fail on the first condition if it's not exactly equal to 'm'.
Was This Post Helpful? 1
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 09 October 2012 - 06:05 PM

Thanks for your help. I thought to use or because it only needed one to be false before it ended the loop.
Was This Post Helpful? 0
  • +
  • -

#4 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 09 October 2012 - 06:20 PM

My problem now is I can't get the info to display in the second for loop depending on the gender entered. The output is different depending on what gender they enter. I think it might be because I'm comparing and not assigning the phrase to the variable I'm using (clients[i].client_gender) but if I assign it it says incompatible types, so i'm not sure how to fix this but I know the problem has to be within the if statements for the gender.

do
		{
			printf("Enter gender (M or F): ");
			scanf("%s", &clients[x].gender);
			fflush(stdin);
			
			if(clients[x].gender == 'm' || clients[x].gender == 'M')
			{
				clients[x].client_gender == "He is %i years old.", clients[x].age;
			}
			else if(clients[x].gender == 'f' || clients[x].gender == 'F')
			{
				clients[x].client_gender == "She is %i years old.", clients[x].age;
			}
			else if(clients[x].gender == ' ')
			{
				clients[x].client_gender == "Age is %i years old.", clients[x].age;
			}
			else
			{
				printf("Error: Gender must be M, F, or blank, please re-enter!");
			}
		}while(clients[x].gender != 'm' && clients[x].gender != 'M' && clients[x].gender != 'f' && clients[x].gender != 'F' && clients[x].gender != ' ');

Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 885
  • View blog
  • Posts: 4,037
  • Joined: 09-June 09

Re: stuck in do loop

Posted 09 October 2012 - 06:27 PM

Quote

clients[x].client_gender == "He is %i years old.", 



The == operator is not the assignment operator, = is the assignment operator.

Not only that, but you cannot assign string constants to character arrays. You can use a strcpy to accomplish this.

i.e.
strcpy(clients[x].client_gender, "He is 10 years old"); 



Note that the string constant in the second parameter will be be appended with a null terminating character in the string table.

This post has been edited by jjl: 09 October 2012 - 06:28 PM

Was This Post Helpful? 0
  • +
  • -

#6 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 09 October 2012 - 07:31 PM

Ok but I need it to display the age the user entered as well, like

strcpy(clients[x].client_gender, "He is %i years old", clients[x].age);


I know this isnt correct but this is what I need it to do. How can I accomplish this?
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 885
  • View blog
  • Posts: 4,037
  • Joined: 09-June 09

Re: stuck in do loop

Posted 09 October 2012 - 08:01 PM

You can use sprintf for that

sprintf(clients[x].client_gender, "He is %i years old", clients[x].age);



Here is documentation on sprintf
http://www.cplusplus...cstdio/sprintf/
Was This Post Helpful? 1
  • +
  • -

#8 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 11 October 2012 - 01:28 PM

I am having some trouble figuring out how to get the program to determine if the user enters nothing for the gender. I tried using a space but it is not working, the program just keeps waiting for the user to enter something. They can either enter m, f, or leave in blank, I'm not sure how to do this.

Here is the code:

int main()
{
	/* Variable Declarations */
	/*-----------------------*/
	
	int number_of_clients;
	int x, i;
	
	/* Information struct declaration */
	
	typedef struct 
	{
		char name[35];
		char address[50];
		char city[25];
		char state[3];
		long int zip_code;
		int age;
		char gender;
		char client_gender[50];
	}Information;
	Information clients[10];
	
	printf("Welcome to the Client Information Program!\n\n");
	
	/* Begin do loop */	
	
	do
	{
		printf("Enter the number of clients(1-10): "); /* Prompts for the number of employees */
		scanf("%i", &number_of_clients);
		fflush(stdin);
		
		if(number_of_clients < 1 || number_of_clients > 10) /* Ensures the number of employees is between 1 and 10 */
		{														/* or else displays error and re-prompts */		
			printf("\nError: Number of employees must be between 1 and 10, please re-enter!\n\n");
		}	
	}while(number_of_clients < 1 || number_of_clients > 10); /* End do loop */
	
	for(x = 0; x < number_of_clients; x++)
	{
		printf("Enter name: ");
		gets(clients[x].name);
		
		printf("Enter street address: ");
		gets(clients[x].address);
		
		printf("Enter city: ");
		gets(clients[x].city);
		
		printf("Enter state: ");
		scanf("%s", &clients[x].state);
		fflush(stdin);
		
		do
		{
			printf("Enter zip code: ");
			scanf("%ld", &clients[x].zip_code);
			fflush(stdin);
			
			if(clients[x].zip_code < 00001 || clients[x].zip_code > 99999)
			{
				printf("Error: Zip code must be between 00001 and 99999, please re-enter!\n");
			}
		}while(clients[x].zip_code < 00001 || clients[x].zip_code > 99999);
		
		do
		{
			printf("Enter age: ");
			scanf("%i", &clients[x].age);
			fflush(stdin);
			
			if(clients[x].age < 1 || clients[x].age > 120)
			{
				printf("Error: Age must be between 1 and 120, please re-enter!\n");
			}
		}while(clients[x].age < 1 || clients[x].age > 120);
		
		do
		{
			printf("Enter gender (M or F): ");
			scanf("%s", &clients[x].gender);
			fflush(stdin);
			
			if(clients[x].gender == 'm' || clients[x].gender == 'M')
			{
				sprintf(clients[x].client_gender, "He is %i years old", clients[x].age);
			}
			else if(clients[x].gender == 'f' || clients[x].gender == 'F')
			{
				sprintf(clients[x].client_gender, "She is %i years old", clients[x].age);
			}
			else if(clients[x].gender == ' ')
			{
				sprintf(clients[x].client_gender, "Age is %i years old", clients[x].age);
			}
			else
			{
				printf("Error: Gender must be M, F, or blank, please re-enter!\n");
			}
		}while(clients[x].gender != 'm' && clients[x].gender != 'M' && clients[x].gender != 'f' && clients[x].gender != 'F' && clients[x].gender != ' ');
	}
	
	printf("\nThe information you entered is:\n\n");
	
	for(i = 0; i < number_of_clients; i++)
	{
		printf(clients[i].name);
		printf("\n");
		printf(clients[i].address);
		printf("\n");
		printf("%s, %s %.5ld", clients[i].city, clients[i].state, clients[i].zip_code);
		printf("\n");
		printf(clients[i].client_gender);
		printf("\n");
	}
}

Was This Post Helpful? 0
  • +
  • -

#9 AKMafia001  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 448
  • Joined: 11-June 11

Re: stuck in do loop

Posted 11 October 2012 - 01:41 PM

scanf("%s", &clients[x].gender);


Read it as a char instead of string, and your program will read space as well!

Correct:
scanf("%c", &clients[x].gender);



Hope this Helps!
Was This Post Helpful? 1
  • +
  • -

#10 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 11 October 2012 - 01:53 PM

Thank you!
Was This Post Helpful? 0
  • +
  • -

#11 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 12 October 2012 - 04:37 AM

Apparently I need to use getchar instead of scanf for the gender. I tried using it but it's not letting me continue when I enter a valid entry. Also I had it set up where entering nothing for the gender(which is valid) required the user to enter a space, but my professor wants them to be able to just press enter once the program prompts for a gender and accept that as nothing. I'm having trouble figuring this out as well. Here is the section of the program:

do
		{
			printf("Enter gender (M, F, or enter a space to leave blank): ");
			getchar(clients[x].gender);
			fflush(stdin);
			
			if(clients[x].gender == 'm' || clients[x].gender == 'M')
			{
				sprintf(clients[x].client_gender, "He is %i years old", clients[x].age);
			}
			else if(clients[x].gender == 'f' || clients[x].gender == 'F')
			{
				sprintf(clients[x].client_gender, "She is %i years old", clients[x].age);
			}
			else if(clients[x].gender == ' ')
			{
				sprintf(clients[x].client_gender, "Age is %i years old", clients[x].age);
			}
			else
			{
				printf("Error: Gender must be M, F, or blank, please re-enter!\n");
			}
		}while(clients[x].gender != 'm' && clients[x].gender != 'M' && clients[x].gender != 'f' && clients[x].gender != 'F' && clients[x].gender != ' ');

Was This Post Helpful? 0
  • +
  • -

#12 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 407
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: stuck in do loop

Posted 12 October 2012 - 04:40 AM

Have you tried using NULL. I think that should probably work.

 else if(clients[x].gender == NULL)


regards,
Raghav

This post has been edited by raghav.naganathan: 12 October 2012 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#13 AKMafia001  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 448
  • Joined: 11-June 11

Re: stuck in do loop

Posted 12 October 2012 - 08:59 AM

Apparently getchar() doesn't takes any arguments. The prototype for getchar() is:
int getchar(void);

Instead you can make little change in your code...

I mean to say:
clients[x].gender = getchar();


Hope this Helps!

PS: It will take spaces too as an input... ;)
Was This Post Helpful? 0
  • +
  • -

#14 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 146
  • Joined: 29-May 12

Re: stuck in do loop

Posted 12 October 2012 - 10:51 AM

ok I changed it using getchar and also tried using null for leaving it blank but it's giving me errors where null is. It keeps saying null is undeclared, it doesn't make any sense to me since it's just null. Here is the updated code:
do
		{
			printf("Enter gender (M, F, or enter a space to leave blank): ");
			clients[x].gender = getchar();
			fflush(stdin);
			
			if(clients[x].gender == 'm' || clients[x].gender == 'M')
			{
				sprintf(clients[x].client_gender, "He is %i years old", clients[x].age);
			}
			else if(clients[x].gender == 'f' || clients[x].gender == 'F')
			{
				sprintf(clients[x].client_gender, "She is %i years old", clients[x].age);
			}
			else if(clients[x].gender == null)
			{
				sprintf(clients[x].client_gender, "Age is %i years old", clients[x].age);
			}
			else
			{
				printf("Error: Gender must be M, F, or blank, please re-enter!\n");
			}
		}while(clients[x].gender != 'm' && clients[x].gender != 
'M' && clients[x].gender != 'f' && clients[x].gender != 'F' && clients[x].gender != null);
Was This Post Helpful? 0
  • +
  • -

#15 AKMafia001  Icon User is offline

  • D.I.C Regular

Reputation: 135
  • View blog
  • Posts: 448
  • Joined: 11-June 11

Re: stuck in do loop

Posted 12 October 2012 - 01:08 PM

Your code just works fine when you compare it with the space...
15	            else if(clients[x].gender == ' ')

Have you tried it?

Just modify your code as I said in my earlier post, that change your getchar()...
Remember that null is different than Space. Don't bother to compare a space with null and expect the condition to become true...
Char    NUL     Space
Dec       0        32
Hx        0        20
Oct     000       040


PS: If in future you want to use null in your any code, then try nullptr...

Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3