c++ row and column problem

Hi i've been having trouble with my c++ program.
instead of repeating the digits over and over, how would
I make it so the repeating digits are blank spaces with the number of the row at the end.

So instead of my program outputting:
1
22
333

I want it to output
1
--2
---3

where the "-" are blank spaces
and so on for the number of rows the reader specifies.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  int main(){
    //Declare variables
    int num;

    //Input number of rows
    cout<<" Enter the number of rows you wish to Output"<<endl;
    cin>>num;
    if(num<=0){
            cout<<" The number cannot be a negative or 0 "<<endl;
            cout<<" Enter another number "<<endl;
    cin>>num;
    }
    for (int row = 1; row <= num; row++) {
    for (int col = 1; col <= row; col++)
    cout << row;
    cout << endl;
    }


Any help would be greatly appreciated. Thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

int main()
{
	//Declare variables
	int num;

	//Input number of rows
	cout<<" Enter the number of rows you wish to Output"<<endl;
	cin>>num;
	cin.ignore();

	while(num<=0)
	{
		cout<<" The number cannot be a negative or 0 "<<endl;
		cout<<" Enter another number "<<endl;
		cin>>num;
		cin.ignore();
	}

	for (int i = 0; i < num; i++)
	{
		cout << i + 1 << endl;
	}


	cin.ignore();
	return 0;
}
Thanks for your response, but that program outputs the numbers vertically with no spaces?
1
2
3
4
5

This program should output with spaces
1
--2
---3
----4
-----5
You were on the right track op.

Try this

1
2
3
4
5
6
7
8
for( int rows = 0; i < num; ++row )
{
    for( int columns = 0; columns < rows; ++columns )
    {
        std::cout << ' ';
    }
    std::cout << row + 1 << std::endl;
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Declare variables
	int num;

	//Input number of rows
	cout<<" Enter the number of rows you wish to Output: ";
	cin>>num;
	cout << endl;

	cin.ignore();

	while(num<=0)
	{
		cout<<" The number cannot be a negative or 0 "<<endl;
		cout<<" Enter another number: ";
		cin>>num;
		cout << endl;
		cin.ignore();
	}

	for (int i = 0; i < num; i++)
	{
		cout << setw(i + 1) <<  i + 1 << endl;
	}


	cin.ignore();
	return 0;
}
The op hasn't learned about manipulating the output stream yet probably so I'd suggest he just stick with the row/column for loop method.
Thank you all for your replies, I was able to solve this.

I have one question,

in giblit's response he uses

std::cout << ' ';

I noticed if i changed it to just a cout statement

cout<<' '<<endl;

It screws up the whole program. Why does that happen?
why is std::cout used and not just a simple cout<<
Last edited on
the std:: in front is called scoping it I put that because I didn't include the std namespace. The reason yours is messed up is because you put an endl; You don't want to put an endl; You are outputting the spaces before the number.
Topic archived. No new replies allowed.