0

この 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;


}
4

3 に答える 3

6

プログラムは常に入力が追加されるのを待っているため、終了しません。これにはいくつかの方法があります。

  • プログラムに「quit」や「exit」、または空白スペースなどの特定のキーワードを探しさせ、それを入力して終了させます。これは非常に単純ですが、あまりエレガントではありません。
  • 入力の唯一のものとして「ストリームの終わり」インジケーターを送信します。Linux と UNIX では、Ctrl-D と入力するだけで、標準入力を閉じたことを示します。一部のコメントが言うように、Ctrl-Z を使用している場合、Windows のファイル指定子の末尾は Ctrl-Z です。
于 2013-04-03T18:09:01.010 に答える
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 ループで呼び出す必要があります。おそらく、それらを関数に入れるのが最善です。

于 2013-04-03T18:09:20.333 に答える
0

このループで配列の末尾を超えて読み取るため、プログラムは未定義の動作を示します。

for (int i = 0; i < 10; i++){
   if (roman_thousands[i] == line){
       cout << "The Arabic equivalent of " 
            << line <<" is: " << i << 0 << 0 << 0 << endl;
    }
}
于 2013-04-03T18:19:45.290 に答える