0

My pseudocode:

Best case: 1) Ask user for a number 2) Test if user's input is a numerical value 3) If user's input is valid, perform floor, round, and ceil functions on user's input 4) Display floor, round, and ceil values of user's input

Worst case: 1) Ask user for a number 2) Test if user's input is a numerical value 3) If user's input is invalid, ask if the user wants to try again with a Y/N choice 4) If user says yes, go back to 1), if user says no - terminate

#include <stdio.h>
#include <math.h>

int main(void) {

    float user_input;
    int a;
    char answ;

    do {
        printf ("Enter a number to find its lower bound, rounded, and upper bound values: ");

        if ((a = scanf("%f", &user_input) == 1)) {

            float floored_input = floor(user_input);
            float rounded_input = round(user_input);
            float ceiled_input = ceil(user_input);

            printf("Lower Bound: %1.0f\n", floored_input);
            printf("Rounded:     %1.0f\n", rounded_input);
            printf("Upper Bound: %1.0f\n", ceiled_input);

            break;

        } else {

            printf("Invalid input. Do you want to try again? (Y/N): ");
            scanf("%c", &answ);

        }
    } while(answ == 'Y' || answ == 'y');

    return 0;
}

The only problem I am having is that when a user enters a value literally other than 'i' the program terminates and i'm back in my working directory.

The user can enter any number and it will work. The user can enter i and he or she will be prompted with a Y/N option

HOWEVER, when the user enters "i2", "hello", "h", "b", "yoooo", etc

The user does not get a Y/N option and the program is terminated

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Tommy Son
  • 17
  • 3
  • 3
    Add a space to `scanf(" %c", &answ);` to filter leading whitespace. Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Note too that `char answ` is an uninitialised variable, for loops that do not enter a value. It should be defined as `char answ = 'y';` – Weather Vane May 28 '19 at 18:09
  • 1
    You need more brackets in `(a = scanf("%f", &user_input) == 1)`. `==` has higher precedence than `=`. – Rup May 28 '19 at 18:10
  • Still doesn't work. Y/N prompt comes up only when the user enters the letter 'i' – Tommy Son May 28 '19 at 18:12
  • 1
    Also, if `scanf("%f", &user_input)` does not return `1` then there could be something stuck in the buffer, which will be picked up by the `scanf(" %c", &answ);` – Weather Vane May 28 '19 at 18:14
  • I tried adding more brackets: if (a = (scanf("%f", &user_input) == 1)) and it still yields the same results – Tommy Son May 28 '19 at 18:16
  • 1
    The variable `a` is not being used, and can be removed. BTW, you haven't yet put the brackets in the correct place, so removing `a` is your best option. That won't solve the problem, of course. To solve the problem (when the user inputs an invalid number), the code needs to read till the newline is found, or the code should read a line with `fgets` and parse it with `sscanf` – user3386109 May 28 '19 at 18:17
  • removing the break still does not fix the problem – Tommy Son May 28 '19 at 18:21
  • i took out the variable a and also in my if function: if (scanf("%f", &user_input) == 1) and it's still doing the same thing. Taking out 'code'a'code' is nice though – Tommy Son May 28 '19 at 18:23
  • 2
    When you enter "i2" the `scanf(" %c", answ);` only removes the first character of the bad input. I suggest you stop struggling with `scanf` and get the input with `fgets` and apply `sscanf` to the string. It is so much easier to dump the string and get another input than to mess around clearing the input buffer. But don't mix `fgets` with `scanf`, use `fgets` for all user responses. – Weather Vane May 28 '19 at 18:25
  • [Here's an example](https://stackoverflow.com/a/5285835/3386109) of using `fgets` with `sscanf` from a somewhat related question. – user3386109 May 28 '19 at 18:30
  • @TommySon Does [This Answer Helps](https://stackoverflow.com/questions/32383918/how-to-make-reiterations-using-yes-no-prompt/32385554#32385554) ? – Michi May 28 '19 at 18:48
  • I added: fgets(input, INPUT_SIZE, stdin); if ((sscanf(input, "%f", &user_input)) == 1) { .....} and now i will be prompted for any invalid input thank you. BUT if i were to type in an invalid input, and choose to enter a number again when prompted the following line is displayed: "Enter a number to find its lower bound, rounded, and upper bound values: Invalid input. Do you want to try again? (Y/N): " and when i enter y or Y the same line gets repeated – Tommy Son May 28 '19 at 18:50
  • @TommySon How about something [Like This](https://ideone.com/qGCfuJ) ? – Michi May 28 '19 at 19:01
  • @Michi sorry it's a bit confusing for me – Tommy Son May 28 '19 at 19:20
  • OT: the function: `floor()`, `round()`, and `ceil()` return `double` not `float` Suggest 1) read the MAN page for functions that you are using and 2) suggest: `floorf()` and `roundf()` and `ceilf()` – user3629249 May 28 '19 at 23:11

1 Answers1

0

the following proposed code:

  1. cleanly compiles
  2. performs the desired functionality
  3. makes use of the header file: ctype.h function: toupper()
  4. uses the proper functions for float values
  5. properly initializes the variable: answ

and now, the proposed code:

#include <stdio.h>
#include <math.h>
#include <ctype.h>

int main(void) 
{
    float user_input;
    char answ = 'Y';

    while( toupper( answ ) != 'N' ) 
    {
        printf ("Enter a number to find its lower bound, rounded, and upper bound values: ");

        if ( scanf("%f", &user_input) == 1 ) 
        {
            float floored_input = floorf( user_input );
            float rounded_input = roundf( user_input );
            float ceiled_input  = ceilf(  user_input );

            printf( "Lower Bound: %1.0f\n", floored_input );
            printf( "Rounded:     %1.0f\n", rounded_input );
            printf( "Upper Bound: %1.0f\n", ceiled_input );
        } 

        do
        {
            printf( "Do you want to try again? {Y/N}: " );
            scanf( " %c", &answ );
            //printf( "debug: answ = %x\n", answ );
        } while( toupper(answ) != 'Y' && toupper( answ ) != 'N' );
    }
    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17