I need help with a simple fix

my code works fine and compiles im just having trouble with what the condition should be for the do-while loop to have the program exist if the user enters a q or Q

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
 // -------------------------------------------------------------------------------
// COP 3014C Fundamentals of Programming
// Assignment ID:   PROG7b
// Due Date: 10/28/12 at 11 pm
// File name:   calculator_b.cpp
//
// Author:   bfields Byron Fields
// 
// Purpose:    You are provided an executable program for a simple calculator.
//               Your job is to design and implement a program that behaves 
//               THE VERY SAME WAY.
//  
// Author: bfields Byron Fields
//    

//---------------------------------------------------------------------------------

#include <iostream>
#include <cmath>

using namespace std;

int main()
{

cout << "(c) 2012, bfields Byron Fields" << endl;

char function;
int loperand;
int roperand;
int dividend;
int divisor;
double power;
int add;
int subtract;
int multiplicand;
int multiplier;
int multiple;
int divided;
int remainder;
double base;
double exponent;
int addops = 0;
int subops = 0;
int mulops = 0;
int divops = 0;
int remops = 0;
int powops = 0;
int quiops = 0;
int badops = 0;

do
{
cout << ' ' << endl;
cout << "=========================================" << endl;
cout << "|             MY CALCULATOR             |" << endl;
cout << "| ===================================== |" << endl;
cout << "|   A/a)   Add                          |" << endl;
cout << "|   S/s)   Subtract                     |" << endl;
cout << "|   M/m)   Multiply                     |" << endl;
cout << "|   D/d)   Divide                       |" << endl;
cout << "|   R/r)   Remainder (Mod)              |" << endl;
cout << "|   P/p)   Power (Exponentiate)         |" << endl;
cout << "|   Q/q)   QUIT (turn off)              |" << endl;
cout << "| ===================================== |" << endl;
cout << "| (c) 2012, bfields Byron Fields        |" << endl;
cout << "=========================================" << endl;


cin >> function;

switch(function)
{
case 'a' :
case 'A' : cout << "A: Enter left operand: " << endl;
           cin >> loperand;
           cout << "A: Enter right operand: " << endl;
           cin >> roperand;
           add = loperand + roperand;
           cout << loperand << " + " << roperand << " = " << add;
           addops =+ 1;
           break;           

case 's' :
case 'S' : cout << "S: Enter left operand: " << endl;
           cin >> loperand;
           cout << "S: Enter right operand: " << endl;
           cin >> roperand;
           subtract = loperand - roperand;
           cout << loperand << " - " << roperand << " = " << subtract;
           subops =+ 1;
           break;

case 'm' :
case 'M' : cout << "M: Enter Multiplicand: " << endl;
           cin >> multiplicand;
           cout << "M: Enter Multiplier: " << endl;
           cin >> multiplier;
           multiple = multiplicand * multiplier;
           cout << multiplicand << " * " << multiplier << " = " << multiple;
           mulops =+ 1;
           break;    

case 'd' :
case 'D' : cout << "D: Enter Dividend: " << endl;
           cin >> dividend;
           cout << "D: Enter Divisor: " << endl;
           cin >> divisor;
           divided = dividend / divisor;
           cout << dividend << " / " << divisor << " = " << divided;
           divops =+ 1;
           break; 

case 'r' :
case 'R'  : cout << "R: Enter left operand: " << endl;
           cin >> loperand;
           cout << "R: Enter right operand: " << endl;
           cin >> roperand;
           remainder = loperand % roperand;
           cout << loperand << " % " << roperand << " = " << remainder;
           remops =+ 1;
           break;   

case 'p' :
case 'P'  : cout << "P: Enter the number: " << endl;
           cin >> base;
           cout << "P: Enter the power: " << endl;
           cin >> exponent;
           power = pow(base,exponent);
           cout << "pow(" << base << ',' << exponent << ')' << " = " << power;
           powops =+ 1;
           break;  


case 'q' :
case 'Q' : quiops =+ 1;
	   cout << "Q: READY TO POWER DOWN ..." << endl;
           cout << "COUNT OF OPERATIONS:" << endl;
           cout << "\t " << addops << " ADD" << endl;
	   cout << "\t " << subops << " SUBTRACT" << endl;
	   cout << "\t " << mulops << " MULTIPLY" << endl;
	   cout << "\t " << divops << " DIVIDE" << endl;
           cout << "\t " << remops << " REMAINDER" << endl;
	   cout << "\t " << powops << " POWER" << endl;
	   cout << "\t " << quiops << " QUIT" << endl;
           cout << "\t " << badops << " BAD OPERATIONS" << endl;
           cout << "\n(c) 2012, bfields Byron Fields" << endl;
           cout << ' ' << endl;
           cout << ' ' << endl;
           cout << ' ' << endl;
           cout << "POWERING DOWN ... Good-bye" << endl;
           break; 

default : cout << ' ' << endl;
}
} while (function != 'q' || 'Q' );

return 0;

}
while (function != 'q' && function != 'Q' );
thanks man i appreciate it. do you have an explanation for why it works with the "and" statement and not the "or" one though ?
The condition in your while statement is evaluated as follows:
1
2
3
  bool temp1 = (function != 'q');
  bool temp2 = 'Q';  // temp2 will always be true
  while (temp1 || temp2)  // Therefore the condition is always true 


See precedence of Operators here:
http://www.cplusplus.com/doc/tutorial/operators/


Basically "or" means "If at least one of these is true, the whole expression is true"

"and" means "If both of these are true then the expression is true, otherwise it's false"
while (function != 'q' || 'Q' );

This means keep doing the loop as long as the thing in the brackets comes out as TRUE. Let's look at the thing in the brackets.

function != 'q' || 'Q'

It's an OR statement. An OR statement comes out as TRUE if either side, or both sides, are TRUE.

Let's look at the left hand side.
function != 'q'
So, this will be TRUE is function isn't 'q', and FALSE for all other cases.


Let's look at the right hand side.
'Q'
Is 'Q' TRUE or FALSE? Well, in C and C++, zero is false and non-zero is true. This is not zero, so it's TRUE. Always and forever. So this:
function != 'q' || 'Q'
has the right hand side as always true, so this
while (function != 'q' || 'Q' );
will always come out as TRUE. Always always always.

Now you repeat the analysis for
while (function != 'q' && function != 'Q' );
Topic archived. No new replies allowed.