2016-12-06 69 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