Hello I'm writing a program which runs a lot of functions that run based on the user inputted menu choice which I will not be including. My question is why is the following code not responding to differences in user input. For example if I enter menu choice 1 or 4 it doesn't matter and will revert to menu choice 1. I know it has something to do with my = or == operators but neither has produced the correct result so im not sure. Please help!
int main() //Handles the if statements concerning the menu of the program
{
int r_identifier[42]; //Variable Declaration
int year_entry[42];
double gpa_entry[42];
string student_name[42];
int index = 0;
int menuchoice; //Variable Declaration
do
{
print_menu(); //Calls function to print menu
get_selection(menuchoice); //Calls function to get the menu selection
if (menuchoice = 1) //Calls the function to input a new user
{
input_new_student(student_name, r_identifier, gpa_entry, index, year_entry);
cout << "\nThe student with R#" << r_identifier[index] << " was created. " << endl;
index++;
}
elseif (menuchoice = 2) //Prints all
{
print_all ();
}
elseif (menuchoice = 3) //Prints statistics about all students in a particular year
{
int year_view;
print_by_year(student_name, r_identifier, gpa_entry, index, year_entry);
}
elseif (menuchoice = 4) //Prints statistics about all entered users
{
print_statistics();
}
elseif (menuchoice = 5) //Quits the program
{
cout << "Have a good summer! ";
cout << endl;
}
} while (menuchoice != 5);
return 0;
}
I assume you have closed the other one down. It would have made sense to stay with that.
However, in addition to the == problem which needs to be fixed you have a problem with the variable menuchoice as it is passed by value to the getmenuchoice function. The function returns a vale that goes nowhere.
so you need to write menuchoice = get_selection(menuchoice); in your line 12 above. In fact your function could be just get_selection() without any need to pass menuchoice as a parameter. If you did then it should be passed by reference and the function then would be void, returning nothing.
(You need to work out a way to get back to the menu afetr you have finished adding students. :)