We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,520 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Do while loop with menu and switch statement

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;
}
3
Contributors
2
Replies
9 Months
Discussion Span
7 Months Ago
Last Updated
6
Views
Question
Answered
acmarshall
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

mikrosfoititis
Junior Poster in Training
74 posts since Nov 2011
Reputation Points: 18
Solved Threads: 11
Skill Endorsements: 1
Question Answered as of 1 Year Ago by mikrosfoititis

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

acmarshall
Newbie Poster
5 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0657 seconds using 2.75MB