Simple question regarding operators

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!

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
  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++;
		}
		else if (menuchoice = 2) //Prints all
		{
			print_all ();
		}
		else if (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);
		}
		else if (menuchoice = 4) //Prints statistics about all entered users
		{
			print_statistics();
		}
		else if (menuchoice = 5) //Quits the program
		{
			cout << "Have a good summer! ";
			cout << endl;
		}
	} while (menuchoice != 5);

	return 0;
}
closed account (48T7M4Gy)
if( menuchoice == 2) etc . Note == not =

This exercise is better suited to a switch control.

http://www.cplusplus.com/doc/tutorial/control/
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/204009/

BTW Don't double post. You waste people's time by duplicating effort. Close one down by green ticking it straight away.
When I do that change, it simply skips past the function call associated with the menu choice and defaults back to the top of the loop

Edit: This is an unrelated issue so I figured it wouldn't be relevant to put it in that thread
Last edited on
closed account (48T7M4Gy)
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. :)
Topic archived. No new replies allowed.