在一个程序中使用for,while,do-while循环

时间:2015-10-18 03:46:29

标签: c++ loops for-loop while-loop

该程序应该得到用户输入(x)的任何数字的倍数,直到达到(y)。所有三个循环都能正常工作,但是当一起使用时,我们得到前两个循环,但第三个循环不输出。另外,我想在一个新行中输出每个循环的输出。我的问题是我能做些什么来使我的输出分三个单独出来,为什么我的第三个循环输出任何东西。

#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);
}

3 个答案:

答案 0 :(得分:1)

嗯,这并不奇怪。当您单独运行第三个循环时,其他两个循环不会修改变量x。 事实上,你的第三个循环正在被执行。第一个循环打印0,5,...,35,然后第二个循环打印0,5,...,35(注意&lt;在for循环的条件下),40由第三个循环打印,然后在打印之后,while上的条件为false,因为40 == 40并且循环结束。

答案 1 :(得分: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);

答案 2 :(得分: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;
}
相关问题