2015-10-21 69 views
1

我的主要功能保持運行的程序,直到用戶輸入退出

int main(){ 
Postfix post; 
char size=100; 
char infix[size]; 
string get_input=" "; 


    cout<<"Input Infix"; 
    cin>>infix; 
    int size=strlen(infix); 
    char postfix[size]; 
    post.infix_to_postfix(infix, postfix, size); 
    cout<<"\nInfix Expression is:"<<" "<<infix; 
    cout<<"\nPostfix Expression is:"<<" "<<postfix; 
    cout<<endl; 

程序轉換綴以與棧後綴符號。我的問題是,有沒有辦法循環直到用戶不想。 與此類似

int n; 
    cin>>n; 

    while (n!=0){ 
    // keep doing whatever 
} 

回答

1

你可以這樣做:

while(cin >> infix){ 
    // your code here.... 
} 

程序將停止接受用戶輸入時用戶按下「CTRL + Z」

1

下面是兩種方法可以做到這個.. 首先是建議使用std::string它會讓你的生活變得輕鬆。儘量把它列入你的編碼習慣..

while (std::getline(std::cin, line) && !line.empty()) 
{ 
    //write your logic here 
} 

爲了打破環用戶必須按enter

方式二

std::string str; 
while(std::cin>>str) 
{ 
//write your logic here 
} 

爲了打破循環按ctrl+D

相關問題