Interesting indescribable bug

Hey everybody. I made a calculator program today, and it works pretty well; but when you do something unexpected(like entering x, y, operator as opposed to x, operator, y) the program loops pretty much everything infinitely. Can anybody explain why this bug exists, and possibly point me in a direction of fixing it? Thanks in advance all.
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
#include<iostream> 
#include<stdlib.h>
#include<string>
using namespace std;

int main()
{
    float x;//First number.
    float y;//Second number.
    string op;//The operator for each problem.
    int choice;//Used in a switch statement.
    bool quit=false;
    cout << "Valid Operators:\n";
    cout << "+ : addition\n";
    cout << "- : subtraction\n";
    cout << "* : multiplication\n";
    cout << "/ : division\n";
    cout << "\n";
    do{ 
    cout << "Select an Option:\n";
    cout << "1) Do problem\n";
    cout << "2) Quit\n";
    cout << "Selection: ";
    cin >> choice;
    cout << "\n";
    switch(choice)
        {
        case 1:
                cout << "First Number: ";
                cin >> x;
                cout << "Operator :";
                cin >> op;
                cout << "Second Number: ";
                cin >> y;
                cout << "\n";
                if (op == "+")
                {
                  cout << x << "+" << y << "=" << x + y << "\n";
                }
                else if (op == "-")
                {
                  cout << x << "-" << y << "=" << x - y << "\n";
                }
                else if (op == "*")
                {
                  cout << x << "*" << y << "=" << x * y << "\n";
                }
                else if (op == "/")
                {
                  cout << x << "/" << y << "=" << x / y << "\n";
                }
                else 
                {
                  cout << "Invalid operator\n";
                }
                cout << "\n";
                break;
                
        case 2:
                quit=true;
                break;
        }
    }
    while (quit==false);
   system("PAUSE");
   return 0;
   }    
I can't explain it very well, but I am pretty sure it's because of the >> operator you used to get the input stream. Try replacing all occasions of cin>>variable with getline(cin,variable), and it will probably work. At least that works for me. It has something to do with characters remaining in the input stream if you access it with cin>>...

Ah, and, unless you are planning to add additional options, don't you think that using the switch statement is kinda pointless here?
Last edited on
I get errors and the program won't compile when I use getline with a number. Am I doing it wrong?
1
2
3
4
5
6
7
cout << "First Number: ";
getline(cin,x);
cout << "Operator :";
getline(cin,op);
cout << "Second Number: ";
getline(cin,y);
cout << "\n";
Last edited on
closed account (jwC5fSEw)
getline() is a string function, and you're trying to use it on a float.

I actually don't think you should be using strings for this. You should just use a char.
Ah no, I think getline will only accept strings as variable parameter. Try defining a string variable (for example, string input) which you give to getline as a parameter. Afterwards you can convert it into an integer with atoi (which requires a conversion into a C string first, example: int x = atoi(input.c_str());) or into a float with atof. If you get an error when defining a string variable, include string. (#include <string>)

To shadow addict: Yeah, that's possible. But if you want to use the same operation for numbers and operators, you will have to use strings cause numbers can have multiple digits, and can also have a + or - operator in front of them.
Last edited on
Ok, so, I changed string op to char op, and I get errors saying ISO C++ forbids it. I think my computer hates me. lol
Wait... You can't define char variables, or you can't use them with any of the functions? I don't understand the problem...

You know what? Add cin.ignore(100,'\n') after every cin>>, and you should be able to use the initial program without any further modifications. (This pretty much does what it looks like, it will remove the next 100 characters from the input stream, or alternatively all characters until the next newline character. In your case, cin.ignore() will probably be already be enough (1 and EOF are the default values))
Last edited on
Topic archived. No new replies allowed.