Hello,
I am trying to parse a users input correctly and for the most part I have done so. However, the only issue which still remains is that when a user enters an input of "1.9 3.4 3.h", the '3.h' is automatically converted to 3.0 which I don't want. I would like to prompt the user to let them know that the input was invalid. Here is my code for the error check so far:
Code:while(1) { /*Prompt user for input and process the information*/ printf("Enter variables for quadratic equation (Enter 'q' or 'quit' to exit): "); fgets(line, 30, stdin); if(strncmp(line, "quit", 4) == 0 || strncmp(line, "q", 1) == 0) return 0; else dataScanned = sscanf(line, "%lf %lf %lf", &a, &b, &c); /*The data read in was invalid*/ if(dataScanned != 3) printf("Invalid Input: Format Is <double> <double> <double>\n\n"); /*Send the data off to qsolver for processing*/ else if(dataScanned == 3) { qret = qsolver(a, b, c, &x1, &x2); printf("Error Value: %d\n", qret); printf("X1 = %f X2 = %f\n\n", x1, x2); } }



LinkBack URL
About LinkBacks



