HELP with Ask and get n (the number of times) to print the letter “X”. Using a while loop, print the letter X n times.

Ask and get n (the number of times) to print the letter “X”. Using a while loop, print the letter X n times.

I am not sure how to start this code... help will be very appreciated!
How would you code the assignment using a for loop?

Using a while loop isn't much different:

http://en.cppreference.com/w/cpp/language/while
Could you show me how I would begin this code?
I may not be understanding the question completely, which is why I do not know how to start it.
Maybe this will help you.

1.] Define a variable to hold a number, initialize it to 0.
2.] Define a char variable to hold the character 'X'.

3.] Ask the user how many times he or she wishes the character to be output
4.] cin >> ...;

5.] Write your while loop and ask yourself which condition to use.
Hint: As long as number > 0 execute this loop

6.] Next, inside the loop, write a statement that outputs the character that many times.
7.] cout << ... ;

Voila. :-)

Edit: The above is an example how to break down the problem. The way to solve such problems is by taking these steps. (Before writing code)

Ask yourself, what should the program do?
Which variables do I need and what kind, int, char, double, float ...?
Do I wish the user to input something? What is it?
Which functions, loops, etc. do I need to write?
What is there condition?
What goes where?
Which output should I expect?

If you know this, and still you are not sure how to solve it, choose that part of the problem you know how to solve. Start with the variables, and work your way down the list.

Try different ways of writing your code to simply produce output, for instance, or to test input.

That should help you find a way to 'attack' the problem, and write code that does what you want it to do.
Last edited on
Thank you!
Topic archived. No new replies allowed.