Can't figure out how to add space.

So the problem wants me to write code that gives me output like this:
0
*1
**2
***3
There is a Blank Space Here.

I keep getting the same output of:
0
*1
**2
***3
**** *Missing the space*

Here is my code:

#include <iostream>
using namespace std;

int main() {
int userNum = 0;
int i = 0;
int j = 0;

for(i = 0; i<= userNum; i++){
cout << i << endl << " " << "";

for (j=0; j< i; j++){

cout << " ";

}
}
return 0;
}

How do you add the blank space in?
Last edited on
Why do you have two for loops?

Try this...
1
2
3
4
for ( int x = 0; x <= userNum; x++ )
{
  cout << x << endl;
}
Hi,

The original code won't work: userNum is 0 and is not modified.

Also:

for ( int x = 0; x <= userNum; x++ )

If userNum was set to some value, the loop will execute userNum +1 times. The normal idiom is :

for ( int x = 0; x < userNum; x++ )

To add a space at the end, just do this:

1
2
3
4
5
for ( int x = 0; x < userNum; x++ )
{
  std::cout << x << std::endl;
}
std::cout << " ";


Hope all is well :+)
Sorry guys. When I put my example in, I thought it would show the spaces.
So the output is suppose to be:
0
*1
**2
***3
*****
**Spaces*
Topic archived. No new replies allowed.