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
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