Need help with C++ Loops

So I am doing a project regarding loops such as for, while, and do-while. I have no idea of where to start, but I know I have to include those types of loops eventually. Here are the prompts:

Part B: Medium
For part B, you will code a small program that will read in an undisclosed amount of numbers. From these numbers you read in, any positive number should be added to a total, all other numbers should be disregarded. (ie: If the user types in -200, you should not ‘add’ it to the total.)

You should continue to add numbers to the total until the user types in the sentinel value. You should use a good value for this. Using 999 is one possibility for a sentinel. There are better values to use for this. What characteristics make a good sentinel value? What kind of loop should you use to code a problem using a sentinel value? (You should use a while loop for Part B.)
Once the loop has finished (the sentinel value was entered) the total should print out.


Part C: Hard
This last part you will be making a guessing game of sorts. You should make use of a do-while statement to solve this problem. The computer should generate a random number between one and ten inclusive [1, 10]. The user should be allowed to guess at the number. After each guess (using an if-statement) tell the user whether his/her guess was too high or too low. Once you display the result, allow the user to guess again until he/she guesses the correct number.


Any Help would be much appreciated!


Last edited on
TheOnlyBuck,

Do not mind TheInfinitesimalGuy, he does not know what you want until you post some code.

When you think about the instructions for part B it tells the variables you will need like: total, input, and sentinel for a start. What type you give those variables will depend on what kind of numbers you will be using. I will suggest a variable bool cont{true}; to use for the while condition. It would be easier to deal with right now. Then within the while loop when you check for the sentinel value being equal to whatever value you choose then you can change cont to false to end the while loop.

The while loop will continue to ask the user for input then check if it I a positive or negative number. If positive add to total. Also check for the sentinel value match to end the loop.

When the while loop is finished print out the total.

That should get you started with your code. When you have problems post your questions here.

In part C the do while loop is similar to the while loop except that it will execute once before checking the while condition.

In both parts consider the flow of the program and the type of statements you might need.
Hope that helps,

Andy

TheSmallGuy,

You exhibit most of the traits of TheGentleGuy and we all know what happened there.
Topic archived. No new replies allowed.