If something is changed do this..

Hey i'm looking for a way to say something like: if this is changed, do this

Basically this is where i need it:

1
2
3
4
5
6
7
8
9
10
if (theFunction == "keyPress")
        {
            cout << "(add keyPress) "; 
            cin >> keyPress; 
        }
        else
        {
            cout << "(add input) "; 
            cin >> input; 
        }


Not sure if this code helped in any way. But i have tried to add "theFunction = keyPress" under "cin >> input", as that is when i need it to change back to being keyPress. So when writing the input and then pressing enter, then it needs to change.
If i can add anything else to help just tell me.
Thanks.
Could you add what are the "states" to which you are referring and what are their values. Are they booleans?
What is "theFunction"?
Yeah my names of things are never any good.
Here:
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

#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

char keyPress;
long double screenValue = 0.0;
long double input = 0.0;
string theFunction = "keyPress";

int main ()
{
    cout << "Functions"
        << "\n (c)      Clear screen"
        << "\n (+)      Add"
        << "\n (-)      Subtract"
        << "\n (*)      Multiply"
        << "\n (/)      Divide"
        << "\n (%)      Modulus"
        << "\n (cos)    Cos"
        << "\n (sin)    Sin"
        << "\n (tan)    Tan"
        << "\n (sr)     Square root"
        << "\n (po)     Power of"
        << "\n (rr)     Remove remainder"
        << "\n (q)      Quit";

    cout << "\n\n [ ";
    cout.width(20);

    while (keyPress != 'q')
    {
        cout << screenValue << " ] \n";
        cout << theFunction << " ";

        if (theFunction == "keyPress")
        {
            cout << "(add keyPress) ";
            cin >> keyPress;
        }
        else
        {
            cout << "(add input) ";
            cin >> input;
        }

        switch (keyPress)
        {
            case 'c':
            screenValue = 0.0;
            break;
            case '+':
            theFunction = "Add";
            screenValue += input;
            break;
            case '-':
            theFunction = "Subtract";
            screenValue -= input;
            break;
        }

    }
    return 0;
}


That is my entire code right now. Basically its gonna be a simple calculator
The reason that changing the value of theFunction didn't work is that the switch statement occurs without ever asking for input.

One way to get it working, split that switch statement into two parts. The first part changes the value of theFunction, and is placed within the if block. The second part changes screenValue, and is placed within the else block. After the second switch statement, change the value of theFunction back to keyPress:
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
while (keyPress != 'q')
{
  cout << screenValue << " ] \n";
  cout << theFunction << " ";

  if (theFunction == "keyPress")
  {
    cout << "(add keyPress) ";
    cin >> keyPress;
    switch (keyPress)
    {
      case 'c': screenValue = 0.0;        break;
      case '+': theFunction = "Add";      break;
      case '-': theFunction = "Subtract"; break;
    }
  }
  else
  {
    cout << "(add input) ";
    cin >> input;
    switch (keyPress)
    {
      case '+': screenValue += input; break;
      case '-': screenValue -= input; break;
    }
    theFunction = "keyPress";
  }
}


And please, for the love of god, don't divide by zero or get the square root of a negative! :)

Last edited on
Did LowestOne answer your question?
Yeah thanks guys, that fixed it!
Topic archived. No new replies allowed.