While and Do While statements

I am needing to rewrite this code to use a while statement for the outer loop and a for statement for the nested loop. Any help is appreciated! Thanks!

int nested = 1;
for (int outer = 1; outer <=2; outer += 1)
{
do //begin loop
{
cout << nested;
cout << " ";
nested += 1;
} while (nested < 4);
cout << endl;
nested =1;
} //end for
closed account (oN3AqMoL)
First of all, use the <>(code) brackets you see under format so your code looks clearer.

Second, some of the syntax you use is OK but its not used commonly. Such as:
1
2
3
i+=1;
//instead of
i++;


Anyways, moving further, this is NOT www.WRITEMYCODE4ME.com. While we provide answers and point people in the right direction we dont wait for someone to give us their code.

Even if we did; what is the purpose of this code? Simply to use the for loop inside a while loop????

Sounds like a homework assigment to me.
This is what i have so far but no luck.

#include <iostream>

using namespace std;

int main ()
{

//declare variables
int nested = 1;

while (nested < 4 )
{
for (int nested = 1; nested <= 2; nested += 1)
cout << endl;
{
cout << nested;
cout << " ";
nested += 1;

} //end for

nested = 1;
} //end while

system("pause");
return 0;
} //end of main function
What happened to the variable outer?
I'd suggest it is needed in the new version of the code too.
Oh ok! Thank you! That helped a lot! Now all I need is to display it the right way. I have it looking like this

1
1

2
2

3
3

and its suppose to be

1 2 3
1 2 3

this is what my code is now after adding the outer variable

#include <iostream>

using namespace std;

int main ()
{

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

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

} //end for

cout << nested;
nested += 1;
} //end while

system("pause");
return 0;
} //end of main function
Check the condition which terminates each loop. The number of iterations of the outer and inner loop must be the same in the new version as in the original.
I'm not exactly sure what mean by that but I'm thinking you mean the default number? Such as While ( outer <4)? Is that what you mean? I'm extremely new to this and I am still trying to figure everything out.
It is now giving me this as my output
1 1
2 2
3 3

I'm not having any errors but it is giving me all of this in my output box when I run the program....I have no idea what all this is meaning :/


>------ Build started: Project: Ch8Pencil3 Project, Configuration: Debug Win32 ------
1> Ch8Pencil3.cpp
1>f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(20): warning C4258: 'nested' : definition from the for loop is ignored; the definition from the enclosing scope is used
1> f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(17) : definition of 'nested' ignored
1> f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(12) : definition of 'nested' used
1>f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(22): warning C4258: 'nested' : definition from the for loop is ignored; the definition from the enclosing scope is used
1> f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(17) : definition of 'nested' ignored
1> f:\ch8pencil3\ch8pencil3 solution\ch8pencil3 project\ch8pencil3.cpp(12) : definition of 'nested' used
1> Ch8Pencil3 Project.vcxproj -> F:\Ch8Pencil3\Ch8Pencil3 Solution\Debug\Ch8Pencil3 Project.exe
That sounds like you declared two different variables both named "nested".
The compiler gives a warning as the code is technically valid, but it might do something different to what you intended.

As for the overall problem, in the original code, the outer loop is controlled by the variable "outer" which starts at 1, then the next time around it is 2. when the value reaches 3, the outer loop terminates.

The inner loop is controlled by the variable "nested", which varies from 1 to 3, when it reaches 4, the inner loop terminates.

Although the type of loop is changed, the required behaviour needs to be the same, that is the inner loop should repeat 3 times and the outer loop should repeat 2 times.
That's what I was thinking as well. I believe that I have my nested variable right though? I'm not seeing the problem with it.
Thank you for explaining this to me.
This is my code now...my output is
1 2 3 1


#include <iostream>

using namespace std;

int main ()
{

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

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

} //end while

system("pause");
return 0;
} //end of main function
Topic archived. No new replies allowed.