Thread: Homework Help

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    4

    Homework Help

    I am currently working on a homework assignment and am trying to create a loop in a menu. In the menu, I need to evaluate the input. If it is not a character, I need it to say, "Invalid Entry, try again" and loop back to the input again. My input needs to be one of the following: D, E, L, or Q. If the entry is not correct, it needs to loop back in order to allow the user to input a correct character.

    In the following code, I was trying to just evaluate that the input was a character and not a digit. I am using the switch/case below in the program and would like to catch an input error before the program gets to that part.

    Code:
    	s='\0';
    	
    	while (isdigit(s)==0){
    	
    	printf("\n\n\tStore selection menu\n\t---------------------\n\n");//Store Menu Menu
    	printf("\tD)  Del Mar\n");
    	printf("\tE)  Encinitas\n");
    	printf("\tL)  La Jolla\n");
    	printf("\tQ)  Quit\n\n");
    	printf("\tSelect D, E, L, or Q : ");
    	scanf("%c", &s);	
    	
    	if (isdigit(s)==0)
    		break;
    	else
    		printf("\tInvalid response, try again");
    		
    	}
    I have included the <ctype.h> in the beginning of the program. This is just a portion of the program.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps isalpha rather than isdigit then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    I tried that and it worked, now I've got to figure out how to get the program to loop back if the user enters a character that is not D, E, L, or Q. I am thinking that I need to use that at the bottom of the switch case under default:, but I can't seem to get a grip on which looping structure to use.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    I forgot to add my code, here it is:

    Code:
    while (isalpha(s)==0){//beginning of loop to test whether input is a letter, starts as a number.
    		
    	printf("\n\n\tStore selection menu\n\t---------------------\n\n");//Store Menu Menu
    	printf("\tD)  Del Mar\n");
    	printf("\tE)  Encinitas\n");
    	printf("\tL)  La Jolla\n");
    	printf("\tQ)  Quit\n\n");
    	printf("\tSelect D, E, L, or Q : ");
    	scanf("&#37;c", &s);	
    	
    	if( isalpha(s) )
    		break;
    	else
    		printf("\n\tInvalid response, try again");
    		//this may be of some importance - != is the same as not equal to in C
    	}//end of loop
    	
    	d=7.25;
    	e=7.50;
    	l=7.75;
    	
    	//calculations of Sales Tax
    	x=p*d*0.01;//del mar result
    	y=p*e*0.01;//encinitas result
    	z=p*l*0.01;//la jolla
    	
    	switch (s) {
    	
    	case 'd': case 'D':
    	printf("\n\tStore Name : Del Mar");
    	printf("\n\tTax Rate : %.2f", d);
    	printf("%%");
    	printf("\n\tPurchase Amount : %.2f", p);
    	printf("\n\tTax Amount : %.2f", x);
    	printf("\n\tTotal Sale Amount : %.2f", p+x);
    	break;
    	case 'e': case 'E':
    	printf("\n\tStore Name : Encinitas");
    	printf("\n\tTax Rate : %.2f", e);
    	printf("%%");
    	printf("\n\tPurchase Amount : %.2f", p);
    	printf("\n\tTax Amount : %.2f", y);
    	printf("\n\tTotal Sale Amount : %.2f", p+y);
    	break;
    	case 'l': case 'L':
    	printf("\n\tStore Name : La Jolla");
    	printf("\n\tTax Rate : %.2f", l);
    	printf("%%");
    	printf("\n\tPurchase Amount : %.2f", p);
    	printf("\n\tTax Amount : %.2f", z);
    	printf("\n\tTotal Sale Amount : %.2f", p+z);
    	break;
    	case 'q': case 'Q':
    	exit (1);
    	default:
    	printf("Invalid Response, Try Again");
    	
    	}
    }

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    4
    I scraped the whole thing and started over using ASCII codes for letters and using if statements to evaluate and a do/while loop. Everything worked out fine, my homework should be complete after a few cosmetic alterations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 11-03-2001, 04:39 PM
  2. Homework
    By kermi3 in forum C++ Programming
    Replies: 15
    Last Post: 09-26-2001, 03:16 PM
  3. Homework
    By kermi3 in forum Windows Programming
    Replies: 5
    Last Post: 09-15-2001, 11:48 AM
  4. Homework
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 09-10-2001, 01:26 PM