Latest attempt at this loop

Okay i'm still working on my calculator and i'm at the last part and can't get my loop to work correctly. I need my program to receive the first number, display the operator then the operand, then allow user to input a second, third, forth, however many he wants until he quits the program by pressing q. Of course I was trying to constantly update my variable so that I could perform new operation on it. Example:
+ 5.0
result so far is 5.0
^2
result so far is 25.0
/ 2.0
result so far is 12.5
q 0
final result is 12.5

and here's the code I have so far:

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include <iostream>
#include <math.h> 


using namespace std;

//Prototypes

//Retrieves the operator, operand and returns operator
//and operand

void scan_data(char, float);

//Reads operator and operand and uses switch statement
//to perform correct operator on the operand

double do_nex_op(char, float, float&);

//Reads result and outputs the result of the operator
//and the operand

float do_next_op(float);

int main()
{
    char op;
    float num2;
    float sum;

    
  cout<< "This program is a very basic calculator \n";
  cout<< "use only operators +,-,*,/, or ^";
  cout<<" to raise a number to a power\n\n";

    
  //display operator and operand
    
    scan_data(op, num2);

    
return 0;    

}

// function to obtain original values

void scan_data(char op, float num2)
{
 
   float num;
   float result;
  
      num = do_nex_op ( op, num2, num);
  
      result = do_next_op(num);

}

// performs operation based on operator input

double do_nex_op(char op, float num2, float& num)
{
   num = 0;
  
  cout<< "Enter a valid operator:\n";
  cin >> op;
  
  cout<< "Enter any number:\n";
  cin >> num2;
       switch (op)
        {
            case'+':
                cout << "\n+"<< num;
                num = num + num2;
                cout<<"\n\n result so far " << num;
                break;
            case '-':
                cout << "\n-"<< num2;
                num = num - num2;
                cout <<"\n\n result so far " << num;
                break;
            case '*':
                cout << "\n*"<< num2;
                num = num * num2;
                cout <<"\n\n result so far "<< num;
                break;
            case '/':
                cout << "\n/"<< num2;
                num = num / num2;
                cout <<"\n\n result so far "<< num;
                break;
            case '^':
                cout << "\n^"<< num2;
                num = pow(num, num2);
                cout <<"\n\n result so far "<< num;
                break;
            case 'q':
                cout <<"\n q" <<" 0";
                cout << "\n\n final result is"<< num;
            default:
                cout<<"\n\n Invalid operator";
                
          }
    
     
   
      
  return (num);
}
// suppose to initiate my loop but not working!!!
float do_next_op(float num)
{
    float num2;
    char op;
    while(!(num2/0))
        cout<<"Enter Next Operation:\n";
        cin>> op;
        cout<<"Enter next number: \n";
        cin>> num2;
    switch(op)
    {
            case'+':
                cout<<"\n +"<< num2;
                num= num2 + num;
                cout<< "result so far is "<< num;
                break;
            case'-':
                cout<<"\n -"<< num2;
                num= num2 - num;
                cout<<"result so far is "<< num;
                break;
            case'*':
                cout<<"\n *"<< num2;
                num= num2 * num;
                cout<<"result so far is "<< num;
                break;
            case'/':
                cout<<"\n /"<< num2;
                num= num2 *num;
                cout <<"result so far is "<< num;
                break;
            case'^':
                cout<<"\n ^"<< num2;
                num= pow(num2, num);
                break;
            case 'q':
                cout<<"\n q"<<"0";
                cout<<"Final result is: \n";
                cout<< num;
            default:
                cout<<"Not a valid operator";
    }        
                    
                    
                    
            


}

One things for sure I've at least gotten good a switch statements if nothing else.
Last edited on
Hi djru,

First, please use the code tags - it is the <> on the right under format.

Have you used the debugger? Or put cout statements in where you think there is a problem, so you can see the values of your variables.

Let us know how you get on

TheIdeasMan
Thanks, that does look MUCH better, I just got off work so i am going to try what you suggested. I know its recognizing my switch statement, the problem is when i try to use a loop its not returning to enter next operation. I was tired last night so I am pretty sure I just need to play around with it some more
You have 2 do_next_op functions that are identical. this might work in C++ because you can have function overloading with different argument types, but this is definitely the wrong way to do it here.

You need a while loop in main with a carefully chosen test-expression.
Okay, i guess I was just tired last night because I got the while loop to run the first try, the teacher wanted it weird like that, i think to try and trick us up but one function is do_NEX_op with no T and the other one is do_NEXT_op with a t. This is the code I ended up with
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <math.h> 


using namespace std;

//Prototypes

//Retrieves the operator, operand and returns operator
//and operand

void scan_data(char, double);

