hello all,
Im having some problems doing this simple program.
the reason Im using atof is because the program crashed if I typed anything else other than a number. Now it doesnt crash it just returns 0.Code:#include <iostream> using namespace std; void input(); float result,firstNum, secondNum; int main(){ input(); result = firstNum + secondNum; cout << "\n\t\t\t\t" << result << endl; return 0; } void input(){ char casting[255]; cout << "\n\t\t\tEnter First Number: "; cin >> casting; firstNum = atof(casting); cout << "\n\t\t\tEnter Second Number: "; cin >> casting ; secondNum = atof(casting); }
What I'd like is actually validate the entry and return an error message if the entry is not a number.
I've tried using something like...
...but maybe is not what I need to use (it doesnt work anyway).Code:while (strcmp(casting, "@*!#&")==1); { cout << "ERROR...only numbers\n"; }
Is there any simple way to do it? thanks in advance.



LinkBack URL
About LinkBacks



