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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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;
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.

Look at the reference section top left of this page, learn about while & for loops.

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.
Anymore advice?

This is what I have now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#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;
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	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: "<<calcAverage(sum,counter,value)<<endl<<endl;
	cout << "Do you want to compute another average (y or n)?" << endl;
	cin >> answer;
	value = 0;
	} while(value >= 0);
Try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<iostream>

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.