Using Char in a Menu/While Loop

Hi guys I'm working on a project that uses type char instead of type int to make selections in a menu that calls functions. I'm really hitting a wall with the while loop, I'm pretty sure my syntax is wrong for type char, any help is appreciated!

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
#include <iostream>
#include <cmath> 
using namespace std; 
// ***********************************************************************
void ShowMenu();
void SquareRoot(double UserEntry);
void AbsValue(double UserEntry);
void Log(double UserEntry);
void Inverse(double UserEntry);
void Cubed(double UserEntry);


// ***********************************************************************
int main(void)
{
	char MenuChoice;
	double UserEntry;

	do
	{
		ShowMenu();
		cin>>MenuChoice;

		while (MenuChoice != '1','2','3','4','5','0')
		{
			cout << "Please select a menu item (0-5):   ";
			cin >> MenuChoice;
		}

		if ( MenuChoice = '0','1','2','3','4','5')
		{
			cout << "Enter a non negative decimal number:";
			cin >> UserEntry;

			switch (MenuChoice)
			{
			case 1: SquareRoot(UserEntry);
					break;
			case 2: AbsValue(UserEntry);
					break;
			case 3: Log(UserEntry);
					break;
			case 4: Inverse(UserEntry);
					break;
			case 5: Cubed(UserEntry);
			}
	}
				
	
		{
		}
	} while (MenuChoice='0');
	return 0;
}
// *********************************************************************
// * Your functions that do the square root, absolute value, log, inverse, an
// * cube go here. 
// **********************************************************************
void ShowMenu()
{
		cout<< "M E N U" <<endl;
	cout<< "1 - Calculate Square Root" <<endl;
	cout<< "2 - Calculate Cube" <<endl;
	cout<< "3 - Calculate Natural Logarithm" <<endl;
	cout<< "4 - Calculate Inverse" <<endl;
	cout<< "5 - Absolute Value" <<endl;
	cout<< "0 - Exit Program" <<endl;
	cout<< "Enter Menu Option   ";
}

void SquareRoot(double UserEntry)
{
	cout << "The Square Root of"<<UserEntry<<"is"
		 << (sqrt(UserEntry)) << endl;
}
void AbsValue(double UserEntry)
{
	cout << "The Absolute Value of" << UserEntry<<"is"
		<< (fabs(UserEntry)) << endl;
}
void Log(double UserEntry)
{
	cout << "The Natural Log of" << UserEntry<<"is"
		<< (log(UserEntry)) << endl;
}
void Inverse(double UserEntry)
{
	cout << "The Inverse of" << UserEntry<<"is"
		<< (1.0/UserEntry) << endl;
}
void Cubed(double UserEntry)
{
	cout << "The cube of" << UserEntry<<"is"
		<< (pow(UserEntry,3)) << endl;





}
Lines 30 and 52:
They will always resolve into a true statement. The "=" operator is only used to assign values. To test if two values are equal, use "==". Also, I think you meant to test if MenuOption != '0' on line 52.

Lines 24 and 30:
To test multiple conditions, you have to unfortunately make separate statements. for example:
if(x == 'a' || x == 'b' || x == 'c')
"&&" is known as the Boolean AND and "||" is the Boolean OR.

You switch statement starting from line 35:
MenuOption is a char, so the '1', '2', etc. stored in it are not equivalent to 1, 2, etc. Place the single quotation marks in each of your cases to test MenuOption against the correct values.

Edit:
The if condition on line 30 seems unnecessary.
Last edited on
Topic archived. No new replies allowed.