0

I was wondering why this while loop doesn't allow my program to terminate?

As I understand (though I could well be wrong) the condition while (cin >> line) checks my input stream for a String and then runs my loop until there is no other String found in the input. However after testing my code I get the correct output but my loop never terminates any idea as to why?

#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;


}
3
  • "However after testing my code I get the correct output but my loop never terminates" -- how did you test your code? Commented Apr 3, 2013 at 18:08
  • 4
    Did you send an EOF (CtrlZ, CtrlD, etc. depending on your terminal and platform) to stdin ? Commented Apr 3, 2013 at 18:08
  • duplicate: stackoverflow.com/questions/5360129/the-question-on-while-cin Commented Apr 3, 2013 at 18:09

3 Answers 3

6

The program always waits for you to add more input, so it won't terminate. There are a few ways around this:

  • Have the program look for a specific keyword like "quit" or "exit" or even just a blank space, and type that to terminate. This is very simple, but not very elegant.
  • Send an "end of stream" indicator as the only thing in your input. In linux and unix, you can just type Ctrl-D, and this will indicate that you've closed standard input. As some of the comments say, Ctrl-Z is the end of file specifier for Windows, if you're using that.
Sign up to request clarification or add additional context in comments.

Comments

2

Your program never ends because your outer loop runs forever.

Possible fix:

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
}

then you are going to have to call all of those for loops. Probably best to put them in a function.

1 Comment

there is nothing wrong in the code. Using Ctrl+d is a clean way to exit from a command line.
0

Your program exhibits undefined behavior because it reads past the end of the array in this loop:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.