【问题标题】:Program doesn't continue loop [closed]程序不继续循环[关闭]
【发布时间】:2014-09-22 18:48:47
【问题描述】:

当我的程序第一次进入while(!looking) 循环时,它会执行任务,但之后它不会继续获取单词并翻译它们。需要一些帮助来弄清楚为什么它没有运行。

while (cin.good()){
    getline(cin, lines); 
    while (!looking) {
        spot = lines.find(" "); 
        if (spot == -1){ 
            looking = true;
            spot = lines.length( ); 
        }
        line = lines.substr(0, spot); 
        TP1stLetter(line); 
        if (!looking)
            lines = lines.substr(spot + 1, lines.length( ) - spot + 1);
        }
        cout << endl;

//while( cin.good() ) {
    //getline (cin, line);
    //for(x = 0; x < line.size(); x++) {
        //char letter = line[x];
        //if (letter == 'a' || letter == 'e' || letter == 'i' 
        //      || letter == 'o' || letter == 'u'){
            //cout << letter;
        //}
    //}
    }
 }

【问题讨论】:

  • 你试过逐行调试吗?
  • 您希望我们“解析”您的整个工具吗?
  • 您似乎没有将 looking 重置为 false
  • @Desolator 我不确定你的意思?
  • @John 只是意味着您的程序非常庞大,需要询问。您最好只询问其中不起作用的部分。

标签: c++ loops while-loop


【解决方案1】:

只需在cout 语句后添加一行代码如下:

if (mode == TOPIG) {
cout << "TOPIG MODE" << endl;
while (cin.good()){
    getline(cin, lines); 
    while (!looking) {
        spot = lines.find(" "); 
        if (spot == -1){ 
            looking = true;
            spot = lines.length( ); 
        }
        line = lines.substr(0, spot); 
        TP1stLetter(line); 
        if (!looking)
            lines = lines.substr(spot + 1, lines.length( ) - spot + 1);
    }
    cout << endl;
    looking = false;

    //while( cin.good() ) {
    //getline (cin, line);
    //for(x = 0; x < line.size(); x++) {  
    //char letter = line[x];
    //if (letter == 'a' || letter == 'e' || letter == 'i'
    //    || letter == 'o' || letter == 'u'){
    //cout << letter;
    //}
    //}
  }
}

【讨论】:

    【解决方案2】:

    我能想到的2件事,希望对你有帮助 - 您是否应该在 while(!looking) 循环中的任何时候设置looking = false? - 您是否在命令行中的参数字符串周围添加引号 像这样 YourExe.Exe these words are five arguments

    但是你需要引号来创建一个字符串 YourExe.Exe "this is one argument"

    【讨论】:

    • 参数是判断topig还是frompig,但是在topig上工作;我希望通过 cin.good 和 getlines,我能够不断地要求翻译行。你的意思是“你应该”还是“你不应该”?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-11
    • 1970-01-01
    • 2011-08-19
    相关资源
    最近更新 更多