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
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????
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.
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.