help with program

closed account (Diz60pDG)
I am writing a program where the user enters postive numbers and the program outputs the average, and then asks the user if they want to repeat the program....I got the average part to work, but I can't seem to figure out how to get the program to ask the user if they want to do it again

any help is appreciated

heres 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
  #include <iostream>
using namespace std;
double average();
char chr;

int main()
{
	cout << "The avergae is: " << average() << endl;
}
double average()
{
	double input = 0;
	double total = 0;
	int count = 0;
	double average;

	do
	{
		cout << "Enter a stream of positive numbers (0 or above)." << endl;
		cout << "Enter a negative number when you are finished." << endl;
		cin >> input;
		if (input >= 0)
		{
			total = input + total;
			count++;
		}

	} 
	while (input >= 0);
	average = total / count;
	
	return average;
	
	char answer;

	do
	{
		cout << "Would you like to repeat?  Enter Y/N";
		cin >> answer;
		if ('N')
			cout << "Thanks for playing!";
	} 
	while (answer = 'Y');

	cin >> chr;
	
	return 0;
	
}
Hi,

1) In average(); funtion you have return average.....so you don't need put return 0......you put that when you finish your program in main funtion...

2)When you need to compare you have to use (==) not just = because if you put

answer = 'Y' // you say character 'Y' is saved in answer variable.

and

answer == 'Y' // you compare the answer variable with character 'Y'.

Here is the code working....and sorry for my grammar english :D

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

double average();

int main()
{
	char answer;

	do
	{
	    cout << "The avergae is: " << average() << endl << endl;
	    
		cout << "Would you like to repeat?  Enter Y/N:  ";
		cin >> answer;
			
	} while(answer == 'Y');
        
        cout << "Thanks for playing!";

       return 0;	
}
double average()
{
	double input = 0;
	double total = 0;
	int count = 0;
	double average;

	do
	{
		cout << "Enter a stream of positive numbers (0 or above)." << endl;
		cout << "Enter a negative number when you are finished." << endl;
		cin >> input;
		if (input >= 0)
		{
			total = input + total;
			count++;
		}

	} 
	while (input >= 0);
	average = total / count;
	
	return average;
	
}
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
char answer;

	do
	{
		cout << "Would you like to repeat?  Enter Y/N";
		cin >> answer;
// Stuff goes in here ??
        } while (answer == 'Y');

        cout << "Thanks for playing!";
Last edited on
It is not necesary... you are alright... :D....I edited.... thanks
Topic archived. No new replies allowed.