Hi folks,
Every time a user inputs anything other than a number in my programs it goes into a mad scroll…..is there a way to stop this….so that they would get an error message if the input was not a number
Thanks fer any help…..
Hi folks,
Every time a user inputs anything other than a number in my programs it goes into a mad scroll…..is there a way to stop this….so that they would get an error message if the input was not a number
Thanks fer any help…..
>>> is there a way to stop this
Yes.
What do you mean, not helpful! My answer gives as much information as your question!!!
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
Well, let me just say that I ALWAYS grab input as strings! That is just my preference, and I have no problem converting numerical strings into ints, floats, etc.
That being said, you could adopt this approach and go further with the validation process.
For instance, you could write a function which validates that a string is in fact all numbers:
Code:int validate(char *input) { int len = strlen(input); int i; for( i = 0; i < len; i++) { if(!isdigit(input[i])) return 0; } return 1; }
Then in main you could do:
Hope that helps!Code:int main() { char input[100]; int num; do{ clrscr(); printf("Enter A Valid Number: \n\n"); fgets(input, 100, stdin); if( !validate(input) ) continue; break; }while(1); num = atoi(input); /// rest of program...
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Thx for the help Sebastiani.............