2016-12-06 79 views
-1

你好我正在编写一个程序,运行很多基于用户输入的菜单选项运行的功能,我不会包括在内。我的问题是为什么下面的代码没有响应用户输入的差异。例如,如果我进入菜单选择1或4,这并不重要,并会回到菜单选择1.我知道这与我的=或==运营商有关,但都没有产生正确的结果,所以我不知道该怎么做做。请帮忙! 。关于运营商的简单查询

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; 
} 
+0

=运算符用于赋值。使用==。 – MordechayS

+1

为什么要将(未初始化)'menuchoice'的值传递给''get_selection()'?你不要在任何地方设置'menuchoice'的值! –

+0

@ piet.t我有一个处理菜单选择输入的函数。它验证并返回用户响应1-5。 –

回答

1

'=' 是用于分配 实施例中:int a = 5个受让人5到名为一个的变量中。

在你的情况下......你应该把所有的'='改为'=='。 '=='用于比较。 例:if(a==5)cout<<a;将打印仅如果等于5 ...

变量menuchoice犯规取一个值...ü不应该把它作为函数getselection的参数......而是U可以使其返回的选择是这样的menuchoice=getselection()

包括其他部分...它给一些更有意义的整个程序,而不是做一段时间...保持它尽可能的简单:)

+0

什么是facepalm时刻..在你说出来之前,我没有意识到2。非常感谢哟!你愿意看看我的程序作为一个整体,让我知道为什么我的一些功能是奇怪的?如果是这样,这里是链接到代码http://pastebin.com/fxhPFeec –

+0

雅确定... @ChrisKeenan – Akilesh