0

Asterisks

Someone please give me an idea to write small program IN C++ 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 "Do you want to try again? (y/n)", and should wait at the end of the line for the user input.

13th Feb 2017, 6:55 PM
Safal Basnet
Safal Basnet - avatar
3 ответов
+ 1
Here's the basic idea of what you're asking. Note that there isn't any type checking so if you enter something other than an Integer it may not work as expected. #include <iostream> using namespace std; int main() { int x; char ans = 'n'; do { cout << "How many asterisks would you like to print? "; cin >> x; for(int i = 0; i < x; ++i) { cout << '*'; } cout << endl << "Do you want to try again? (y/n) "; cin >> ans; } while (ans != 'n' && ans != 'N'); return 0; }
13th Feb 2017, 7:35 PM
ChaoticDawg
ChaoticDawg - avatar
0
Thanks. I got the basics but how shall I rerun the same code again if the user wants to try agian?
13th Feb 2017, 7:59 PM
Safal Basnet
Safal Basnet - avatar
0
it's in the code I already gave. Try running it.
13th Feb 2017, 8:01 PM
ChaoticDawg
ChaoticDawg - avatar