Thread: Beginner C problem

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    1

    Beginner C problem

    Code:
    int main() {
        double firstInputCheck = 0;
        double MoneyEachMonth;
        char firstInputNewLine = '?';
        
        while (true) {
            if (firstInputCheck == 0 || firstInputNewLine != '\n' ) {
                printf("Enter how much money you will be putting towards loans/retirement each month: ");
                firstInputCheck = scanf(" %lf", &MoneyEachMonth);
                scanf("%c", &firstInputNewLine);
            } else {
                break;
            }
        }
    }
    When I try to put in an incorrect input (like bob) it proceeds to print the printf statement 3 times instead of just once.
    Attached Images Attached Images Beginner C problem-c-question-jpg 

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Recheck the docs on scanf, your using the output wrong if memory serves me correctly

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    When data on a stream cannot be converted it stays in the stream buffer. When you type 'bob' scanf() will return 0 and "bob\n" will stay in the stdin's buffer... the latter scanf() will read 1 char leaving "ob\n" in the buffer... In the next iteration, "ob" will cause scanf() to fail and the latter scanf() will leave "b\n"... Third interation: scanf() will fail and the second scanf() will get it, leavind only '\n' on buffer.

    Probably is best to do something like this:
    Code:
    char buffer[1024];
    double value;
    int valid=0;
    
    while ( ! valid )
    {
      printf( "Enter value: " ); 
      fflush( stdout );  // to garantee stdout buffer flushing.
    
      if ( fgets( buffer, sizeof buffer, stdin ) )
        valid = sscanf( buffer, "%lf", &value ) == 1;
    }

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by flp1969 View Post
    When data on a stream cannot be converted it stays in the stream buffer. When you type 'bob' scanf() will return 0 and "bob\n" will stay in the stdin's buffer... the latter scanf() will read 1 char leaving "ob\n" in the buffer... In the next iteration, "ob" will cause scanf() to fail and the latter scanf() will leave "b\n"... Third interation: scanf() will fail and the second scanf() will get it, leavind only '\n' on buffer.

    Probably is best to do something like this:
    Code:
    char buffer[1024];
    double value;
    int valid=0;
    
    while ( ! valid )
    {
      printf( "Enter value: " ); 
      fflush( stdout );  // to garantee stdout buffer flushing.
    
      if ( fgets( buffer, sizeof buffer, stdin ) )
        valid = sscanf( buffer, "%lf", &value ) == 1;
    }
    Very good answer; I was not able to think of a simple answer based on the OP code; I did not think of the idea of starting from scratch to solve the issue.
    And, I would have used an function that the OP might not yet know how to write an user defined function.

    Tim S.
    "...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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner's Problem
    By Gigabitten in forum C Programming
    Replies: 8
    Last Post: 11-27-2011, 05:56 PM
  2. Beginner's problem with C
    By scheng924 in forum C Programming
    Replies: 1
    Last Post: 05-19-2009, 03:44 PM
  3. Beginner problem
    By strokebow in forum C++ Programming
    Replies: 9
    Last Post: 09-13-2008, 09:54 AM
  4. Beginner's problem.. please help
    By jstevanus in forum C++ Programming
    Replies: 10
    Last Post: 12-08-2004, 04:08 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM

Tags for this Thread