While and For Statements

I have this code running without any errors, but it is not displaying the correct way and I can't figure it out! I have tried so many different things! This is my code...

#include <iostream>

using namespace std;

int main ()
{

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

while (outer < 4 )
{
cout << outer;
cout << " ";
outer += 1;
cout << endl;

{
for (int nested = 1; nested <= 2; nested += 1)

cout << nested;
cout << " ";
nested += 1;
}

} //end while

system("pause");
return 0;
} //end of main function

My output should look like
1 2 3
1 2 3

but I keep getting??
1
12 2
12 3
12
You will not get 1,2,3 on the same line for outer because you put endl at the end, which creates a new line.

Do not do this:

1
2
3
4
//declare variables
int nested = 1;

for (int nested = 1; nested <= 2; nested += 1) 


You are declaring nested two times. You are making a local variable in the for loop, but once outside the parameters, the compiler doesn't know which one YOU want to use.

If you want a newline after 1, 2, and 3, end the while loop after it's done with outer. Now, print a newline and begin the for loop.

Also, please use the [code] bbcode and indent.
Last edited on
be careful that you distinguish between string quotes and single character apostrophes. If you want to output a space, do this:
 
std::cout<<' ';  //notice that its not " " but ' '.  Important. 


Also consider using nested++ instead of nested +=1. Its a little easier to read and is more the standard.

does this actually compile? As yayu said you are redefining a variable. this should cause a compile time error.

If you want your ouput to look like this:
1 2 3
1 2 3

You need to not nest the loops.
Last edited on
Ok I changed my for statement to

for (nested = 1; nested <= 2; nested += 1;)

I am not sure how to use the bbcode?

use [] with the word code within to open the block.
use [] with the word /code within the close the block.
Is the semicolon at the end a typo? If not, you should get a compiler error. Anyways, is it working as intended now? If not, post your new code.

For BBCode, look on the right of the reply text box. The "<>" button is the code bbcode. Put your code inside of there.
I still didn't figure out the BBCode...but this is my code that I have and it is still showing
1 2 3
1
I have been changing just about everything to see if I could find a fix

#include <iostream>

using namespace std;

int main ()
{

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

while (outer < 4 )
{
cout << outer;
cout << " ";
nested += 1;
outer += 1;

}
for (nested = 1; nested <= 2; nested += 1)
{
cout << endl;
cout << nested;
cout << " ";
nested += 1;
outer += 1;
}

//end while

system("pause");
return 0;
} //end of main function
Well, there's a lot of extra code in there that isn't needed.
But I'd suggest you focus on this, which I posted a while ago:
the inner loop should repeat 3 times and the outer loop should repeat 2 times.


Now look at the code you have above. In fact you no longer have an outer and an inner loop. Instead there are two completely independent loops, one after the other. (I think some of the previous suggestions pointed in that direction, but in my opinion that was misguided and wrong advice).
Last edited on
the inner loop should repeat 3 times and the outer loop should repeat 2 times.


Chervil is right. Sorry about that.

If you look at the two lines:
1 2 3
1 2 3

You'll see that they are repeating, and the only thing that is changing is the position where it starts. So the outer loop ends the line, and the inner loop outputs the numbers.

while the outer loop is less then 3
for each nested and nested is less then 4 output the value of nested plus a space.
end the line in the outer loop.
increment outer.
loop

1
2
3
4
5
6
while (outer < 3)
{
     for(nested; nested < 4; nested++)
          std::cout<<nested<<' ';
     outer++;
};


Last edited on
ooh, I assumed he/she wanted them to be separate. Don't know why, sorry.
Last edited on
Topic archived. No new replies allowed.