【发布时间】:2013-03-02 17:59:33
【问题描述】:
好的,所以我正在尝试编写一个主程序,它将要求用户输入数字 1 到 6,如果数字是 6,它将结束程序。如果大于6,它会要求重新输入数字。问题是,当我运行它时,它不会检查“if”语句并自动转到“请输入另一个选项”这一行
任何想法为什么我的程序会做这样的事情?
更新:我是说它会自动跳过所有 if 语句并询问 while 循环中的最后一个问题。
int main()
{
int userChoice = 0;
print(); //printing all of the options.
cout << "Please enter one of the options listed below" <<endl;
cin >> userChoice;
while(userChoice != 6)// 6 = the user wishing the end the program when they press 6.
{
if(userChoice == 1) //adding integer to the front of the list
{
addValueFront();
}
else if(userChoice == 2)//adding integer to the back of the list
{
addValueBack();
}
else if(userChoice == 3)//removing from the list
{
int n = 0;
cout << "Please enter the integer you wish to remove" << endl;
cin >> n;
removeValue(n);
}
else if(userChoice == 4)//printing the list
{
printList();
}
else if(userChoice == 5)//printing the number of items from the list
{
printItem();
}
else
{
cout << "The number you have entered is too high. Please try again" << endl;
cin >> userChoice;
}
cout << "please enter another option" <<endl;
cin >> userChoice; //sets up which option the user can choose from.
}
}
【问题讨论】:
-
标题描述性不太强。
-
定义“不工作”。我敢打赌它正在工作并且完全按照那里的代码告诉它去做。
-
这个块似乎没问题。此外,
switch-case更适合您的需求。 -
如果你指的是这个,你会在你的 tail-else 块和紧随其后的
cin >> userChoice;中双重提示错误的输入值。 -
@chris - 这是煤气总管! - 他还没有付账!
标签: c++