ok so eventually im going to be making a postfix calculator with a stack implementation and getting inputs such as 45 20+. I'm just having trouble with getting the input correct, I want to ignore whitespace, if the user enter a number, i want to cin a double, if the user enter an operator, such as +, i want to input a char. What i have so far is as follows:
1 #include"dstack.h"
2
3 #include<iostream>
4 #include<stdlib.h>
5 using namespace std;
6
7 void error();
8
9 int main()
10 {
11 char value = cin.peek();
12 char op;
13 double num;
14
15 while(!cin.eof())
16 {
17 if( (isdigit(value)) ) //|| value == '.') )
18 {
19 cin >> num;
20 cout << "Your number is: " << num << endl;
21 }
22
23 else if( (isspace(value)) )
24 {
25 cin.ignore();
26 }
27
28 else if ( (value = '+') )
29 {
30 cin >> op;
31 cout << "You entered a char: " << op << endl;
32 }
33
34 else if ( (isalpha(value)) )
35 {
36 error();
37 }
38 }
39 cout << "No more input" << endl;
40 }
41
42 void error()
43 {
44 cerr << "error" << endl;
45 exit(1);
46 }
Since im just testing, im not putting anything on a stack or anything of that sort yet, and theres more operators, im just testing '+' to get it working. Whats happening is if I enter a number, it works just fine, but say i enter 30, press enter, then input +, then enter, it loops my cout for number statement a million times. Is my program getting stuck in each if statement? Any help would be appreciated, and if any clarification or other information is needed, just ask. There needs to be spaces between numbers, white space needs to be ignored, and there doesnt need to be spaces between operators. Eventually, whats supposed to happen is the user inputs something such as 30 30 30++, and it evaluates that equation.