Originally Posted by
NewbGuy
Ok for the most part, I have solved the problem of the user entering longer numbers than i wanted, but I still have a problem with the user entering the wrong type of data.
If I ask for a decimal value and the user enters a character or a string, the program crashes, and vice versa.
The solution to this could be to use a loop for all the input, which collects a string into a temporary buffer (since a string can hold anything), and then you work on the string
Code:
#include <stdio.h>
int main() {
char input[64], one;
int i, two;
for (i=1;i<=4;i++) {
switch (i) {
case (1): printf("Enter a character: "); break;
case (2): printf("Now a number: "); break;
...etc
fgets(input,64,stdin);
switch (i) {
case (1): one=input[0]; break;
case (2): sscanf(input, "%3d", &two); break;
...etc
With the second switch, you may want to implement error checking routines that will issue a message and set i-- if the user enters bad input.