关于运营商的简单查询

时间:2016-12-06 07:37:03

标签: c++ operators

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

答案 0 :(得分:1)

<强> 1 &#39; =&#39;是为了分配    例如:int a = 5将5分配给名为a。

的变量

在你的情况下...你应该改变所有你的&#39; =&#39;到&#39; ==&#39;。 &#39; ==&#39;是为了比较。 例如:if(a==5)cout<<a;只会在等于5 ...

时打印

2 变量menuchoice不会取值...你不应该把它作为函数getselection的参数...相反你可以让它返回像这样的选择{{1 }}

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

相关问题