Calculator program mode input problem.

Why won't this variable work as a mode control in a calculator program. The result is the console asking for a mode input indefinitely.

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
  #include <iostream>

using namespace std;

int mult ( int x, int y );
int divide (int x, int y );

int main()
{
    for (;;) {
    int x;
    int y;
    char mode;
    char m;
    char d;

    cout<<"Please input mode (m for multiply, d for divide): ";
    cin>> mode;
    cin.ignore();
    if ( mode == m ) {
    cout<<"Please input two numbers to be multiplied: ";
    cin>> x >> y;
    cin.ignore();
    cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
    }
    else if ( mode == d ) {
        cout<<"Please input two numbers to be divided: ";
        cin>> x >> y;
        cin.ignore();
        cout<<"The quotient of your two numbers is "<< divide ( x, y ) <<"\n";
    }
    }
    cin.get();
}

int mult ( int x, int y )
{
    return x * y;
}

int divide ( int x, int y )
{
    return x / y;
}
if ( mode == m ) {What is contained in m variable? Where do you assign it?
Same for d.
I was trying to make it so that when the user types m or d into the console after asked to input a mode it would set the char mode variable to whatever they had input, then if mode was equal to m it would trigger the multiply code, if it were d it would trigger the divide code, and I have no idea what would happen if it were anything other than those two things
Last edited on
1
2
char mode; //Declares variable named mode
char m; //Declares variable named m 


If you want a character, use character literal: 'x' — character x
Ah, that would explain it, I tried using "m" but that threw up some errors, I'll try 'm' and see if it works.

Success, thanks, had no idea that you needed to do anything special when using literal characters instead of variable named after a character.
Last edited on
Topic archived. No new replies allowed.