Checking User Input

I'm trying to create a simple calculator, and I want the user's input to be of the form: one or more digits (0-9) optionally followed by a decimal point and one or more digits. So 5, 5.1, and 0.51 are numbers, but abc, -5, .51, and 51. are not.

The trouble is that I don't really know how to check this input. My experience with Python tells me I would first cin the input into a string and then convert it into a double, but I don't know how that works in C++.

Could someone help me out?
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
#include <iostream>
#include <climits>
using namespace std;

int main () {

long double total = 0, number, temp = total;
char operat = ' ', answer;

cout << "= " << total << endl;
     do {
           cout << "enter number ";
           cin >> number;
           if (number > LDBL_MAX || number < LDBL_MIN) {  //HERE YOU SET WHAT WILL BE ACCEPTED
			   cout << "wrong number entered! " << endl;
			   number = 0;
			   continue;
		   }
		   if (total == 0) total = number;

			switch (operat) {
			case ' ' : break;
			case '+' : {
				temp = total;
				total+= number;
				cout << temp  << " " << operat << " " << number << " = " << total << endl;
				break;
						}
			case '-' : {
				temp = total;
				total -= number;
				cout << temp << " " << operat << " " << number << " = " << total << endl;
				break;
						}
			case '/' : {
				if (number == 0) {
					cout << "LOL can't divide with 0, ashol!!" << endl;
					continue;
				}
				temp = total;
				total /= number;
				cout << temp << " " << operat << " " << number << " = " << total << endl;
				break;
						}
			case '*' : {
				temp = total;
				total *= number;
				cout << temp << " " << operat << " " << number << " = " << total << endl;
				break;
						}
			case 'e' : {
				temp = total;
				for (short i = 1; i < number; i++)
						total *= total;
				if (total > LDBL_MAX || total < DLBL_MIN) {
					total = 0;
					operat = ' ';
					cout << "total to large, set to 0 " << endl;
					continue;
				}
				cout << temp << "E " << number << " = " << total << endl;
				break;
						}
			case '=' : {
				cout << "= " << total << endl;
				cout << "calculae again? y/n ";
				cin >> answer;
				if (answer == 'y') {
					total = 0;
					operat = ' ';
					continue;
				}
						}
			default: {
				cout << " wrong operator entered!! ";
				total = 0;
				break;
					}
			}
           cout << "enter operator ";
           cin >> operat;
		   if (operat == '=') {
			   	cout << "= " << total << endl;
				cout << "calculae again? y/n ";
				cin >> answer;
				if (answer == 'y') {
					total = 0;
					operat = ' ';
					continue;
				}
			}
		}
	 while (operat != '=');

return 0;
}
Last edited on
Topic archived. No new replies allowed.