//Prompting user for numbers
cout << "Please input two numbers" << endl;
cin >> num1;
cin >> num2;
I am developing a simple C++ calculator for practice. Is there an IsNumeric (from VB) equivalent in C++ where I can prevent user from crashing the program by entering non-numerical inputs? Thank you in advance!
You could try if (cin.bad()) or if (cin.fail())
I'm not sure off the top of my head whether that would be sufficient error checking, but it's worth a shot.
#include <iostream>
#include "conio.h"
#include <string>
#include <sstream>
usingnamespace std;
double add(int x, int y);
double subtract(int x, int y);
int main(){
bool restartBoo = true;
while(restartBoo)
{
//Variables declaration
string userInput;
int num1;
int num2;
char type;
char restartChar;
bool userChar = true;
//Prompting the user on calculation type and storing numbers
cout << "What do you want to do, enter a for addition or s for subtraction?" << endl;
cin >> type;
//Validating calculation type
while(userChar)
{
if(type == 'a' || type == 's')
{userChar = false;}
else
{
userChar = true;
cout << "You have entered an invalid character, a or s in lower case only, please re-enter." << endl;
cin >> type;
}
}
//Prompting user for numbers
cout << "You have chosen " << type << endl;
cout << "Please input the first number" << endl;
getline (cin, userInput);
stringstream(userInput) >> num1;
cout << "Please input the second number" << endl;
getline (cin, userInput);
stringstream(userInput) >> num2;
//Code for different calculation cases
switch (type)
{
case'a':
cout << add(num1, num2) << endl;
break;
case's':
cout << subtract(num1, num2) << endl;
break;
default:
cout << "You have entered an invalid calculation type, type a or s only." << endl;
break;
}
//Prompting the user for restart
cout << "Do you want to start over? y/n" << endl;
cin >> restartChar;
//Code for restart cases
if(restartChar == 'y')
{
restartBoo = true;
system("cls");
}
elseif(restartChar == 'n')
{restartBoo = false;}
else
{
cout << "You have entered an invalid character!" << endl;
restartBoo = true;
}
}
_getch();
return 0;
}
double add(int x, int y){
return x + y;
}
double subtract(int x, int y){
return x - y;
}
This is the code for very first C++ addition/subtraction calculator. I tried stringstream as suggested but I did not get the desired result as the user gets prompted at the same time for inputting the first and second number. I do not know what went wrong, any help will be appreciated, thank you!
If it's simply that the user is getting prompted to re-enter the first and second number, then you should exclude the user getting prompted from the loop that tests for correct input from the addition/subtraction tree.
I got "Please input the first number" and "Please input the second number" displayed simultaneously but I want the former to appear first followed by the latter. I have no idea how to fix it.
I had tried the small example given by gelatine and it works well. I do not know why mine does not work as expected.