#include <iostream>
usingnamespace std;
class setup{
public:
void title(){
system("TITLE calculator");
system("color 2");
cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
}
};
int main()
{
setup setup1;
setup1.title();
int howmany;
double a;
double b;
double c;
double d;
double e;
char cchar;
char cagain;
cout << "please enter the amount of numbers you want to use: ";
cin >> howmany;
switch (howmany){
case 1:
cout << "invalid number" << endl;
break;
case 2:
do{
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << " = "
<< (a + b) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " = "
<< (a - b) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " = "
<< (a * b) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " = "
<< (a / b) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
break;
case 3:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " = "
<< (a + b + c) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << " = "
<< (a - b - c) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " = "
<< (a * b * c) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " = "
<< (a / b / c) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
case 4:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << "+" << d << " = "
<< (a + b + c + d) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << "-" << d << " = "
<< (a - b - c - d) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " = "
<< (a * b * c * d) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " = "
<< (a / b / c / d) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
case 5:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
cout << "please enter the fifth number you would like to use" << endl;
cin >> e;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " + " << d << " + " << e << " = "
<< (a + b + c + d + e) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << " - " << d << " - " << e << " = "
<< (a - b - c - d - e) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " * " << e << " = "
<< (a * b * c * d * e) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " / " << e << " = "
<< (a / b / c / d / e) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
default:
cout << "invlaid number.you can only use up to 5" << endl;
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
}
system("pause");
return 0;
}
Using correct indenting will help you debug your programs in the future. It is not a request, it is a must as tougher programs will get MUCH more difficult if your code remains messy.
At line 193, when the condition is true, it will return you to the "do" at line 151. It will NOT return you to the top of the main
At line 32, you ask how many numbers to use. This is NOT in a loop and so once you get it the first time, you will never see it again. You need to put it in a loop for it to come up again.
Use proper indenting and you'll see exactly where each "While" bring you back and exactley what is in each loop/switch.
I just spent 20 minutes formatting your code to correct the indenting. Otherwise it is illegible. That is why no one has responded to you.
This is what you need to do:
1. Delete ALL lines with do{
2. Delete ALL lines with }while (cagain == 'y' || cagain == 'Y');
3. Place }while (cagain == 'y' || cagain == 'Y'); immediately before your system("pause")
4. Place a do{ immediately before your first cout <<
Other things you missed:
5. break at the end of cases 3, 4, and 5 of switch (howmany){.
#include <iostream>
usingnamespace std;
class setup{
public:
void title(){
system("TITLE calculator");
system("color 2");
cout << "hello!!!, IM A CALCULATOR... lets get started" << endl << endl;
}
};
int main()
{
setup setup1;
setup1.title();
int howmany;
double a;
double b;
double c;
double d;
double e;
char cchar;
char cagain;
cout << "please enter the amount of numbers you want to use: ";
cin >> howmany;
switch (howmany){
case 1:
cout << "invalid number" << endl;
break;
case 2:
do{
cout << "please enter the first number you want to use" << endl;
cin >> a;
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << " = "
<< (a + b) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << " = "
<< (a - b) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " = "
<< (a * b) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " = "
<< (a / b) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
break;
case 3:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " = "
<< (a + b + c) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << " = "
<< (a - b - c) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " = "
<< (a * b * c) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " = "
<< (a / b / c) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
case 4:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << "+" << d << " = "
<< (a + b + c + d) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << "-" << d << " = "
<< (a - b - c - d) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " = "
<< (a * b * c * d) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " = "
<< (a / b / c / d) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
case 5:
do{
cout << "please enter the operation you would like to use" << endl;
cin >> cchar;
cout << "please enter the first number you would like to use" << endl;
cin >> a;
cout << "please enter the second number you would like to use" << endl;
cin >> b;
cout << "please enter the third number you would like to use" << endl;
cin >> c;
cout << "please enter the forth number you would like to use" << endl;
cin >> d;
cout << "please enter the fifth number you would like to use" << endl;
cin >> e;
switch (cchar){
case'+':
cout << "the answer is: " << a << " + " << b << "+" << c << " + " << d << " + " << e << " = "
<< (a + b + c + d + e) << endl;
break;
case'-':
cout << "the answer is: " << a << " - " << b << "-" << c << " - " << d << " - " << e << " = "
<< (a - b - c - d - e) << endl;
break;
case'*':
cout << "the answer is: " << a << " * " << b << " * " << c << " * " << d << " * " << e << " = "
<< (a * b * c * d * e) << endl;
break;
case'/':
cout << "the answer is: " << a << " / " << b << " / " << c << " / " << d << " / " << e << " = "
<< (a / b / c / d / e) << endl;
break;
default:
cout << "you cant use that operation";
break;
}
default:
cout << "invlaid number.you can only use up to 5" << endl;
cout << "would you like to start again (y or n)";
cin >> cagain;
}while (cagain == 'y' || cagain == 'Y');
}
system("pause");
return 0;
}
I have to go to work now so I haven't applied the changes suggested above to your code.