Posttest loop problems

I am needing this code to display
1 2 3
1 2 3
as my out put and right now I am only getting one set of numbers instead of two. Any help?

#include <iostream>

using namespace std;

int main ()
{

//declare variables
int nested = 1;

//repeat while nested < 4;
cout << nested;

do //begin loop
{
cout << nested;
cout << " ";
nested += 1;
} while (nested < 4);

system("pause");
return 0;
} //end of main function
What you do is what you get. You have only one loop that displays numbers from 1 to 3 inclusively.
1
2
3
4
5
6
7
8
for(int i=0; i<2; i++){
do //begin loop
{
cout << nested;
cout << " ";
nested += 1;
} while (nested < 4);
}
I received some help on it at school and this code is displaying 1 2 3 4, but I actually need
1 2 3
1 2 3
My teacher said we will not need a for loop, only two do while loops.


#include <iostream>

using namespace std;

int main ()
{

//declare variables
int nested = 1;
int outer = 1;

do //begin loop
{
do //begin loop
{
cout << nested;
cout << " ";
nested += 1;
} while (nested < 4);
outer += 1;
} while (outer <= 2);
//end while

system("pause");
return 0;

} //end of main function
A pair of for loops would probably be clearer.

Anyway, you need to reset nested = 1 after the inner loop completes.
I fixed that, but I know have an output that is giving me numbers over and over. So I must have an infinite loop, right?


#include <iostream>

using namespace std;

int main ()
{

//declare variables
int nested = 1;
int outer = 1;

do //begin loop
{
do //begin loop
{
cout << nested;
cout << " ";
nested += 1;
} while (nested < 4);
nested = 1;
} while (outer <= 2);
//end while

system("pause");
return 0;

} //end of main function
Yes, you have an infinite loop. When you added that "nested = 1" statement, you wiped out the previous "outer += 1" statement. You didn't want to do that. You want both statements.
Oh ok I've got it now! Thank you! One more question its showing it as
1 2 3 1 2 3
where am I needing my endl at? I tried it many places and its spacing it at the wrong places. I tried after my cout << nested?
I've got it thank you for your help!
Topic archived. No new replies allowed.