【问题标题】:While loop not going back into loop虽然循环不会回到循环
【发布时间】:2015-09-14 01:26:06
【问题描述】:

我一直在做一个项目,我几乎完成了我遇到的最后一个问题,即创建一个 while 循环,不断询问用户是否要转换表达式。到目前为止,它只做了一次,然后就不再继续询问了。我知道这是一个简单的问题,我认为我的逻辑是错误的,但由于某种原因它不起作用。

这是我的主要内容:

int main(){
string answer=" ";

string expression;

while(answer!="no"){
    cout<<"Would you like to do a conversion,type yes or no:";
getline(cin,answer);

    cout<<" Enter a Post Fix expression:";


getline(cin,expression);
convert(expression);

}



return 0;

}

虽然对于我的问题来说并不是真正必要的,但我的主要代码上方的代码以防万一:

/*
 * PLEASE DO NOT PLACE A SPACE BEFORE YOU INPUT THE FIRST OPERAND
 *
 * 
 */
#include "stack.h"

void convert(string expression){
stack k; //Stores raw input string
stack c; //stores input string without spaces
stack s;//stores the string values

string post =" ";
string rightop="";
string leftop="";
string op ="";
int countop=0;// counts the number of operators
int countoper=0;// counts the number of operands
for (int i =0; i<=expression.length()-1;i++){
k.push(expression[i]);

if(expression[i] == '*' ||
  expression[i] == '+' ||
  expression[i] == '-' ||
  expression[i] == '/')
  {

    countop++;  

  }

}
c.push(expression[0]);
    int count=expression.length()/2;

     countoper=(count-countop)+1;

    if (countop==countoper){ //tells when there are too many opertors and not enough operands
        cout<<"too many operators and not enough operand"<<endl;
        exit(1);
    }
    if(countop==countoper*2){ //tells when there are too many opertors and not enough operands

        cout<<"too many operands and not enough operators"<<endl;
        exit(1);
    }
for(int i=1;i<=expression.length()/2;i++){

    c.push(expression[2*i]);

}




for(int i=0; i<2;i++){

    leftop=c.top();
    c.pop();

    rightop=c.top();
    c.pop();
    op=c.top();
    c.pop();

    post="(" + leftop + " " + op + " " + rightop + ")";

    s.push(post);
    if(count<6){
        cout<<s.top()<<endl;
    }

}

if (count>=6){

    cout<<"(";
    cout<<s.top();

    cout<<c.top();

    s.pop();

    cout<<s.top();

    cout<<")";
    }
}

【问题讨论】:

    标签: loops menu while-loop stack main


    【解决方案1】:

    我还运行了您的代码,while 循环对我有用。 也许你可以先注释掉你的 convert(expression);从那里调用和调试!

    #include <iostream>
    using namespace std;
    
    
    int main(){
    string answer=" ";
    
    string expression;
    
    while(answer!="no"){
    cout<<"Would you like to do a conversion,type yes or no:";
    getline(cin,answer);
    
    cout<<" Enter a Post Fix expression:";
    
    
    getline(cin,expression);
    /*convert(expression);*/
    
    }
    
    
    
    return 0;
    
    }
    

    【讨论】:

    • 我非常确信这是我的转换方法,因为它在注释掉后才起作用。我不知道我的转换方法出了什么问题。
    【解决方案2】:

    我尝试了从您那里复制的代码,它运行良好。这让我得出结论,我不明白问题是什么或你想要什么。

    #include <iostream>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    using namespace std;
    int main(){
        string answer=" ";
        string expression;
    
        while(answer!="no"){
            cout<<"Would you like to do a conversion,type yes or no:";
            getline(cin,answer);
    
            cout<<" Enter a Post Fix expression:";
    
            getline(cin,expression);
            // do something
        }
    
        return 0;
    }
    

    这是在做什么:

    1) 用户对第一个问题回答“否”,然后用户在第二个输入上写一些东西。退出程序。

    2) 用户回答与“否”不同的内容,然后用户写一些内容。回到第一个问题。

    你想要的行为是什么?解释得更好。

    【讨论】:

    • 我又试了一遍,还是没有回到第一个问题。我认为可能是我的转换功能将其关闭不确定。
    • 当你按照你写的运行程序时,你说它只运行一次循环。接下来发生什么?程序会终止还是卡住?我想要得到的是您的 convert() 函数是否卡在无限循环或阻塞上,或者您的 convert() 函数抛出一些导致程序终止的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-14
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多