简体   繁体   English

满足中断条件后循环继续

[英]While loop continuing after break condition met

My current assignment requires me to allow for a user to pass strings of characters and convert from roman numerals to a decimal value.我当前的任务要求我允许用户传递字符串并将罗马数字转换为十进制值。 I've faced an issue that when random words are passed through the program it must return the longest valid prefix and ignore the remaining characters.我遇到了一个问题,当随机单词通过程序时,它必须返回最长的有效前缀并忽略剩余的字符。 My code runs through a while loop and once it reaches the break condition it repeats the code again for the remaining characters.我的代码通过一个while循环运行,一旦达到中断条件,它就会再次为剩余的字符重复代码。 I'm unsure what next step I should take to fix this code to output correctly.我不确定下一步应该采取什么措施来正确将此代码修复为 output。 Following image shows the characters to pass through the code.下图显示了要通过代码的字符。 Image of test failure测试失败的图像

#include <iostream>
#include <ctype.h>

using namespace std;
int
main (){
   while (!cin.eof ()){
    char inputChar;
    int result = 0;
    while (cin.get (inputChar))
    {
        inputChar = toupper (inputChar);
        if (inputChar == 'M')
            result = result + 1000;
        else if (inputChar == 'D')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M')
            {
                result = result - 500;
                continue;
            }
            else if (inputChar == 'D'){
                result = result;
                continue;
            }
            else
            {
                result = result + 500;
                continue;
            }
        }
        else if (inputChar == 'C')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M' || inputChar == 'D')
            {
                result = result - 100;
                continue;
            }
            else
            {
                result = result + 100;
                continue;
            }
        }
        else if (inputChar == 'L')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M' || inputChar == 'D' || inputChar == 'C')
            {
                result = result - 50;
                continue;
            }
            else
            {
                result = result + 50;
                continue;
            }
        }
        else if (inputChar == 'X')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M' || inputChar == 'D' || inputChar == 'C'
                || inputChar == 'L')
            {
                result = result - 10;
                continue;
            }
            else
            {
                result = result + 10;
                continue;
            }
        }
        else if (inputChar == 'V')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M' || inputChar == 'D' || inputChar == 'C'
                || inputChar == 'L' || inputChar == 'X')
            {
                result = result - 5;
                continue;
            }
            else
            {
                result = result + 5;
                continue;
            }
        }
        else if (inputChar == 'I')
        {
            inputChar = cin.peek ();
            inputChar = toupper (inputChar);
            if (inputChar == 'M' || inputChar == 'D' || inputChar == 'C'
                || inputChar == 'L' || inputChar == 'X' || inputChar == 'V')
            {
                result = result - 1;
                continue;
            }
            else
            {
                result = result + 1;
                continue;
            }
        }
        else{
            break;
        }
    }
    if (result != 0)
    {
        cout << result << endl;
    }
}
return 0;
}

Your break statement does not go out of the both while loops.您的break语句不会 go 退出两个 while 循环。 It only breaks the inner loop, not the outer one.它只打破了内部循环,而不是外部循环。 You can use a flag to tell the outer loop not to continue:您可以使用标志告诉外部循环不要继续:

int main() 
{
   bool exit_loop = false; // define a flag here
   while (!cin.eof()) {
      char inputChar;
      int result = 0;
      if (exit_loop) // check the flag here
         break;
      while (cin.get(inputChar))
      {

and while breaking set the flat to true:并在打破时将单位设置为真:

 }
 else 
 {
    exit_loop = true; // set the flag here
    break;
 }

Maybe try clearing the buffer at the end of the outer loop.也许尝试在外循环结束时清除缓冲区。 This way the longest prefix is printed and the same word isn't being used again through the iteration.这样,最长的前缀就会被打印出来,并且在迭代过程中不会再次使用相同的单词。

    if (result != 0)
    {
        cout << result << endl;
    }
    cin. ignore(10000, '\n'); //used to ignore or clear characters from the input buffer
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM