d) Use a for loop and the fill() manipulator

Use a for loop and the fill() manipulator to print the following:
1*2**3***4****5*****6******7*******8********9*********10 and so on, up to 100.

I am trying to figure this out, I can get the numbers to print by themselves, and I can print 100 *'s and the # 100, However I can't make it do what I need it to do. Here is the code I have, can someone point me in the right direction.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    long int i=0;

    for (i=0; i < 100; i++)

        cout << i;
        cout.width(i);
        cout.setf(ios::right);
        cout.fill('*');
         
         
    return 0;
}
Think about adding another integer, one that is compared to your 'i' integer in the second part of the for(...) loop. Maybe a second nested for(...) loop?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    long int i=0;
    int w;

    for (i=0; i < 100; i++)

             for(w=0; w<100; w++)

        
        cout.width(w);
        cout.setf(ios::right);
        cout.fill('*');
         cout << i << w;
         
    return 0;
}



I tried this a while ago and got the same result.
Think more like this:


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
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
    long int i=0;
    int w,x;

    for (i=0; i < 100; i++)
{
    
             for(w=0; w<=i; w++)
{
        cout.fill('*');
        cout.width(i);
 
}
cout << i << '\n';
w = 0;
}
 
         
    cin>>x;
    return 0;
}


I inserted a new line for readability so that you can confirm that it is doing what it is supposed to.

EDIT: Displayed output in the wrong order.

RE-EDIT: I spoke too soon my code is coming up one star short...
Last edited on
Ok I cheated a little bit but here's what I came up with and again I used the new line character to make the output more readable:

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

int main()
{
    long int i=0;
    int w,x;

    for (i=1; i < 100; i++)
{
    cout << i; 
    cout.width(i+1);
    cout.fill('*');
    cout << '\n';
}
 
         
    cin>>x;
    return 0;
}
Last edited on
Thank you I think I got it. I was wondering what the cin>>x; is for I tried it without it and it works but I am curious?
cin>>x; is my lazy bones way of holding the window open so that I can inspect the out put. There are much better ways and I meant to replace it before posting my code but I didn't. Also the for loop on line 10 should read:

 
for(i=1; i <= 100; i++)


Otherwise it technically cuts off at 99
Last edited on
for(i=1; i <= 100; i++) by removing the = sign it goes to 100. Thank you for your help!! I have been doing so many i/o problems I forgot the basics, or I need to do something else for a while.
I got that backwards I did add an = sign to make it work, thanks again.
Will this work?

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

using namespace std;

int main()
{
    long int i=0;
    cout.fill ('*');

    for (i=1; i<=100; i++)
    {

     cout.width (i);
     cout << i;

    }
    return 0;
}
Do you need to #include <iomanip>? It's not part of <iostream>?
Yup without compiling it myself I would say that should work, except I couldn't get cout.width(i) to return the right number of '*' symbols so I cheated and added a +1. Other then that it's basically what I did except in a different order, yours is technically better because mine sets the cout.fill('*') at every interation of the loop but it doesn't need to be done.
I think the question is kind of ambiguous because it's so hard to count the stars... I don't know if it's asking to print a field of i characters padded with '*' and integer i within that field... or is it asking to print integer i and then print i number of stars after it.

If it's the former, then mine works. But if it's the latter, then you have to add

1
2
if (i > 9) cout.width (i+1);
if (i > 99) cout.width (i+2)


after the first cout.width (i);.
Topic archived. No new replies allowed.