該程序應該獲得用戶輸入(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);
}
你的問題是什麼? –
你得到的輸出是什麼?有沒有運行時錯誤? –
當我輸入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