Thread: I'm new to C, confuse with something..

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    12

    I'm new to C, confuse with something..

    Dear sir and madam,

    I would like to ask why it print 2 times instead of one if I input a wrong character ._.?

    Code:
    #include<stdio.h>
    
    int main()
    {
    	float H_P = 24.90, I_S = 25.90, V_D = 25;
    	float G_B = 3, SOUP = 5;
    	char choice;
    
    
    	printf("Scruptious Pizza\n");
    	printf("================\n\n\n");
    	printf("Choice\t\tType\t\t\tSelling Prize(RM)\n");
    	printf("_________________________________________________________\n");
    	printf("Pizza\t\t(a) Hawaiian\t\t\t%.2f\n", H_P);
    	printf("\t\t(b) Island Supreme\t\t%.2f\n", I_S);
    	printf("\t\t(c) Veggie Delight\t\t%.2f\n\n", V_D);
    	printf("Side\t\t(d) Garlic Bread\t\t %.2f\n", G_B);
    	printf("Order\t\t(e) Soup\t\t\t %.2f\n\n\n", SOUP);
    	printf("_________________________________________________________\n\n");
    	printf("\t\tPlease Choose:\n");
    	printf("\t\t[S]ales\n");
    	printf("\t\t[P]ayment\n");
    	printf("\t\t[E]xit");
    
    
    	do {
    		printf("\n\t\tYour choice:"); /*Why it print 2 times instead of one if I input a wrong character :O*/
    		scanf("%c", &choice);
    	}
    	while (choice != 'S' && choice != 's' && choice != 'P' && choice != 'p' && choice != 'E' && choice != 'e');
    	
    	if (choice == 'S') /*The Sales screen <3*/
    		{
    		/*clrscr(); idk why it don't work here..*/
    		printf("\tSALES\n");
    		printf("\t_____\n");
    	}
    }
    I'm new to C, confuse with something..-131231231-jpg

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Because you have the return key stored in your input buffer every time you read in one char. Issue a getchar() afterwards to discard it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    12
    Quote Originally Posted by claudiu View Post
    Because you have the return key stored in your input buffer every time you read in one char. Issue a getchar() afterwards to discard it.
    Can you explain in a more simple way? ><
    And actually what is the function for getchar();?

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Every time you try to read in one character you implicitly read two, the one you type in and the 'enter' key. Unless you discard the latter, it will be picked up as input on your next iteration in the loop.

    Why don't you Google getchar?
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,036
    Better option is to Google scanf.
    scanf - C++ Reference

    And use

    Code:
    scanf(" %c", &choice);
    Instead of
    Code:
    scanf("%c", &choice);
    [in format]Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    12
    Thanks all, now I know what I did wrong.. You guys are so helpful ;D Thanks <3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PayPal confuse me
    By Akkernight in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 04-20-2009, 11:56 AM
  2. a little confuse o_0???
    By omarbags in forum C++ Programming
    Replies: 15
    Last Post: 01-24-2008, 01:03 AM
  3. confuse about included files
    By C-Dumbie in forum C++ Programming
    Replies: 2
    Last Post: 12-31-2002, 07:04 PM
  4. confuse with character array
    By dv007 in forum C Programming
    Replies: 6
    Last Post: 08-09-2002, 01:05 PM
  5. Too confuse
    By dv007 in forum C++ Programming
    Replies: 6
    Last Post: 07-25-2002, 05:33 PM