2017-03-28 61 views
1

我很難搞清楚如何在C++中結束do-while循環。處理x個數據集後,我需要停止循環。也就是說,沒有設定的數據量,用戶確定他們何時完成輸入值。用假值在C++中結束while while循環

當用戶決定他們完成輸入數據時,我需要能夠停止我的do-while循環。

我的主程序應該在一個循環中讀入並處理3個整數值的組,直到數據集合結束。

對於3個值的每個組,主程序將打印這些值,然後將3個值作爲參數發送到另一個函數。

這是我到目前爲止有:

#include <iostream> 
using namespace std; 

int main() { 
    int temp1, temp2, temp3; 

    do { 
     cin >> temp1 >> temp2 >> temp3; 
     cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl; 
    } 
    while (****this is where I need help!*****); 

    return 0; 
} 

我的問題是,我有多個輸入值,所以我怎麼知道while循環應該以停止處理值什麼條件?

+1

所以*問*用戶「你想繼續?」,如果沒有,然後退出循環。 –

+3

忘記第二個代碼。從用戶的角度來看,您希望程序如何運作? –

回答

1

甲簡單的解決方案將被引入,基於值(例如Y或N)的新的輸入進行休息操作

char temp4 
std::cin>>temp4; 

if(temp4!='y') 
break; //exits the loop 

或如果你想要做在

while(temp4=='y'); 
+0

如果我輸入'Y',則程序退出。不是我期待的。 – 1201ProgramAlarm

1

介紹的條件它,直到EOF,你需要break

do { 
    cin >> temp1 >> temp2 >> temp3; 
    if (!cin) 
     break; 
    cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl; 
} 
while (true); 

或:

while (true) { 
    cin >> temp1 >> temp2 >> temp3; 
    if (!cin) 
     break; 
    cout << "The 3 values are: " << temp1 << " " << temp2 << " " << temp3 << endl; 
}