Need help with loops and functions

I'm trying to create a program where the user can enter numbers over and over, until they enter a negative number, which will end the program. It will also calculate the average of the positive numbers that were entered.

This is what I have so far:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include using namespace std; double average(); int main(){ char answer; cout << "Average calculator." << endl; cout << endl; cout << "Enter a stream of positive numbers (0 or above)." << endl; cout << "Enter a negative number to indicate you are finished." << endl; int value; cin >> value; while (value >= 0) {average(); if (value >= 0) {cout << "The average is: " << average() << endl; cout << endl; cout << "Do you want to compute another average (y or n)?" << endl; cin >> answer;} if (answer == 'y') {cout << "Enter a stream of positive numbers (0 or above)." << endl; cout << "Enter a negative number to indicate you are finished." << endl; average();} if (answer == 'n') {return 0;} } } double average(){ int value; int count = 0; int sum = 0; double average; do {cin >> value; sum += value; count++; average = sum / count; return average; if (value < 0) {return 0;} } while (value >= 0); }``````

So far, the program just allows me to input numbers, but it doesn't not show the average or end the program when I enter a negative number. Please help!
Last edited on
First, please edit your post so that it uses code tags - the <> button on right.

You haven't done the while loop properly, - you need braces to make a compound statement.

HTH
Okay, the while loop should be fixed now right? What else am I doing wrong?
There is no need to call average function after the: while(value>=0).

Also, the reason why it runs without asking the user whether he wants to compute more is because, if he enters 'y' at the first instance, answer is always equal to 'y'. So you must reset it(maybe with answer=0) at the end or beginning of the loop.

Hope it helps,
Aceix.
Thank you.

This is what I have now:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657`` ``````#include using namespace std; double average(); int main(){ char answer; cout << "Average calculator." << endl; cout << endl; cout << "Enter a stream of positive numbers (0 or above)." << endl; cout << "Enter a negative number to indicate you are finished." << endl; int value; cin >> value; average(); while (value >= 0) { if (value >= 0) {cout << "The average is: " << average() << endl; cout << endl; cout << "Do you want to compute another average (y or n)?" << endl; cin >> answer;} if (answer == 'y') {cout << "Enter a stream of positive numbers (0 or above)." << endl; cout << "Enter a negative number to indicate you are finished." << endl; average();} if (answer == 'n') {return 0;} answer = 0; } } double average(){ int value; int count = 0; int sum = 0; double average; do {cin >> value; sum += value; count++; average = sum / count; return average; if (value < 0) {return 0;} } while (value >= 0); }``````

Last edited on
Why doesn't this part loop? It ends after I enter y.
 ``123456789101112131415`` `````` do { counter++; cout << "Enter a stream of positive numbers (0 or above)." << endl; cout << "Enter a negative number to indicate you are finished." << endl; cin>>value; if (value < 0) {cout<<"The average is: "<> answer; value = 0; } while(value >= 0);``````
Try something like this
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include using namespace std; int menu(); void inputNum(); void outputResults(); int num = 0; int count = 0; double avg , total; int main() { menu(); return 0; } void inputNum() // this is a sentinel controlled loop. { cout << "Enter number" << endl; cin >> num; while (num >= 0)// Entering a negative number will terminate the loop. { total += num; count = count ++; cout << "Enter number" << endl; cin >> num; } outputResults(); } void outputResults() // calculate average & print results to the screen { avg = (total / count ); cout << endl; cout << "Total "<< total << endl; cout << "Count " << count << endl; cout << "Average " << avg << endl; cout << endl; count = 0;// clear count before returning to menu total = 0;// clear total before returning to menu. menu(); } int menu() { int choice; cout << "=========================================" << endl; cout << "Press 1 to enter a new series of integers." << endl; cout << "Press 2 to EXIT." << endl; cout << "=========================================" << endl; cin >> choice; cout << endl; switch (choice) { case 1: inputNum(); break; case 2: return 0; break; default: cout << "Not a valid choice, try again." << endl; menu(); } }``````
Topic archived. No new replies allowed.