need idea/help

okay for uni i got given a question in class, i am at a loss at how to solve it i understand that i need to put boundaires in and average my loop but i don't know exactly what to write...i also have to make the loop reset if -1 is used which is easy enough....

i am using visual studio 2010 ultimate the program in in win32 with its source file being a .cpp


this is the question.


Write a program in C that uses a while loop to continue looping until the user enters ‘-1’. Make the program get an integer number from the user each loop and add it to a total. Average the values each loop. Print out a message indicating the current total and average each loop. If the value exceeds 2000 then reset the total and start from 0. Print out a message indicating this has happened. If the user enters a number less than 1 then discount the number and print out a message indicating the number is invalid. If the user enters a value that is not a number the discount the value and print out a message indicating the value is not a number.



any helpful link/video or advice would be nice or even an example....i dont want the entire quesion answered as that defeats the point of learning how to do it in my opinion just need some help.
Here's what I would do:

1
2
3
4
5
6
7
8
9
10
11
12
// Declare variables for input, counter
// Initialise input to something not = -1
// Initialise counter to zero
// START while loop (condition != -1)
// --- Ask user for input
// --- Validate input (check it's not a letter or -1 or < 1)
// --- Add the value to the counter
// --- Check the counter total
// --- If > 2000, set counter to 0, output message
// --- Otherwise output message displaying total
// END while loop
// Output some kind of goodbye message 


Other hints:
I would consider defining -1 as a constant or a #define value, something like EXIT_VALUE. It makes it more readable.

When validating the input, don't forget to break out of the loop if -1 is hit. You don't want the rest of that iteration of the loop to proceed.

EDIT: Give it a go, see how far you get and post back with anything that you're stuck on.

Last edited on
Topic archived. No new replies allowed.