Thread: getchar()

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    getchar()

    Hi. My school assignment is to write a program to help someone learn their time tables. It works as expected however when trying to replace a system("pause") at the end of the program with a getchar() alongside press enter instructions I find that that part of the code is skipped over and the program instantly returns zero after my printf "press enter to exit" function. Any help would be much appreciated thanks.
    Sorry for the lack of comments but the variables are pretty obvious and its really a simple program.
    (advice on the rand() would be nice too but I want to understand the lack of chance for user input on the getchar first)

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    int num1,num2,answer,guess,menu,diff;
    
    printf("Please select from the following...");
    printf("\n1. Calculator\n2. Quiz\n0. Exit\n");
    	scanf("%i",&menu);
    
    while(menu)
    {
    if(menu==1)
    {
    	printf("\nEnter a number:");
    		scanf("%i",&num1);
    	printf("\nEnter another number:");
    		scanf("%i",&num2);
    	
    	answer=num1*num2;
    	
    	printf("\nWhat is %iX%i? ",num1,num2);
    		scanf("%i",&guess);
    	
    	if (guess==answer)
    		{
    			printf("\nCORRECT");
    		}
    	else
    		{
    			printf("\nIncorrect, it is %i",answer);
    			diff=guess-answer;
    			printf("\nYou were off by %i",diff);
    		}
    }
    
    else if(menu==2)
    {
    	num1=rand() % 12 + 1;
    	num2=rand() % 12 + 1;
    	printf("\n%i X %i =",num1,num2);
    		scanf("%i",&guess);
    	
    	answer=num1*num2;
    		
    	if (guess==answer)
    		{
    			printf("\nCORRECT");
    		}
    	else
    		{
    			printf("\nIncorrect, it is %i",answer);
    			diff=guess-answer;
    			printf("\nYou were off by %i",diff);
    		}
    }
    else
    {
    printf("\nInput Error, Please Try Again");
    }
    
    printf("\n\nPlease select from the following...");
    printf("\n1. Calculator\n2. Quiz\n0. Exit\n");
    	scanf("%i",&menu);
    }
    printf("\nPress Enter to Exit\n");
    getchar();
    return 0;
    }
    p.s. also how to take the magnitude of a variable(i.e. "diff"). Square and Sqrt?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    scanf leaves the newline in the buffer because you're not doing anything with it, so getchar immediately reads it and returns.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    scanf() doesn't catch the '\n' in the stream. You'll have to manually drain the input buffer. Usually, on this site, you'll be told to not use scanf(), but to use fgets() and sscanf() in it's place (as fgets() has more protection against goofy data). However, your code as it stands would need something like ch='a'; while ((ch != EOF) && (ch != '\n')) ch = getchar(); after your scanf() torain and trash any remaining chars after the valid data is processed.

    EDIT: Come on quzah. . . you need to start proofing your posts so I'm not 4+ minutes behind you each post.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Type faster. Besides, there's an edit button, so why proof read?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Hey thanks for the quick help I just had to run off for a moment. Ummm I'm tired and will take a more in depth look at fget later. Ugh I wish i hadn't put this off to the last minute. I'll either leave it as is or maybe try to put to getchars in a row or something so that the first will eat the \n and the second works as it should. Or clean out the buffers if there's an easy function for that. Actually I'm now thinking that the 2 getchars should work. Though pretty shoddy programming. Maybe I'll try now.

    EDIT:
    Ehhhh works like a charm first one eats it second one works. Hopefully the teach won't notice the uber weakness.
    Specially on the whole random numbers start off the same every time lol.
    Last edited by richfatsanta; 01-17-2007 at 02:24 AM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    srand will fix that. There's a FAQ on random numbers you may want to look at.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User Psi's Avatar
    Join Date
    Dec 2006
    Posts
    3

    Thumbs up Thanks

    I had the same problem using getchar() as a pause. Thank you for your instructions. Using double getchar() is really smart and simple.
    Last edited by Psi; 01-18-2007 at 12:59 AM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Psi
    I had the same problem using getchar() as a pause. Thank you for your instructions. Using double getchar() is really smart and simple.
    . . . until there's something besides a newline in the input buffer.

    Ideally you might use
    Code:
    int c;
    
    while((c = getchar()) != '\n' && c != EOF);
    but if you don't think the user will type EOF then this will suffice
    Code:
    while(getchar() != '\n');
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getchar() problem
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 01-25-2006, 02:49 PM
  2. getchar buffer size
    By oncemyway in forum C Programming
    Replies: 3
    Last Post: 08-02-2005, 12:49 AM
  3. getchar() problem from K&R book
    By anemicrose in forum C Programming
    Replies: 13
    Last Post: 04-04-2004, 11:06 PM
  4. help with getchar lol
    By Taco Grande in forum C Programming
    Replies: 5
    Last Post: 03-18-2003, 09:25 PM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM