sorry for my english is poor
i make simple calculator, and want to validate it, it is good but not meet my expectation. so the problem is
1. about first number validation, i convert it to string, but user still can input 22xxx where xxx was discarded, my wish is to make user only can input double.
2. about option, i try validate only accept +,-,*,/ it success but problem is user can input -+ and if wrong input like e,r it show message twice, i wish to make user can only input 1 char so if error only one message show.
3. about second number validation same as first number problem but i was wondering why it show message twice, so i want to know how to prevent double messsage.
4. when user continue there is message error on displaying on first number. i don't know what make this error happening, can anyone show me why error message happens and how to prevent it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
int main(){
double a1, a2, j;
int i;
string input, input2;
char opsi;
cout <<"simple calculator \n";
do
{
cout <<"insert first number \n";
getline(cin, input);
stringstream myStream(input);
if (myStream >> a1)
break;
cout << "Invalid number, please try again" << endl;
} while (true);
do{
cout <<"chose your option +,-,*,/ \n";
cin >> opsi;
if ((opsi == '+') || (opsi == '-') || (opsi == '*') || (opsi == '/'))
break;
else
cout <<"your option wrong " <<"\n";
} while (opsi != '+' || '-' || '*' || '/');
do
{
cout <<"insert second number \n";
getline(cin, input2);
stringstream myStream(input2);
if (myStream >> a2)
break;
} while (true);
if (opsi == '+') {
j = a1 + a2;
cout << "answer: " << j <<".\n";
}
else if (opsi == '-') {
j = a1 - a2;
cout << "answer: " << j <<".\n";
}
if (opsi == '*') {
j = a1 * a2;
cout << "answer: " << j <<".\n";
}
if (opsi == '/') {
j = a1 / a2;
cout << "answer: " << j <<".\n";
}
while (i != 0 or i != 1){
cout <<"enter 1 for continue or 0 to quit";
cin >> i;
if (i == 1){
main();
}
else if (i == 0){
return 0;
}
else
cout << "wrong input ";
}
}
|
thanks