有关操作员的简单查询

Simple query regarding operators

本文关键字:简单 查询 操作员      更新时间:2023-10-16

您好,我正在编写一个程序,该程序运行了许多基于用户输入的菜单选择运行的功能,我将不包括。我的问题是,为什么以下代码不响应用户输入的差异。例如,如果我输入菜单选择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;
}

1 。'='是用于分配的 例如:int a = 5分配5个名为a。

的变量

在您的情况下... u应该更改所有ur'=''=='。'=='是用于比较。例如:if(a==5)cout<<a;仅在a等于5 ...

时才打印A

2 变量menuchoice不采用一个值... u不应该将其作为函数getSelection的参数...相反,您可以让它返回类似于此 menuchoice=getselection()

3 包括其他部分...它为整个程序提供了更多的含义,而不是在...保持尽可能简单:)