Adding White space

Hi guys, how do I add white space for every count? for example if userNum = 3 then it should print

0
1
2
3

1
2
3
4
5
6
7
8
9
10
11
12
13
  int main() {
   int userNum  = 0;
   int i = 0;
   int j = 0;

for (i=0; i <= userNum; ++i){
   while (j <= userNum){
      cout << j << endl;
      ++j;
     
   }
   
}
Last edited on
Not sure what you're asking other than missing a } your code works good and I'm guessing that's just a mistake in copping it. Your code outputs exactly that. Do you want white space between every number? If so add cout << '\n'; in your while loop. If not please be more specific where you want more white space.
Last edited on
If you mean something like this, you can play with setfill and setw in the iomanip header (example here: http://www.cplusplus.com/reference/iomanip/setfill/). It can be done without nesting loops.

1
2
3
4
0
 1
  2
   3
@Wildblue I'm not allowed to solve it using setw and showpoint. I have to use nested loops.

@joe864864 Sorry this is what it's supposed to output

1
2
3
4
0
 1
   2
     3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main() {

   int userNum  = 0;
   std::cout << "number? " ;
   std::cin >> userNum ; // accept userNum from standard input

   for( int num = 0; num <= userNum; ++num ) { // for each number 'num' from 0, 1, ... userNum 

       // first print 'num' spaces
       for( int num_sp = 0 ; num_sp < num ; ++num_sp ) {

            std::cout << ' ' ; // print one space each time through the loop
                               // this inner loop executes 'num' times in all
                               // ergo 'num' spaces are printed
       }

       // then print the number 'num' followed by a new-line
       std::cout << num << '\n' ;
   }
}
Topic archived. No new replies allowed.