2015-10-18 41 views
0

該程序應該獲得用戶輸入(x)的任何數字的倍數,直到達到(y)。所有三個循環都能正常工作,但是當一起使用時,我們會得到前兩個循環,但第三個循環不會輸出。此外,我想要輸出每個循環的輸出在一個新的行。我的問題是我能做些什麼來使我的輸出分成三行,爲什麼不是我的第三個循環輸出任何東西。在一個程序中使用do-while循環

#include <iostream> 
#include <string> 
#include <cstdlib> 
using namespace std; 

bool error(const string & msg); 

int main(){ 
    unsigned x, y; 
    cout << "Give me a number you want the multiples of and give me the max limit: "; 
    cin >> x >> y || error("Input Failure: "); 
    if (x == 0) error("Input can't be 0"); 

    unsigned increment = x; 
    x = 0; 

    while(x < y) 
    { 
     cout << x << ", "; 
     x += increment; 
     if (x > y) break; 
} 
    cout << endl; // This was the other problem. I kept putting it inside the loop 
    for(int x; x < y; x += increment){ 
     cout << x << ", "; 
     if (x > y) break; 
    } 

    cout << endl; // This was the other problem. I kept putting it inside the loop 
    x = 0; // This is what originally was wrong 
    do{ 
     cout << x << ", "; 
     x += increment; 
     if (x > y){ 
      break; 
     } 

    }while (x < y); 

} 

bool error(const string & msg){ 
     cout <<"Fatal error: " << msg << endl; 
     exit(EXIT_FAILURE); 
} 
+0

你的問題是什麼? –

+0

你得到的輸出是什麼?有沒有運行時錯誤? –

+0

當我輸入5和40我得到0,5,10,15,20,25,30,35,0,5,10,15,20,25,30,35,40和我沒有收到任何錯誤。這很奇怪,因爲當我單獨運行第三個循環時,我得到0,5,10,15,20,25,30,35 – Flow

回答

1

好吧,這並不奇怪。當您單獨運行第三個循環時,變量x不會被其他兩個循環修改。實際上,你的第三個循環正在執行。第一個循環打印0,5,...,35,然後第二個循環打印0,5,...,35(注意for循環的條件中的<),第40個循環打印第三個循環,然後在它打印之後,while的條件是false,因爲40 == 40並且循環結束。

+0

我看到你說的確實正在執行,我看到在前兩個循環中,x = 0。在第二個循環中執行的第三個循環x中。我在do循環之前放了x = 0,並且它現在完美地執行了,現在我只需要知道如何讓它們在各自的行上有它們。但是謝謝你! – Flow

+0

沒問題!你個人的意思是什麼?你需要在一行中輸出每個循環?如果是這種情況,只需在第二個和第三個循環開始之前執行'cout << endl'。 – nbermudezs

+0

HAHA ughhh我花了至少一個小時試圖找出答案。我一直把它放在循環的內部,我會在垂直線上得到5 10 15個別的線。非常感謝你,我永遠不會忘記那個 – Flow

0
unsigned increment = x; 
x = 0; 

while(x < y) 
{ 
    cout << x << ", "; 
    x += increment; 
    if (x > y) break; 
} 

for(x = 0; x < y; x += increment){ 
    cout << x << ", "; 
    if (x > y) break; 
} 
x=0; 
do{ 
    cout << x << ", "; 
    x += increment; 
    if (x > y){ 
     break; 
    } 

}while (x < y); 
+0

謝謝!我之前通過你的評論解決了它,但是我很欣賞代碼 – Flow

0

for循環不會輸出,因爲你重新定義x並沒有給它一個新的價值。我不確定爲什麼你使用三個循環,幾乎做同樣的事情,除非你只是好奇他們是如何工作的。例如,你可以做這樣的事情,用不同的循環輸出相同的東西:

#include <iostream> 
using namespace std; 

int main() 
{ 
    unsigned counter, increment, limit; 

    cout << "Give me a number you want to increment by of and give me the max limit: "; 
    cin >> increment >> limit; 

    //Check to see if the input stream failed 
    if(cin.fail()) 
    { 
     cout << "Fatal Error: Input Failure" << endl; 
     return -1; 
    } 

    if(increment == 0 || limit == 0) 
    { 
     cout << "Input Cannot Be 0" << endl; 
     return -1; 
    } 

    if(limit <= increment) 
    { 
     cout << "Limit <= Increment" << endl; 
     return -1; 
    } 

    cout << "First Loop" << endl; 
    counter = 0; //Set counter to 0 for 1st loop 
    while(counter <= limit) 
    { 
     cout << counter << ", "; 
     counter += increment; 
    } 
    cout << endl; 

    cout << "Second Loop" << endl; 
    //for loop resets counter for 2nd loop 
    for(counter = 0; counter <= limit; counter += increment) 
    { 
     cout << counter << ", "; 
    } 
    cout << endl; 

    cout << "Third Loop" << endl; 
    //Reset counter again for 3rd loop 
    counter = 0; 
    do{ 
     cout << counter << ", "; 
     counter += increment; 
    }while (counter <= limit); 

    return 0; 
} 
+0

哇我遲到了遊戲:P – RitterS

+0

哈哈是啊遲到遊戲我不得不使用所有三個循環做家庭作業,但我會看看你的代碼只是爲了進入其他人的大腦,看看他們是如何計算出來的 – Flow