#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Need Help with looping in C Language


    I'm new to programming.

    If user selects "4", the following code will be executed (shown below).
    When user enters a value for myNumber1 (non zero) and then enters a value for myNumber2 (this value being zero), it displays:

    Please enter two numbers to divide (separate by space): 4 0
    Invalid - Cannot divide by zero!
    Would you like to continue (y/n)?
    <--I do not want this to display yet until two numeric values are entered correctly

    If the division is undefined (e.g. 4/0), it should only display "Invalid - Cannot divide by zero!" and loop back to display the line where it ask to enter two numbers to divide. This is what I want, shown below):

    Please enter two numbers to divide (separate by space): 4 0
    Invalid - Cannot divide by zero!
    Please enter two numbers to divide (separate by space): 4 4

    Result of dividing 4.00 and 4.00 is 1.00
    Would you like to continue (y/n)?



    How can I fix the code so that it does what I want?

    Providing code (of course, I am using the switch statement but the problem lies in case 4):

    Code:
    case 4: 
    printf("\nPlease enter two numbers to divide (separate by space): "); 
    while ((scanf("%f %f", &myNumber1, &myNumber2)) != 2) 
             { 
             while (getchar() != '\n') 
             continue; 
             printf("\nError reading your input. Please try again!"); 
             printf("\nPlease enter two numbers to divide (separate by space): "); 
             } 
    if (myNumber2 == 0) 
             printf("Invalid - Cannot divide by zero"); 
    else 
             printf("\nResult of dividing %.2f and %.2f is %.2f", myNumber1, myNumber2, myNumber1/myNumber2); 
    printf("\nWould you like to continue (y/n)? "); 
    scanf("%c", &resp); 
    break;
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,161
    Rep Power
    2222
    This:
    scanf("%c", &resp);
    tells the computer to read in the next character from the input buffer. Since the next character is already in the buffer, the newline char from the Enter key for the previous scanf, it doesn't wait for you to enter the response. Rather, you want to tell scanf to ignore all white space (eg, blanks, tabs, newlines) before reading in the character that you will enter. To do that, you need to add a space before the "%c" thus:
    scanf(" %c", &resp);
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,110
    Rep Power
    1803
    More consistent formatting might make it easier for you to fathom:

    Code:
    case 4:
    {
        printf("\nPlease enter two numbers to divide (separate by space): "); 
        do
        {
            while ((scanf("%f %f", &myNumber1, &myNumber2)) != 2) 
            { 
                while (getchar() != '\n') 
                    continue; 
                printf("\nError reading your input. Please try again!"); 
                printf("\nPlease enter two numbers to divide (separate by space): "); 
            } 
    
            if( myNumber2 == 0 ) 
            {
                printf("Invalid - Cannot divide by zero"); 
            }
    
        } while( myNumber2 == 0 ) ;
    
        printf("\nResult of dividing %.2f and %.2f is %.2f", myNumber1, myNumber2, myNumber1/myNumber2); 
        printf("\nWould you like to continue (y/n)? "); 
        scanf("%c", &resp); 
    }
    break;
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep Power
    0

    Thanks, it's fixed now


    Thank you Clifford!!! it worked!!

IMN logo majestic logo threadwatch logo seochat tools logo