Help! Nested looping program help!

Despite all I have read and researched, I am finding it extremely difficult to complete this program. Anyone that can help me in any way would be most appreciated!


Write a program that uses loops that displays the below patterns. Must use nested for looping code. Must use either a single + or a single space on each loop.

+
++
+++
++++
+++++
++++++
+++++++
++++++++
+++++++++
++++++++++

++++++++++
+++++++++
++++++++
+++++++
++++++
+++++
++++
+++
++
+

This is all I have, and it's not right. I just can't figure this out for the life of me!!

#include <iostream>
using namespace std;

void main()
{
int row, col;
int print = 0;

for( row = 0; row < 4; row++)
{
for(col = 0; col < 4; col++)
{
if( print == col )
{
cout << "+";
}//end if
else
{
cout << " ";
}//end else

}//end inner for

print++;
cout << endl;
}//end outer for
system("pause");
return 0;
}//end main

/* proof
+
S+
SS+
SSS+
*/
((((((THE S'S BEFORE THE + SIGN ARE NOT ACTUALLY IN THE PROGRAM/PROOF BUT WHEN I COPIED AND PASTED IT KEPT LEAVING OUT THE SPACES. SO THE "S" REPRESENTS THE AMOUNT OF SPACE, BUT IT IS NOT ACTUALLY IN THE PROGRAM))))))



Again, I know what I have so far is not even close to what the program is asking me to do. I'm just so confused. Any help/ideas/suggestions would be most appreciated sooner the better please & thanks!
Last edited on
Do it with two sets of nested loops. One counting up, one counting down.

Here's the one going up. You do the one going down. For the one going down, you still need the outer loop ("i" loop) to count from 0 to 9, but you need the inner loop ("j" loop) to count down from 10 to i each time round.

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i = 0; i < 10; ++i)
{
  for (int j = 0; j <= i; ++j)
  {
    cout << '+';
  }
  cout << endl;

  if ( i == 9)
  {
    cout << endl; 
  }
}
Last edited on
This is the message I get with your code...

'Lab04-02PatternDisplays.exe': Loaded 'C:\CP107\CP107\Lab04-02PatternDisplaysSolution\Debug\Lab04-02PatternDisplays.exe', Symbols loaded.
'Lab04-02PatternDisplays.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Lab04-02PatternDisplays.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Lab04-02PatternDisplays.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Lab04-02PatternDisplays.exe': Loaded 'C:\Windows\SysWOW64\msvcp100d.dll', Cannot find or open the PDB file
'Lab04-02PatternDisplays.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Cannot find or open the PDB file
The thread 'Win32 Thread' (0x1ac8) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x1528) has exited with code 0 (0x0).
The thread 'Win32 Thread' (0x2328) has exited with code 0 (0x0).
The program '[5124] Lab04-02PatternDisplays.exe: Native' has exited with code 0 (0x0).

& I'm not sure what it is I'm doing wrong, I've done this 100 times, at the end of my code I put...

{
system("pause");
return 0;
cout << endl;
}

so that I can see the actual code, and every time I debug it does not pause, the screen goes away just as fast as it appeared. I've never had this problem before? I tried putting the system("pause") in a few different places, outside of different brackets, and it doesn't make a difference. Any suggestion?
Those messages indicate that the program ran normally and finished with a return 0. Show all the code you're building.
#include <iostream>
using namespace std;

for (int i = 0; i < 10; ++i)
{
for (int j = 0; j <= i; ++j)
{
cout << '+';
}
cout << endl;

if ( i == 9)
{
system("pause");
return 0;
cout << endl;
}


***This is the only code I have, which is just what you gave me. I wanted to put it in and see how it would run. But it is not pausing, I cannot see what the code is actually creating. I'm very confused because I've put the system("pause") in at the bottom of every code and in the exact same spot, it always allows me to see my running program, this time no matter where I put it it is exiting as soon as it opens.
Hi,

You are missing one very fundamental and basic thing for a C or C++ program. Moschops posted a partial program. Hint: you had it in your original code, but it doesn't have void in front of it, should be int

Please always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/
You are clearly cargo-cult programming. I'm impressed that you got this far without understanding even the most basic layout of a C++ program. Stop what you're doing, go back to the start and learn about the function named main.
I understand why you might think that, but that's not the case at all. I have been sick the past few days and maybe not thinking as clearly. I did forget to put in the main function, not because I didn't know it was suppose to be there, simply because I overlooked it. I did copy & paste the code you gave to me, but not because I wanted to just use your code & not learn about the program I am trying to create. I was just trying to run the code you gave to me to see what the output would look like. I know that wasn't the whole code, I'm not looking for someone to give me the answers just looking for help. What I have so far still isn't working, I took your code & did what I thought you had said to do, but I guess I am misunderstanding.


This is my code so far,

#include <iostream>
using namespace std;

int main()

for (int i = 0; i < 10; ++i)
{
for (int j = 0; j <= i; ++j)
{
cout << '+';
}
cout << endl;

if ( i == 9)
{
cout << endl;

for (int i = 0; i > 10; ++i)
{
for (int j = 0; j >= i; ++j)
{
cout << '+';
}
cout << endl;

if ( i == 9)
{
cout << endl;

system("pause");
return 0;
}


I am still having a problem with the output screen disappearing. Like I said above, I have used system("pause") 100 times in the same place in all my code's, but this time it doesn't seem to be working. The screen goes away just as quickly as it appears so I'm not able to see what my output actually looks like. If anyone has an idea what is going on for this problem I'm having I would greatly appreciate it!!
There's a problem with placement of braces, and indentation/formatting would make that stand out.
It's unlikely that the code as shown above would compile at all.
You need an opening brace "{" after int main().
You've introduced 2 unnecessary "if (i == 9)" clauses that have an opening but no closing brace.
neither of your "for (int i = " clauses has a closing brace.
The body of your last 2 "for" clauses will never run - both are wrong, and if you fix the first one, the second will run forever.

Look at your code.

1
2
3
4
5
6
7
if ( i == 9)
{
cout << endl; 

system("pause");
return 0;
}


This pause you want will only happen if i is 9 at the end. Why have you done this? Don't you want this to always pause? Look at your code.
Topic archived. No new replies allowed.