LOOP QUESTION

I tried to make 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. The number of asterisks to print should be stored in a variable. Finally the program asks the user if it should start over, and repeats as many times as the user wants. The prompt to run the program again should look like the one below, and should wait at the end of the line for the user input.
Do you want go again? (y/n):

I want to know the code that make by two different way. (while and for)\

Please help me!!
Last edited on
You should post what you have so far...no one is going to give you the answers to your homework. Are you stuck on the whole thing? Or just the part where it asks the user to run again or not? Good luck!
Here's an example of what you are requesting:

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
27
28
29
#include <iostream>

using namespace std;

int main(){

	char question;

	cout << "Do you want to start the program?" << endl;
        cout << ">";
	cin >> question;

	while (question != 'n') {

		int n_asterisks;

		cout << "How many asterisks do you want to print out?" << endl;
		cout << ">";

		cin >> n_asterisks;

		for (int i = 0; i < n_asterisks; ++i) {
			cout << "*" << endl;
		}

	}

    return 0;
}


Only thing missing is that the program starts automatically after it prints out the asterisks. It doesn't ask you if you want to run the program. Though I have made the code so that you should be able to modify it for your needs.
Last edited on
Topic archived. No new replies allowed.