Hello all, I am not sure what's wrong my code but, when I run my program it keeps going to my default switch statement. I would like my program to keep looping the menu until I put in the quit function. Can someone please tell me what I'm doing wrong? Thank you. My code is below

# include <cmath>
# include <iostream>
# include <iomanip>
# include <cctype>
# include <string> 
using namespace std;
int main()
{
string msg = "Please select from the following menu: ";

string menu = " [+] Add      [%] mod      [s] sin \n"
              " [-] Sub      [_] floor    [S] aSin \n"
          " [*] Mult     [~] ceil     [c] cos \n"
          " [/] Div      [^] power    [C] acos \n"
          " [x] Quit     [r] sqrt     [t] tan \n"
          " [?] Help     [l] log10    [T] atan \n"; 

cout << msg << endl << menu; 

int op;
cin >> op; 

do {switch( op )
{

case '+':
float n1, n2, add; 
cout << "Please enter two numbers: "; 
cin >> n1 >> n2; 
add = n1 + n2; 
cout << add << endl; 
break;

case '-':
float n3, n4, sub; 
cout << "Please enter two numbers: ";
cin >> n3 >> n4; 
sub = n3 - n4; 
cout << sub << endl; 
break; 

case '*':
float n5, n6, mult; 
cout << "Please enter two numbers: "; 
cin >> n5 >> n6; 
mult = n5 * n6; 
cout << mult << endl; 
break; 

case '/':
float n7, n8, div; 
cout << "Please enter two numbers: "; 
cin >> n7 >> n8; 
div = n7/n8; 
cout << div << endl; 
break; 

case 'x': return 0;
break; 

case '?': cout << "Select an item from this menu to compute" << endl; 
break; 

case '%':
int n9, n10, mod; 
cout << "Please enter two numbers: "; 
cin >> n9 >> n10; 
mod = n9 % n10; 
cout << mod << endl; 
break; 

case '_':
float n11, flo; 
cout << "Please enter one number: "; 
cin >> n11; 
flo = floor(n11); 
cout << flo << endl; 
break; 

case '~':
float n12, ce; 
cout << "Please enter one number: "; 
cin >> n12; 
ce = ceil(n12); 
cout << ce << endl; 
break; 

case '^':
float n13, n14, power; 
cout << "Please enter two numbers: "; 
cin >> n13 >> n14; 
power = pow(n13, n14); 
cout << power << endl; 
break; 

case 'r':
float n15, root; 
cout << "Please enter one number: "; 
cin >> n15; 
root = sqrt(n15); 
cout << root << endl; 
break; 

case 'l':
float n16, log; 
cout << "Please enter one number: "; 
cin >> n16; 
log = log10(n16); 
cout << log << endl; 
break; 

case 's':
float n17, s; 
cout << "Please enter one number: "; 
cin >> n17; 
s = sin(n17); 
cout << s << endl; 
break;

case 'S':
float n18, S; 
cout << "Please enter one number: "; 
cin >> n18; 
S = asin(n18); 
cout << S << endl; 
break;

case 'c':
float n19, c; 
cout << "Please enter one number: "; 
cin >> n19; 
c = cos(n19); 
cout << c << endl; 
break;

case 'C':
float n20, C; 
cout << "Please enter one number: "; 
cin >> n20; 
C = acos(n20); 
cout << C << endl; 
break;

case 't':
float n21, t; 
cout << "Please enter one number: "; 
cin >> n21; 
t = tan(n21); 
cout << t << endl;
break; 

case 'T':
float n22, T; 
cout << "Please enter one number: "; 
cin >> n22; 
T = atan(n22); 
cout << T << endl; 
break;

break;
default: cout << "Selection is not recognized" << endl; break; 
} }while  (op != 'x'); 

system ("pause");
return 0;
}

Recommended Answers

All 6 Replies

Try making the op variable of type char.
You will also want to begin your do{...}while loop a little bit earlier before the cin >> op command hits.

Try making the op variable of type char.
You will also want to begin your do{...}while loop a little bit earlier before the cin >> op command hits.

Thank you! I just changed it now. I can't believe it was as simple as that. You really helped a lot

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int m;
int n;
char op;

char choice;


do
{cout<<"enter 1st number";
cin>>m;
cout<<"enter 2nd number";
cin>>n;
cout<<"enter operator"<<endl;
cin>>op;
switch (op)
{   case'+':
         cout<<"m+n= "<<m+n<<endl;
         break;
    case'-':
          cout<<"m-n= "<<m-n<<endl;
          break;
    case'*':
            cout<<"m*n= "<<m*n<<endl;
            break;
    case'/':
        cout<<"m/n= "<<m/n<<endl;
        break;}

}while(choice=='y');
    cout<<"perform another operation(y/n)"<<endl;
    cin>>choice;



    _getch();
}

why is this program just stopping when I enter choice.

you need to put lines 35 and 36 inside the do while loop. Also dont use void main(). In the c++ standard it says that main returns an int.

You should also start a new thread, instead of hijacking someone else's old threat!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.