//Reads operator and operand and uses switch statement
//to perform correct operator on the operand

double do_nex_op(char, double, double&);

//Reads result and outputs the result of the operator
//and the operand

float do_next_op(double);

int main()
{
    char op;
    double num2;
    double sum;

    
  cout<< "This program is a very basic calculator \n";
  cout<< "use only operators +,-,*,/, or ^";
  cout<<" to raise a number to a power\n\n";

    
  //display operator and operand
    
    scan_data(op, num2);

    
return 0;    

}

// function to obtain original values

void scan_data(char op, double num2)
{
 
   double num;
   double result;
  
      num = do_nex_op ( op, num2, num);
  
      result = do_next_op(num);

}

// performs operation based on operator input

double do_nex_op(char op, double num2, double& num)
{
   num = 0;
  
  cout<< "Enter a valid operator:\n";
  cin >> op;
  
  cout<< "Enter any number:\n";
  cin >> num2;
       switch (op)
        {
            case'+':
                cout << "\n+"<< num2;
                num = num + num2;
                cout<<"\n\nresult so far " << num;
                break;
            case '-':
                cout << "\n-"<< num2;
                num = num - num2;
                cout <<"\n\nresult so far " << num;
                break;
            case '*':
                cout << "\n*"<< num2;
                num = num * num2;
                cout <<"\n\nresult so far "<< num;
                break;
            case '/':
                cout << "\n/"<< num2;
                num = num / num2;
                cout <<"\n\nresult so far "<< num;
                break;
            case '^':
                cout << "\n^"<< num2;
                num = pow(num, num2);
                cout <<"\n\nresult so far "<< num;
                break;
            case 'q':
                cout <<"\n q" <<" 0";
                cout << "\n\nfinal result is"<< num;
            default:
                cout<<"\n\nInvalid operator";
                
          }
    
     
   
      
  return (num);
}
// suppose to initiate my loop but not working!!!
float do_next_op(double num)
{
    double num2;
    char op;
    do
  {     cout<<"\n\nEnter Next Operation:\n";
        cin>> op;
        cout<<"Enter next number: \n";
        cin>> num2;
    switch(op)
    {
            case'+':
                cout<<"\n+"<< num2;
                num= num2 + num;
                cout<< "\nresult so far is "<< num;
                break;
            case'-':
                cout<<"\n -"<< num2;
                num= num2 - num;
                cout<<"\nresult so far is "<< num;
                break;
            case'*':
                cout<<"\n *"<< num2;
                num= num2 * num;
                cout<<"\nresult so far is "<< num;
                break;
            case'/':
                cout<<"\n/"<< num2;
                num= num2 / num;
                cout <<"\nresult so far is "<< num;
                break;
            case'^':
                cout<<"\n ^"<< num2;
                num= pow(num2, num);
                cout << "\nresult so far is "<< num;
                break;
            case 'q':
                cout<<"\n q"<<"0";
                cout<<"\nFinal result is: \n";
                cout<< num;
            default:
                cout<<"\nNot a valid operator";
    }        
 } while (num != 'q');             
                    
                    
   return 0;         


}


Now I am debugging and trying to figure out why when I try to use the division operator in my loop its returning a weird decimal
For example : I input 24 as my first operand, then when prompted try to divide it by 2 and it returns 0.083333333, weird, i tried to change all my floats to doubles and it didn't really do anything which is understandable if I understand the difference correctly. It was worth a try though
And I believe I just figured that part out I had
1
2
3
4
5
case'/':
                cout<<"\n/"<< num2;
                num= num2 / num;
                cout <<"\nresult so far is "<< num;
                break;


When I needed
1
2
3
4
5
case'/':
                cout<<"\n/"<< num2;
                num= num / num2;
                cout <<"\nresult so far is "<< num;
                break;


I had my num and num2 variables reverse
I do have one more quick question,

Would it be bad programming to use a if/else statment inside of my switch statement.
I ask because I am trying to think of how I would display an error if the user tries to divide a number by 0
For example:
1
2
3
4
5
6
7
8
case'/':
                  cout<<"\n/"<<num2;
                  if (num2 = 0)
                   cout<<"Error Cannot divide by zero";
                  else
                   num= num / num2;
                   cout << "\nresult so far is "<< num;
                  break;
Don't use do while loops - they can be as confusing as hell. Use a while loop instead.

You still have 2 do_next_op functions - not good.

When using cout, don't use "\n" use endl like this

 
 cout<<"\nFinal result is: " << endl;


endl flushes the buffer which can be important sometimes.

If you do division, check for divide by zero.



Edit: what happens when you step through with the debugger?
Last edited on
Topic archived. No new replies allowed.