Thread: scanf doesn't take input before proceeding to next line

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    scanf doesn't take input before proceeding to next line

    This program is supposed to take a value then print the rounded value of that number to the nearest integer, tenth, hundredth, and thousandth. Afterwards it is supposed to ask you if you want to round another number at which point you say yes or no. I know alot of the parts in this program aren't fully working yet but what is really confusing me is the do while structure.

    When it goes to say "Would you like to enter another value?" it doesn't wait for my response from scanf before going to say "I'm sorry you entered an incorrect value please enter y or no." After I enter a value it keeps on looping the same message. Everything seems to be there what am I missing?

    Sorry for writing such a long question.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void roundNumbers( void );
    void roundToInteger ( float );
    void roundToTenths( float );
    void roundToHundreths( float );
    void roundToThousandths( float );
    
    int main()
    {
    int loopcontrol;
    
    
       printf("n is %d and y is %d\n", 'n', 'y');
    /* -------------------------------------This is where I seem to be having the problem ---------------------------------------- */
    do {
       roundNumbers();
       
       printf("Would you like to round another number(press y or n)? ");
       scanf("%d", &loopcontrol);
    
       while( loopcontrol != 'y' || loopcontrol != 'n' )
          {
          printf("I'm sorry you entered an incorrect value please enter y or n");
          scanf("%d", &loopcontrol);
          }
    
    }  while(loopcontrol == 121);
             
    
    return 0;
    }
    
    
    
    void roundNumbers( void )
    {
       float x = 0;
    
       printf("Enter value to be rounded: ");
       scanf("%f", &x);
    
       roundToInteger( x );
       roundToTenths( x );
       roundToHundreths( x );
       roundToThousandths( x );
    
       return;
    }
    
    
    
    void roundToInteger( float number )
    {
       float y, x;
    
       x = number;
       y = floor(x * .5);
    
       printf("%.2f rounded to the nearest integer is %.2f\n", x, y);
    
       return;
    }
    
    
    void roundToTenths( float number )
    {
       float y, x;
    
       x = number;
       y = floor(x * 10 + .5);
    
       printf("%.2f rounded to the nearest tenth is %.2f\n", x, y);
    
       return;
    }
    
    
    void roundToHundreths( float number )
    {
       float y, x;
    
       x = number;
       y = floor(x * 100 + .5);
    
       printf("%.2f rounded to the nearest hundredth is %.2f\n", x, y);
    
       return;
    }
    
    
    void roundToThousandths( float number )
    {
       float y, x;
    
       x = number;
       y = floor(x * 1000 + .5);
    
       printf("%.2f rounded to the nearest thousandth is %.2f\n", x, y);
    
       return;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
       scanf("%d", &loopcontrol);
    
       while( loopcontrol != 'y' || loopcontrol != 'n' )
    "%d" trys to read an int, your input is a char ( 'y' or 'n' ) so scanf doesn't update your int loopcontrol.
    you have to use "%c" as format and scan a char variable.
    Kurt

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    try that? although it was done in a few seconds... i think it makes more sense than your code?

    Code:
    #include <stdio.h>
    #include <math.h>
    
    void roundNumbers( void );
    
    int main()
    {
        char loopcontrol;
    
    
       roundNumbers();
       fflush(stdin);
       printf("Would you like to round another number(press y or n)? ");
       scanf("%s", &loopcontrol);
       
    while(loopcontrol != 'n') {
       fflush(stdin);
    
       while( loopcontrol != 'y' && loopcontrol != 'n' )
          {
          printf("I'm sorry you entered an incorrect value please enter y or n");
          scanf("%c", &loopcontrol);
          }
          
       roundNumbers(); 
       printf("Would you like to round another number(press y or n)? ");
       scanf("%s", &loopcontrol);
    
    }
             
    
    return 0;
    }
    
    
    
    void roundNumbers( void )
    {
       float x;
    
       printf("Enter value to be rounded: ");
       scanf("%f", &x);
       
       
       printf("%.2f rounded to the nearest integer is %.0f\n", x, round(x));
       printf("%.2f rounded to the nearest tenth is %.0f\n", x, round(x/10)*10);
       printf("%.2f rounded to the nearest 100 is %.0f\n", x, round(x/100)*100);
       printf("%.2f rounded to the nearest 1000 is %.0f\n", x, round(x/1000)*1000);
    
       return;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by mas0
    try that? although it was done in a few seconds... i think it makes more sense than your code?
    Not too much

    scanf("%s", &loopcontrol);
    wrong format

    Code:
     while( loopcontrol != 'y' && loopcontrol != 'n' )
          {
          printf("I'm sorry you entered an incorrect value please enter y or n");
          scanf("%c", &loopcontrol);
          }
          
       roundNumbers();
    If I enter 'n' here you still calculate numbers

    Code:
    fflush(stdin);
    Read the FAQ... And don't do this anymore
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    fflush(stdin);
    instead use this function

    Code:
    void clear_buffer(void)
    {
        int ch;
        
        while((ch=getchar()) != '\n' && ch != EOF);
    }
    ssharish2005

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    182
    Quote Originally Posted by ZuK
    Code:
       scanf("%d", &loopcontrol);
    
       while( loopcontrol != 'y' || loopcontrol != 'n' )
    "%d" trys to read an int, your input is a char ( 'y' or 'n' ) so scanf doesn't update your int loopcontrol.
    you have to use "%c" as format and scan a char variable.
    Kurt
    I actually had it that way, I was just messing around with different data types trying to see if that would work. There is an ascii in one of those loops that i hadn't cleared up either.

    To Mas0
    I could write this program in a simpler fashion but I'm trying to learn about functions right now.

    What perplexes me is why it completely skips over that one scanf statement. The one where it asks "Would you like to round another number?" but goes on to the next one with no problem. Is scanf not good for character values or something?


    Judging by some of the statements made here it sounds like I should be clearing the buffer but that is a little over my head at this point.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    182
    I got the loop control working by replacing y or n with 1 or 0 so I guess the problem was with scanf taking in the value of characters. So I guess my question why couldn't scanf take my char values properly?

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    Quote Originally Posted by yougene
    I got the loop control working by replacing y or n with 1 or 0 so I guess the problem was with scanf taking in the value of characters. So I guess my question why couldn't scanf take my char values properly?

    nope it the flushing of the last character issue. twhen u enter (new line) a character you also hit enter. the enter is stored and when u use scanf to read into a new char that new char is interpreted to be[enter] so it skips it... thats why it totally skipped it. thats why i used fflush(stdin) BUT after reading the faq,... u should use something else. but this is so confusing because i was told in class to use that...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  3. Reading input with scanf
    By dat in forum C Programming
    Replies: 3
    Last Post: 05-29-2003, 03:54 PM
  4. Limiting scanf input
    By SMurf in forum C Programming
    Replies: 0
    Last Post: 11-21-2002, 06:39 AM
  5. Skipping the rest of the line while doing file input
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 09-30-2002, 09:25 AM