Thread: getchar() problem

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    getchar() problem

    This code works exactly like I expect by itself:

    Code:
           #include <stdio.h>
    
    	int main()
    	{
    	char reqOrSugg, oldOrNew; 
    	
      	printf ("\nIs This Textbook Required Or Suggested?\n");
    	printf ("Enter R for Required or S for Suggested:\n");
    	reqOrSugg = getchar();
    	while (getchar() != '\n');
    
    	printf ("Is This Textbook Old Or New?\n");
    	printf ("Enter O for old or N for new:\n");
    	oldOrNew = getchar();
    	while (getchar() != '\n');
    
    	printf ("%c%s",reqOrSugg,"\n");
    	printf ("%c%s",oldOrNew,"\n");
    
    	return 0;
            }
    But in my program seen next it is skipping reading in the R but the N is read in fine.

    Code:
            ~
            printf ("\nPlease Enter The Expected Enrollment: \n");
    	scanf ("%lf", &expectedEn);
    	printf ("%1.0lf%s",expectedEn,"\n");
    	
    	printf ("\nIs This Textbook Required Or Suggested?\n");
    	printf ("Enter R for Required or S for Suggested:\n");
    	reqOrSugg = getchar();
    	while (getchar() != '\n');
    
    	printf ("Is This Textbook Old Or New?\n");
    	printf ("Enter O for old or N for new:\n");
    	oldOrNew = getchar();
    	while (getchar() != '\n');
    
    	printf ("%c%s",reqOrSugg,"\n");
    	printf ("%c%s",oldOrNew,"\n");
    
            if (reqOrSugg == 'R' || oldOrNew == 'N')
    		{
    		numToOrd = expectedEn * .9;
    		}
            ~
    Can anyone explain this to me and tell me how to correct this? I suspect it has something to do with the new lines. I cant get the if/else to work without this working first. Ty

    Justin

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    printf ("\nPlease Enter The Expected Enrollment: \n");
    scanf ("%lf", &expectedEn);
    printf ("%1.0lf%s",expectedEn,"\n");
    	
    printf ("\nIs This Textbook Required Or Suggested?\n");
    printf ("Enter R for Required or S for Suggested:\n");
    reqOrSugg = getchar();
    while (getchar() != '\n');
    
    printf ("Is This Textbook Old Or New?\n");
    printf ("Enter O for old or N for new:\n");
    oldOrNew = getchar();
    while (getchar() != '\n');
    The scanf function will leave the trailing newline character in the input stream after reading in the enrollment. This newline character is read by the next getchar call and saved into the reqOrSugg variable. You will then be in the while loop waiting for another newline character but thinking you are still at the point in the line above where you wish to enter the R/S character. Once you press R/S and then enter you exit the while loop. Your R/S choice is not stored anywhere, it is discarded.

    Solution: You need to get rid of the newline character after your call to scanf.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    Makes sense to me but riddle me this. I tried it with ALL the \n taken out before the getchar() and still cant read in reqOrSugg. Even if that would have worked I am still screwed because all the output comes out messed up and thats major deductions.

    How can I format the output by line w/o messing up getchar()? Besides I thought that this
    Code:
    	while (getchar() != '\n');
    was supposed to make the getchar() ignore new lines?

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    Been thinking about it and maybe its not the \n. I am thinking that getchar() is reading in the <return> from the previous input. If thats the case how do I getchar() with out reading in the <return>?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by jlharrison
    Makes sense to me but riddle me this. I tried it with ALL the \n taken out before the getchar() and still cant read in reqOrSugg. Even if that would have worked I am still screwed because all the output comes out messed up and thats major deductions.

    How can I format the output by line w/o messing up getchar()? Besides I thought that this
    Code:
    	while (getchar() != '\n');
    was supposed to make the getchar() ignore new lines?
    All that loop will do is ignore excess characters up to (and including) the first newline. If you have input consisting of a hundred q's followed by enter and then an 'e', then the loop above will discard everything in the input stream and stop at the 'e' which will be the next character read. You can put another copy of that while loop just after the scanf call to eat up the trailing newline that is causing your problems.


    Code:
    printf ("%c%s",reqOrSugg,"\n");
    printf ("%c%s",oldOrNew,"\n");
    How about:
    Code:
    printf ("%c\n",reqOrSugg);
    printf ("%c\n",oldOrNew);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    I will do you one better. Take this whole section of code out...

    Code:
    printf ("%c%s",reqOrSugg,"\n");
    printf ("%c%s",oldOrNew,"\n");
    Still stores the value of reqOrSugg as nothing.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    Thank You

    I understand what the while loops purpose is now. Changed code to this and all is well!

    Code:
    while (getchar() != '\n');
    oldOrNew = getchar();

    Code:
    while (getchar() != '\n');
    oldOrNew = getchar();
    Thanks for your patience as I am super new to this!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursion Problem I think
    By clearrtc in forum C Programming
    Replies: 4
    Last Post: 07-31-2006, 07:10 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. problem with parser code
    By ssharish2005 in forum C Programming
    Replies: 2
    Last Post: 12-02-2005, 07:38 AM
  4. Replies: 5
    Last Post: 11-07-2005, 11:34 PM