The program says it's stopped working after I input the first number...

#include <iostream>
using namespace std;

int average();

int main(){
char answer;

cout << "Average calculator." << endl;
cout << endl;
cout << " Enter a stream of positive numers (0 or above)." << endl;
cout << "Enter a negative number to indicate you are finished." << endl;

average();
cout << "Do you want to compute another average (y or n)?" << endl;
cin >> answer;

while (answer == 'y')
{cout << "Average calculator." << endl;
cout << endl;
cout << " Enter a stream of positive numers (0 or above)." << endl;
cout << "Enter a negative number to indicate you are finished." << endl; 
}
if (answer == 'n')
{return 0;}

return 0;
}



int average(){

int value = 0; 
int count = 0;
int sum = 0;
int average;

while (value >= 0)
   {cin >> value;
    sum = sum + value;
    count++;
    count = count - 1;
    average = sum / count;


    if (value < 0)
    {return average;}

}
}
Comments
atm code

You should just put all the code that you're going to repeat under the while loop, instead of splitting it up. In fact, use a do loop which guarantees it always runs once.

do {
    cout << "Average calculator." << endl;
    cout << endl;
    cout << " Enter a stream of positive numers (0 or above)." << endl;
    cout << "Enter a negative number to indicate you are finished." << endl;
    average();  // you forgot to include another call to average()
} while (answer == 'y');

The real issue with your program is when you're calculating average, lines 42-44.

let's say for instance that count = 0

count++;    // count = 1
count = count -1;   // count = 0
average = sum / count;  // division by 0

I would grab a piece of paper and a pencil and start here:

int value = 0; 
int count = 0;
int sum = 0;
int average;

Write down the above variables on the paper and their values.
Then continue looking at each line of code and change the value of each of the variables:

while (value >= 0)
   {cin >> value;
    sum = sum + value;

So change value to a number you decide to input.
Change sum accordingly
Be sure to "input" different numbers and your negative number as you run through these steps. NOTE: only ONE (1) number per cin...

    count++;
    count = count - 1;

change count accordingly

    average = sum / count;

change average -- is this right? what is an average? the sum of what?

    if (value < 0)
    {return average;}

}
}
I changed a few things, but I still need help...


#include <iostream>
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;


while (  )             //not sure what to put here
cin >> value;
if (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);

}

See what WaltP has said in his above post. I'm quite sure you will get your answer for this

while ( ) //not sure what to put here

This article has been dead for over six months. Start a new discussion instead.