Asterisks

I have an assignment where we are to write a small program that asks the user how many asterisks it should print out. The user responds with a number then the program prints that number of asterisks. Then the program asks the user if it should start over. Can you please assist me with while and for loops?

1
2
3
4
5
6
7
8
9
10
  {
        cout << "How many asterisk do you want me to print?";
        cin >> number


        cout << "Do you want to go again? (Y/N) ";
        cin >> again;

    }while (again == 'Y' || again == 'y');
There are while loops:

1
2
3
4
while( again == 'Y' )
  {
   // stuff to repeat
  }


Then there are do/while loops

1
2
3
do {
      //stuff to repeat here
   } while( again == 'Y' );


Your post misses the "do" - it's required for that form

Now, there are for loops

1
2
3
4
for( int i = 0; i < number; ++i )
   {
    // stuff to repeat here
   }


That is likely what you want after cin >> number (with stuff inside to print the asterisks)

Last edited on
Thank you.
So I am assuming that you don't use for, do and while together?
I am so sorry for being bothersome but I really don't understand this at all.
@Missiepooh,

Well, not "together", but "nested" - one is inside the other.

Post all that you have for us to work on it.

1
2
3
4
5
6
7
8
9
10
11
do {
     // ask for num here

      for( int n = 0; n < num; ++n )
         { 
          // repeated thing in here
         }

      // ask about again here

   } while( again == 'Y');
Last edited on
thank you
Another way to do it (if you want to use std::string) is to do that :
1
2
3
4
#include <string>

//ask for num
std::cout<<std::string(yourNum , yourChar);


Here I call the constructor of std::string which fill a string of size yourNum with the char yourChar.

Obviously if you want to learn, I advise you to do a loop as shown before.
Topic archived. No new replies allowed.