hello all,
Im having some problems doing this simple program.
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);
}
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.
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...
Code:
while (strcmp(casting, "@*!#&")==1);
{
cout << "ERROR...only numbers\n";
}
...but maybe is not what I need to use (it doesnt work anyway).
Is there any simple way to do it? thanks in advance.