Search code examples
c++for-loopwhile-loopdo-loops

Need 3 separate outputs


#include <iostream>
using namespace std;

int main (){

    int row, 
        column = 0,
        colCount = 3,
        rowCount = 3;

    //for loop
    for (column; column < colCount; column++){

        for(row = 0; row <= (rowCount - 1); row++){

            cout << column << " " << row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

        }

        cout << endl;

    }

    //while loop
    while(column < colCount){

        while(row < rowCount){

            cout << column << " "<< row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

            row++;          

        }

        cout << endl;   
        column += 1;
        row = 0;
    }

    //do while loop
    do{
        do{
            cout << column << " "<< row;

            if(row < (rowCount - 1)){
                cout << ", ";           
            }

            row++;  
        }while(row < rowCount);

        cout << endl;
        column +=1;
        row = 0;
    }while(column < colCount);

}

When commenting out 2/3 loops, each one will produce the wanted output. All together it seems to run on top of each other and adds extra output.

Current output:

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2
3 3

Wanted output:

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

0 0, 0 1, 0 2
1 0, 1 1, 1 2
2 0, 2 1, 2 2

How do I get an output out of each loop?


Solution

  • You can leave the for loop as it is:

        for (; column < colCount; column++){
            for(row = 0; row <= (rowCount - 1); row++){
                std::cout << column << " " << row;
                if(row < (rowCount - 1))
                    std::cout << ", ";
            }
            std::cout << std::endl;
        }
    

    Now column and row gets to 3 in the for-loop above, which makes it the while loop never going to be executed. So, you need to make both of them to 0.

    And, the third do-while loop always executes before any condition checks, thats why you get 3 3

    Anyways, here is a solution for your problem.

    #include <iostream>
    
    int main ()
    {
        int row, column = 0, colCount = 3, rowCount = 3;
    
        //for loop
        for (column; column < colCount; column++){
            for(row = 0; row <= (rowCount - 1); row++){
                std::cout << column << " " << row;
                if(row < (rowCount - 1))
                    std::cout << ", ";
            }
            std::cout << std::endl;
        }
        std::cout<<std::endl;
        //while loop
        column = 0;
        row = 0;
        while(column < rowCount){
            while(row < rowCount){
                std::cout << column << " "<< row;
                if(row < (rowCount - 1))
                    std::cout << ", ";
                row++;          
            }
            std::cout << std::endl;   
            column += 1;
            row = 0;
        }
        //do while loop
        std::cout<<std::endl;
        column = 0;
        row = 0;
        do{
            do{
                std::cout << column << " "<< row;
                if(row < (rowCount - 1))
                    std::cout << ", ";
                row++;  
            }while(row < rowCount);
            std::cout << std::endl;
            column +=1;
            row = 0;
        }while(column < colCount);
        return 0;
    }