Calculator works.. but not with a loop

I have a do while loop in "setCalc". The code runs, but the program freezes and doesn't work after I ask for z (answer). It works perfectly fine without any loop, but doesn't not run when there is a loop. Can someone tell me why please?

It doesn't work with any loop. I've tried multiple ways of configuring it and still no success.




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
#include <iostream>
#include <windows.h>

using namespace std;
class crab
{
    char input;


    public:
      crab(){

            cin >> x;
           cin >> input;

            cin >> y;

    }

    void setCalc (){


            int j = 3;


        do {

             switch (input){
            case 'x':
            case 'X':
            case '*':
            z = x * y;
            break;

            case '+':
            z = x+y;
            break;

            case '-':
            z = x-y;
            break;

            case'/':
            z = x/y;
            break;
            default:
            cout<< "no worky."<<endl;
           system("cls");
             } } while (j == 3);







   };

    int getCalc (){
           return z;
   }

    private:
           int x,y,z;



};



int main()
{
    crab ob;
    ob.setCalc ();
    cout << ob.getCalc();

    return 0;
}
Last edited on
j is always 3.
Infinite loop.change j value.
1
2
3
4
5
int j = 3;

do {

}while(j==3);


you never change j... so the loop will run forever...
I want the loop to run forever. it depends on the users execution anyways. the code just doesn't work when theres a loop and I don't understand why.
Last edited on
there is no input...
"input" never changes and will run forever with no data

maybe add crab(); at the beginning of your loop...
Topic archived. No new replies allowed.