Nested for-loop problem

I was given this assignment:

Write a C++ program that asks the user to enter an integer N and display N lines as follows. On line i, display i dollars ($), followed by a single pound (#) character. For example, line 0 will have 0 dollars signs followed by a pound sign. Line 1 will have 1 dollar sign followed by a pound sign. As an example, for N=6, the pattern should look as follows. Hint: Use nested for loops.

#
$#
$$#
$$$#
$$$$#
$$$$$#
$$$$$$#

This is all I have so far, and I'm unclear how to progress.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main() 
{
        int N

        cout<<"Enter an integer:"<<endl;
        cin>>N>>endl;

        for(int line = 0; N >= 0; line++)

You need one for loop that will print the specified number of lines.
Then inside that for loop, you need a nested for loop that will print the required number of $.

Line 11: your termination condition is bogus. Your loop will never stop as long as the user enters a non-negative number.

Line 9: You can't do a cin to endl.



you need a ; after int N

you need an if else statement inside your 2nd for loop.

your main needs a closing brace.

http://cpp.sh/85zjx
Last edited on
Thank you all very much. It works now!
Last edited on
Lines 13, 15: Your for loops are still bogus.

Line 16: You're printing N (which you should not be) and the wrong character. You should be printing only a single $ from the inner loop.

Line 17: You want to print the # after you've completed the inner loop. You will need {} to enclose the contents of the outer loop.

Topic archived. No new replies allowed.