IF statement not working in basic menu

Hi,

I have to design a simple menu for my program, where a user presses a key corresponding to their menu choice. However, no matter what button they press, it will automatically pick the first menu option. I have included a line of code to ensure that the key press is being picked up, and it is, but the code seems to be ignoring it. I have been trying to find an answer to what I have done wrong all day, but I'm stumped.

This is basically it:


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
//This is the menu.  It displays the options that the user can make.

cout << "To insert an element into the table, press I. \n";
cout << "To delete an element from the table, press D. \n";
cout << "To look up an element in the table, press L. \n";
cout << "To obtain the current size of the table, press S. \n";
cout << "To print the current table, press P. \n \n";


char menuChoice; //As the menu is dependant on the user pressing a single key for their selection, I will declare menuChoice as a char.

cin >> menuChoice;  //This allows the user to make a choice by inputting their selection.

cout << "You have selected :" << menuChoice << endl;

if (menuChoice == 'I' || 'i')  //This allows the user to use either lower or upper case (I or i).
{
cout << "You have chosen to insert an element into the hash table. \n";
return 0;
}

else if (menuChoice == 'D' || 'd') //This allows the user to use either lower or upper case (D or d).
{
cout << "You have chosen to delete an element from the hash table. \n";
return 0;
}

else if (menuChoice == 'L' || 'l') //This allows the user to use either lower or upper case (L or l).
{
cout << "You have chosen to look up an element in the hash table. \n";
return 0;
} 

else if (menuChoice == 'S' || 's') //This allows the user to use either lower or upper case (S or s).
{
cout << "You have chosen to view the current size of the hash table. \n";
return 0;
}

else if (menuChoice == 'P' || 'p') //This allows the user to use either lower or upper case (P or p).
{
cout << "You have chosen to print out the current hash table. \n";
return 0;
}

else if (menuChoice !='I' || !='i' || !='D' || !='d' || !='L' || !='l' || !='S' || !='s' || !='P' || !='p')
{
cout << "You have made an invalid selection.
}

return 0;

}
 

I seem to have got my If statement wrong, seeing as it just takes the first option by default, no matter what the user selects. The user could type anything, but it ignores it.

I have literally just learned how to do some basic things in C++, so apologies for the terrible structure/code.

Hope someone can help!
Last edited on
You might want to consider using a switch statement instead of all those if else's.
There's a section on using it in the tutorials on this web site.
Looking again your problem might be because of the return 0; at the end of each if statement. You're telling the program to terminate. Remove them and try running it again.
Last edited on
 
if (menuChoice == 'I' || 'i')


This is evaluating to "If ((menuChoice == 'I') OR ('i' is TRUE))"
The latter will always be true: when enclosing a single character in single quotes, like 'i' or 'd' or anything else, it evalutes to an integer corresponding to the character. Every single one of these possible integers is positive, and any positive number will happily silently evaluate to TRUE in C++. So you basically managed to write:

if (menuChoice == 'I' || true)

Which will always execute.
Simply try instead:

if (menuChoice == 'I' || menuChoice == 'i')

So that you're actually comparing it to menuChoice in the second part of the if statement.
Topic archived. No new replies allowed.