【问题标题】:While loop doesn't terminate?虽然循环不会终止?
【发布时间】:2013-03-25 13:15:32
【问题描述】:

我想知道为什么这个 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;


}

【问题讨论】:

  • “但是在测试我的代码后,我得到了正确的输出,但我的循环永远不会终止”——你是如何测试你的代码的?
  • 您是否向标准输入发送了 EOF(CtrlZ、CtrlD 等,取决于您的终端和平台)?

标签: c++ while-loop conditional


【解决方案1】:

程序始终等待您添加更多输入,因此它不会终止。有几种方法可以解决这个问题:

  • 让程序查找特定关键字,例如“quit”或“exit”,甚至只是一个空格,然后键入该关键字以终止。这很简单,但不是很优雅。
  • 发送“流结束”指示符作为输入中的唯一内容。在 linux 和 unix 中,您只需键入 Ctrl-D,这将表明您已关闭标准输入。正如一些 cmets 所说,Ctrl-Z 是 Windows 文件说明符的结尾,如果您使用它的话。

【讨论】:

    【解决方案2】:

    你的程序永远不会结束,因为你的外循环永远运行。

    可能的修复:

    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 循环。可能最好将它们放在一个函数中。

    【讨论】:

    • 代码没有问题。使用Ctrl+d 是一种从命令行退出的干净方法。
    【解决方案3】:

    您的程序表现出未定义的行为,因为它在此循环中读取到数组的末尾:

    for (int i = 0; i < 10; i++){
       if (roman_thousands[i] == line){
           cout << "The Arabic equivalent of " 
                << line <<" is: " << i << 0 << 0 << 0 << endl;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2014-06-06
      相关资源
      最近更新 更多