为什么循环卡住了
why is loop stuck
现在唯一剩下的问题是我的choice
while 循环是无限的,因为 break 语句似乎根本没有脱离循环,所以程序不会读取answer
循环。 此外,"无效条目"显示所有其他不正确的输入,而不是每次输入无效字符时都显示
#include <iostream>
#include <cctype>
using namespace std;
int getAges(int age, const int SIZE);
char getChoice();
void displayInOrder(int numbers[], const int SIZE, char choice);
void displayInReverse(int numbers[], const int SIZE, char choice);
int main()
{
const int SIZE = 5;
int numbers[SIZE] = { 1, 2 ,3 ,4, 5 };
char answer = 0;
int age = 0;
char choice = 0;
while (choice = getChoice())
{
if (toupper(choice) == 'O')
{
displayInOrder(numbers, SIZE, choice);
break;
}
else if (toupper(choice) == 'R')
{
displayInReverse(numbers, SIZE, choice);
break;
}
else
{
cout << "Invalid entry! - Must be O or Rnn";
break;
}
}
while (toupper(answer) == 'Y')
{
system("cls");
age = getAges(age, SIZE);
choice = getChoice();
displayInOrder(numbers, SIZE, choice);
displayInReverse(numbers, SIZE, choice);
cout << "Run program again (Y or N)? ";
cin >> answer;
if (toupper(answer) == 'N')
{
exit();
}
}
return 0;
}
int getAges(int age, const int SIZE)
{
cout << "Enter " << SIZE << " ages: nn";
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
cin >> age;
cout << endl;
return age;
}
char getChoice()
{
char choice;
cout << "How do you want to see the ages displayed? nn Enter O for In Order, or R for In Reverse.nn";
cin >> choice;
return choice;
}
void displayInOrder(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in order: nn";
for (int i = 0; i < SIZE; i++)
{
cout << numbers[i] << endl;
}
}
void displayInReverse(int numbers[], const int SIZE, char answer)
{
cout << "Here are the ages in reverse order: nn";
for (int i = SIZE - 1; i >= 0; i--)
{
cout << numbers[i] << endl;
}
}
1."无效条目"显示所有其他不正确的输入:你在while循环中有一个getChoice((调用,在这里:
else
{
cout << "Invalid entry! - Must be O or Rnn";
choice = getChoice();
}
后跟 getChoice(( 调用
while (choice = getChoice())
因此,不会处理前面的 getChoice((。
2.">如果用户输入N进行回答,我不确定如何关闭程序",您应该考虑是否可以到达第二个while循环以及在哪里设置answer
变量或基本上在哪里接受用户输入以结束程序?你应该看看它是否可以在第一个while循环中得到照顾?
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 为什么我的for循环不能正确获取argv
- 为什么在这个代码结束循环中没有得到结束
- 为什么我无法更改"set<set>"循环中的值<int>
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 为什么我不能在 while 循环中创建线程?
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 当我在第一个循环中使用"auto"时,它工作正常,但是使用"int"它会给出错误,为什么?
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么我的程序在for循环中k=0时返回垃圾值
- 为什么在递归中使用循环会产生意想不到的结果?
- 为什么在 while 循环中返回表达式不起作用
- 为什么我的 scanf() 没有在我的数组上迭代我的 for 循环?
- 而循环:简单的除法程序输出零,不明白为什么
- 为什么我在尝试在单向链表中打印元素时会出现这个永无止境的循环
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么循环不会停止?C++
- 我的CLL程序在c++中不会停止循环!为什么
- (c++) For循环-为什么它这样做