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.
#include <string>
#include <iostream>
#define numberoftimestorun 6 //this is how many times we will double
usingnamespace 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...
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.
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>
usingnamespace std;
int main(void){
string mark = "*";
for( int i = 1; i < 6; ++i ){
cout << mark << endl;
mark += mark;
}
return 0;
}