Doubling amounts of "*" per line?

Greetings,

I need to make a program that outputs 5 lines of asterisks, doubling the amount of asterisks per line. Like so:
*
**
****
********
****************

I'm working with this code, hoping to modify it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int main()
{

    string const mark = "*";

    string rowOfMarks = "";

    for( int i = 1;  i <= 5;  ++i )
    {
        rowOfMarks += mark;
      cout << rowOfMarks << std::endl;
    }
    return 0;
}


Ive fiddled a bit with it but to no avail. Any suggestions?

Best regards,
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
#include <string>
#include <iostream>

#define numberoftimestorun 6

using namespace std;

int main()
{
    string const mark = "*";
    string rowOfMarks = "";
    int numbertoprint = 1;
    rowOfMarks += mark;
    cout << rowOfMarks << endl;
    for( int j = 1; j < numberoftimestorun; j++)
    {
       numbertoprint += numbertoprint;
       for( int i = 1;  i < numbertoprint;  i++ )
          rowOfMarks += mark;
    cout << rowOfMarks << endl;
    }
    system("pause");
    return 0;
}


Please try your best to understand it, and ask any questions you might have, instead of copypasta.
Thanks for your reply Malachi!

I've tried commenting the code as how I understand it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  string const mark = "*";
  //defines string constant mark as *
    string rowOfMarks = "";
    //defines string rowOfMarks as "" (an empty space)
    int numbertoprint = 1 ;
    // number of times to print = 1
    rowOfMarks += mark;
    //Adds * to the empty space
    cout << rowOfMarks << endl;
    //Outputs the rows of *'s to the screen
    for( int j = 1; j < numberoftimestorun; j++)
    {// A for loop that starts from 1 and runs until it reaches 6, adding 1 to j on each loop
numbertoprint += numbertoprint;
//Adds 1 to numbertoprint = 2
       for( int i = 1;  i < numbertoprint;  i++ )
       //A for loop that starts at 1 and runs until it reaches 2, adding 1 to 1 on each loop
rowOfMarks += mark;
//adds * to the empty space
    cout << "\n" << rowOfMarks << endl;
    //Outputs the rows of *'s to the screen
    }
    system("pause");
//Pauses system to let me see what happens on the screen 



First loop:
*, **, ***, ****, *****, ******

Second loop:
Is meant to double the first loop(?). However, it somehow adds after doubling.
Last edited on
Okay, so looking at my output I missed one important little detail, so I've added it to the code... with comments

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 <string>
#include <iostream>

#define numberoftimestorun 6 //this is how many times we will double

using namespace std;

int main()
{
    string const mark = "*";
    string rowOfMarks = "";
    int numbertoprint = 1; //how many asterixes to print
    rowOfMarks += mark;
    cout << rowOfMarks << endl;
     int lastnumberprinted = 1; //how many asterixes we printed last time
    for( int j = 0; j < numberoftimestorun; j++) //while we've run less time than we're supposed to
    {
       int lastnumberprinted = numbertoprint; //record how many we printed last time
	   numbertoprint += numbertoprint; //doubles how many to print
       for( int i = lastnumberprinted;  i < numbertoprint;  i++ ) //while we've printed less than we're supposed to we add an asterix, starting from how many we printed last time.
          rowOfMarks += mark;
    cout << rowOfMarks << endl;
    }
    system("pause");
    return 0;
}


before I'd missed how many we'd printed before being a part of it, so it was printing the new doubled string as well as the old one. I saw it do 2, then 4, then stopped looking to see if it worked right in my haste to program my own things...
Last edited on
Malachi: How about next time you let people solve their own homework?
Thank you very much Malachi. I really appreciate your help.

Helios: I'm following a tutorial and it asks the reader to figure out small practical examples. I'd like at least to get some of it before moving on. Thanks to Malachi I learned something I could not figure out for myself, given the supplied info.

However, what I can figure out for myself is that your post contains nothing of use to anyone.
it asks the reader to figure out small practical examples

I suggest you follow what the tutorial said to do.

And @Malachi: Yeah, please don't just give solutions, give hints so they can find it out on their own.
This board has gotten a lot stricter hehe - I like it!
That seems like a lot of work. Why not just:

1
2
3
4
5
6
string line( "*" );
for( int i = 0; i < 5; ++i )
{
    cout << line << endl;
    line.append( line );
}
Last edited on
I know this is a late post, but all you needed to do was add 'mark' back to itself instead of adding it to a second variable.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main(void){
	string mark = "*";
	for( int i = 1;  i < 6;  ++i ){
		cout << mark << endl;
		mark += mark;
	}
	return 0;
}
Topic archived. No new replies allowed.