C++ 关于运算符的简单查询

C++ 关于运算符的简单查询,c++,operators,C++,Operators,您好,我正在编写一个程序,它运行许多基于用户输入的菜单选择的功能,我将不包括这些功能。我的问题是为什么下面的代码不响应用户输入的差异。例如,如果我输入菜单选项1或4,则无所谓,将恢复为菜单选项1。我知道这与我的=或==运算符有关,但两者都没有产生正确的结果,所以我不确定该怎么办。请帮忙 int main() //Handles the if statements concerning the menu of the program { int r_identifier[42]; //Variab

您好,我正在编写一个程序,它运行许多基于用户输入的菜单选择的功能,我将不包括这些功能。我的问题是为什么下面的代码不响应用户输入的差异。例如,如果我输入菜单选项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;
}
int main()//处理有关程序菜单的if语句
{
int r_标识符[42];//变量声明
国际年鉴条目[42];
双gpa_条目[42];
字符串student_name[42];
int指数=0;
int menuchoice;//变量声明
做
{
print_menu();//调用函数以打印菜单
get_selection(menuchoice);//调用函数获取菜单选择
if(menuchoice=1)//调用函数以输入新用户
{
输入新学生(学生姓名、r\U标识符、gpa\U条目、索引、年份\U条目);

cout1“=”用于赋值 例如:int a=5将5赋值给名为a的变量

在你的情况下…你应该把所有你的“=”改为“=”。 “==”用于比较。
例如:
if(a==5)cout=运算符用于赋值。使用==。为什么要将(未初始化的)
menuchoice
的值传递给``get\u selection()
?您不必设置
menuchoice`anywhere!@piet.t我有一个处理菜单选择输入的函数。它验证并返回用户响应,1-5。@MordechayS当我进行此更改时,它只需忽略输入并反复运行print_menu函数。@ChrisKeenan行
get_selection(menuchoice)
忽略任何可能从函数返回的值,并且由于
menuchoice
不是指针,您将无法在函数中更改其值。这是一个多么激动人心的时刻啊..在您说之前我没有意识到2。非常感谢!您愿意从整体上看一下我的程序并让我知道吗为什么我的一些函数很奇怪?如果是的话,这里是代码的链接,你肯定…@ChrisKeenan