虽然循环不终止?
While loop doesn't terminate?
我想知道为什么这个while循环不允许我的程序终止?
正如我所理解的(尽管我很可能是错误的)条件while (cin >> line)
检查我的输入流中的字符串,然后运行我的循环,直到在输入中没有找到其他字符串。然而,测试我的代码后,我得到了正确的输出,但我的循环从来没有终止任何想法,为什么?
#include <cstdlib>
#include <iostream>
#include <cctype>
using namespace std;
int main() {
string roman_digits[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
string roman_tens [] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
string roman_hundreds [] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
string roman_thousands [] = {"", "M","MM", "MMM"};
string line;
char c;
cout << "Type in a Roman numeral: ";
// Loops through inputted Roman Numerals.
while (cin >> line){
int i = 0;
// Loops through a Roman numeral and changes it to uppercase.
while(line[i]){
c = line[i];
c = (toupper(c));
line[i] = c;
i++;
}
// Loops through checking roman numeral with the thousands array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++){
if (roman_thousands[i] == line){
cout << "The Arabic equivalent of "
<< line <<" is: " << i << 0 << 0 << 0 << endl;
}
}
// Loops through checking roman numeral with the hundreds array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++){
if (roman_hundreds[i] == line){
cout << "The Arabic equivalent of " << line << " is: " << i << 0 << 0 << endl;
}
}
// Loops through checking roman numeral with the tens array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++){
if (roman_tens[i] == line){
cout << "The Arabic equivalent of " << line << " is: " << i << 0 << endl;
}
}
// Loops through checking roman numeral with the digits array and if there is a match prints out the equivalent arabic number.
for (int i = 0; i < 10; i++){
if (roman_digits[i] == line){
cout << "The Arabic equivalent of " << line << " is: " << i << endl;
}
}
}
return 0;
}
程序总是等待您添加更多的输入,所以它不会终止。有几种方法可以解决这个问题:
- 让程序查找一个特定的关键字,如"退出"或"退出",甚至只是一个空格,并键入该终止。这很简单,但不是很优雅。
- 发送一个"流结束"指示符作为你输入的唯一内容。在linux和unix中,您只需键入Ctrl-D,这将表明您已经关闭了标准输入。正如一些评论所说,Ctrl-Z是Windows的文件结束说明符,如果你正在使用它。
你的程序永远不会结束,因为你的外部循环永远在运行。
可能的解决办法:
while (cin >> line)
{
int i = 0;
if (line == "quit") break;
while(line[i])
{
c = line[i];
c = (toupper(c));
line[i] = c;
i++;
}
// run for loops
}
则需要调用所有的for循环。也许最好将它们放在函数中。
你的程序表现出未定义的行为,因为它在这个循环中读取了数组的末尾:
for (int i = 0; i < 10; i++){
if (roman_thousands[i] == line){
cout << "The Arabic equivalent of "
<< line <<" is: " << i << 0 << 0 << 0 << endl;
}
}
相关文章:
- while 循环不以哨兵值终止
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- C++ 即使不满足条件,循环也会终止
- 为什么 EOF 在循环扫描期间没有终止?
- 对于检查 >=0 终止条件时的循环索引类型
- 程序在终止循环后不接收输入
- 即使我尝试递减 t,While 循环也没有终止
- 如何修复不会在退出选择时终止的 do-while 循环
- 如何用字符终止整数输入循环?
- 读入变量时循环终止,'Range_error' on C++
- 当我输入非整数值时,我的二次方程代码终止,如何让它正确循环?
- 如何正确终止此循环?
- 多个查询的循环过早终止
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 终止具有特定字符的循环
- 程序终止,而不是继续整数输入验证循环
- 程序似乎在 for 循环 - C++后静默终止
- 循环终止不正确时
- C :终止循环的最优雅方法,该循环在字符串向量上迭代
- 如果未检测到alpha,则终止循环