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 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;
}
elseif (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;
}
elseif (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;
}
elseif (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;
}
elseif (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;
}
elseif (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.
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.
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.