Im making a calculator, and I can't seem to get it to restart.

I know i have to use some kind of loop, but I just can't get it to work.

Heres a bit of my calc just to show you.

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <windows.h>
#define PI 3.14159265

using namespace std;

int main()
{
double param, result, exp;
float b = 0, c = 0, d = 0, e= 0, f = 0;
float w =0 , x=0, y=0;
int z=0;
char a;
int t2, t3, g = 0;
char Q, q, R, r;

cout << "Welcome to the Ultimate Calculator. Copyright Daniel Xie 2008" << endl;

cout << "\n\n\nPlease enter your operation. First number, then the operation, then the second  number.\n(eg. 2*6 or 5+3)\n****If there is no 2nd number then just put in 0.****\n\n* = multiplication\n/ = division with decimal answer\n+ = addition\n- = subtraction\ny = square root\nu = cube root\np = exponent (1st number to the power of 2nd number)\nc = cosine\ns=sine\nt=tangent\n\n\n\n";
cin >> b;
cin >> a;
cin >> c;
cin.ignore();

while (a != Q || a != q || a != R || a != r)
{
switch(a)
{
case '+': d = b+c;
cout <<"\n\nAnswer:  " << d<< endl;
cin.get();
break;

case '-': d = b-c;
cout <<"\n\nAnswer:  " << d <<endl;
cin.get();
break;

case '*': d = b*c;
cout <<"\n\nAnswer:  " << d << endl;
cin.get();
break;

case '/': d = b/c;
cout <<"\n\nAnswer:  " << d << endl;
cin.get();
break;
case 'y': d = sqrt (b);
cout <<"\n\nAnswer: " << d << endl;
cin.get();
break;
case 'u': d = pow(b,1.0/3.0);
cout <<"\n\nAnswer: " << d <<endl;
cin.get();
break;
case 'p': d = pow(b,c/1.0);
cout << "\n\nAnswer: "<< d << endl;
cin.get();
break;
case 'c': d = cos (b*PI/180);
cout << "\n\nCosine of " << b << "is " << d << endl;
cin.get();
break;
case 's': d = sin (b*PI/180);
cout << "\n\nSine of " << b << "is " << d << endl;
cin.get();
break;
case 't': d = tan (b*PI/180);
cout << "\n\nTangent of " << b << "is " << d << endl;
cin.get();
break;
default: cout << "Invalid operation, please try again. " << endl;
cin.get();
return 0;
break;

}
}
}

Can someone show me how to restart this?

5
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by jesseb07
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <windows.h>
#define PI 3.14159265
using namespace std;

int main() {

//double param, result, exp;
float b = 0, c = 0, d = 0/*, e= 0, f = 0*/;
//float w =0, x=0, y=0;
//int z=0;
char a;
//int t2, t3, g = 0;
char Q, q, R, r;

cout << "Welcome to the Ultimate Calculator. Copyright Daniel Xie 2008"
<< endl;
for (;;) {
cout
<< "\n\n\nPlease enter your operation. First number, then the operation, then the second  number.\n(eg. 2*6 or 5+3)\n****If there is no 2nd number then just put in 0.****\n\n* = multiplication\n/ = division with decimal answer\n+ = addition\n- = subtraction\ny = square root\nu = cube root\np = exponent (1st number to the power of 2nd number)\nc = cosine\ns=sine\nt=tangent\n\n\n\n";
cin >> b;
cin >> a;
cin >> c;
cin.ignore();

while (a != Q || a != q || a != R || a != r) {
switch (a) {
case '+':
d = b+c;
cout <<"\n\nAnswer:  " << d<< endl;
cin.get();
break;

case '-':
d = b-c;
cout <<"\n\nAnswer:  " << d <<endl;
cin.get();
break;

case '*':
d = b*c;
cout <<"\n\nAnswer:  " << d << endl;
cin.get();
break;

case '/':
d = b/c;
cout <<"\n\nAnswer:  " << d << endl;
cin.get();
break;
case 'y':
d = sqrt(b);
cout <<"\n\nAnswer: " << d << endl;
cin.get();
break;
case 'u':
d = pow(b, 1.0/3.0);
cout <<"\n\nAnswer: " << d <<endl;
cin.get();
break;
case 'p':
d = pow(b, c/1.0);
cout << "\n\nAnswer: "<< d << endl;
cin.get();
break;
case 'c':
d = cos(b*PI/180);
cout << "\n\nCosine of " << b << "is " << d << endl;
cin.get();
break;
case 's':
d = sin(b*PI/180);
cout << "\n\nSine of " << b << "is " << d << endl;
cin.get();
break;
case 't':
d = tan(b*PI/180);
cout << "\n\nTangent of " << b << "is " << d << endl;
cin.get();
break;
default:
cout << "Invalid operation, please try again. " << endl;
cin.get();
return 0;
break;

}
break;
}
}
return 0;
}

