Smart Number Input:
C
Medium
2 views
Problem Description
Take number input from the user until they enter -999. Check each input to see if it is an integer. If a float is entered, give a warning and ignore the decimal part.
Official Solution
#include <stdio.h>
int main() {
double input;
int intValue;
while (1) {
printf("Enter a number (-999 to quit): ");
// Check if input is a valid number
if (scanf("%lf", &input) != 1) {
printf("Invalid input! Please enter a number.n");
// Clear input buffer
while (getchar() != 'n');
continue;
}
// Check for sentinel value
if (input == -999) {
printf("Program terminated.n");
break;
}
intValue = (int)input;
// Check if input was a float
if (input != intValue) {
printf("Warning: Float entered. Decimal part ignored.n");
}
printf("Processed integer value: %dn", intValue);
}
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!