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>
usingnamespace std;
int main()
{
longint 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?
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++) 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.
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)