It does? When i run it it has no mistakes at all, just doesn't restart.

But you can go ahead and delete it.

It does? When i run it it has no mistakes at all, just doesn't restart.

But you can go ahead and delete it.

I don't know if FTProtocol was referring to your code. He might have been referring to his own for all I know. ivailosp actually "solved" it for you, though once again he just posted code without mentioning a word about what was changed and why. He's also left all of the original problems in the code in place, and he clearly didn't test it since it never stops for input after the second calculation is entered. If you look at line 21, he's turned this into an infinite for-loop. You could replace that line with:

while(true) {

and it'll be the same.

It's an infinite loop with no way to get out of it. He kept your original while loop in there on line 29, which is completely pointless. Q, q, R, and r are uninitialized. Unless by some dumb weird luck your compiler initialized all of the four variables above to '*' and you happened to enter '*', if you didn't have the "break" on line 91, you'd be in an infinite loop since the values of a, Q, q, R, and r never change in this while loop. The "break" in line 91 defies the whole point of the loop anyway since you'll only go through this loop once due to the "break". Get rid of line 27 in the code. It results for the program never pausing for input after the first input. You can also delete all of the cin.get() lines in the switch statement as far as I can tell.

Go back to your original code, take out the cin.ignore() line and the cin.get() lines. Take out ivailosp's infinite for-loop on line 21. Take the while loop that is on line 29 and put it where the for-loop was on line 21.

Now look at your while loop:

while (a != Q || a != q || a != R || a != r)

Note that there are no quotes in this line. a is a char, so I'm guessing that you really wanted this:

while (a != 'Q' || a != 'q' || a != 'R' || a != 'r')

If the user types an upper or lower case 'q' or 'r', the program quits? I'm not sure. But actually this is taken care of already in line 87 so 'q' and 'r' are going to exit the program there anyway. The condition above will always be true, by the way, since you have the OR sign, not the AND sign (|| vs. &&). Basically the only reason your program didn't "work" before is because you had the cin statements BEFORE while loop rather than inside the while loop, and you have that cin.ignore() line that should be deleted.

Currently bad input exits out of the program. Do you want to do that or just display an error message? You can either change the while loop condition (you should do that anyway since the while loop condition is always true) to deal with the exit option, or you can put them in your switch statement as a "case" where it returns 0 to exit. If you don't want the program to end with bad input, don't have the default return 0.

edit: my fault for not noticing this was addressed in the above post, but I suppose a little more explanation why wouldn't hurt :)

this line of code:

while (a != 'Q' || a != 'q' || a != 'R' || a != 'r')

is essentially a

while(true)

change the or's to and's and that part will work as intended

while (a != 'Q' && a != 'q' && a != 'R' && a != 'r')

reasoning behind that change:

in order for that to return false, the key you enter would have to be a 'Q', 'q', 'R', and an 'r'. for an or statement to return false, all elements in the statement must be false.

if you switch them to and's, if you enter a 'Q', 'q', R', or an 'r', it will break the loop since only 1 element has to be false in an and statement to exit out of it.

~J

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.