Help with simple menu, trying to use if statement.

Hello, I am trying to create a simple menu using multiple if statements. I am not getting the results I am looking for. When I run my program, I am trying to enter the menuChoice (a,b,c,d,e) and have the output show the correct number in the initialInvestment variable. Can somebody please explain what I am doing incorrectly. Thanks in advance

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
  int main()
{

	// Program Variables

	char menuChoice;
	float initialInvestment = 0;
	int yearsToInvest = 0;
	float interestRate = 0;
	float finalAmount = 0;
	float interestEarned = 0;
	float timeToDoubleInvestment = 0;
	

	// Get Investment Amount - Display Menu Options

	cout << "Choose Investment Amount" << endl;
	cout << "------------------------" << endl;
	cout << "    A .......  5000" << endl;
	cout << "    B ....... 10000" << endl;
	cout << "    C ....... 15000" << endl;
	cout << "    D ....... 20000" << endl;
	cout << "    E ....... 25000" << endl;
	cout << "------------------------" << endl;
	cout << "  Enter option A-E: ";
	cin >> menuChoice;

	// Determine initial investment based on menu choice input
	// Use 5000 for an invalid response

	if (menuChoice == 'a' || 'A')
	{
		initialInvestment = 5000;
	}
	if (menuChoice == 'b' || 'B')
	{
		initialInvestment = 10000;
	}
	if (menuChoice == 'c' || 'C')
	{
		initialInvestment = 15000;
	}
	if (menuChoice == 'd' || 'D')
	{
		initialInvestment = 20000;
	}
	if (menuChoice == 'd' || 'D')
	{
		initialInvestment = 25000;
	}
	else
	{
		initialInvestment = 5000;
	}
	cout << initialInvestment;
	return 0;
}


EDIT: When I do run the program, and for example I enter C for the menuChoice variable, th answer will always come out as 25000, when it is supposed to be 15000.. I do not know how to fix this error, which I feel is very simple but I can not fix. I was instructed to not use an if-else if statement. But if that is the only way to fix, please mention that and explain why. Thanks.
Last edited on
1
2
3
4
if (menuChoice == 'a' || 'A')
{
	initialInvestment = 5000;
}


This is not valid C, nor C++.

Try:

1
2
3
4
if (menuChoice == 'a' || menyChoice == 'A')
{
	initialInvestment = 5000;
}
thank you sir
Topic archived. No new replies allowed.