IsNumeric equivalent in C++

1
2
3
4
		//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.
use stringstream;small example here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
  string mystr;
  float price=0;
  int quantity=0;

  cout << "Enter price: ";
  getline (cin,mystr);
  stringstream(mystr) >> price;
  cout << "Enter quantity: ";
  getline (cin,mystr);
  stringstream(mystr) >> quantity;
  cout << "Total price: " << price*quantity << endl;
  return 0;
}
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include "conio.h"
#include <string>
#include <sstream>
using namespace 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"); 
			}

		else if(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.
The stringstream method you used, isn't different at all from using cin. Here you're just copying one stream to another.

Use something like this instead:
1
2
3
4
5
6
int a;
do
{
  cin.clear();
  cin >> a;
} while (cin.fail());


Or maybe:
1
2
3
4
5
int a;
while (cin.peek() < '0' || cin.peek() > '9')
{
  cin >> a;
}
Last edited on
I refer to line 41 - 49 specifically.

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.
Topic archived. No new replies allowed.