AvRAGE++

I'm currently working on my newest program called AvRAGE which is a small averaging program (hence the name) that allows the user to average any given numbers. well i have come across a slight problem. the problem is how do i make it so as the user can choose any amount of numbers to be averaged instead of just lets say 3 numbers. (ex. a person wants to use 3 numbers the first time then 5 the next 7 the next and 2 the next) what would i need to include in my program to make this possible? below is my code so far:
/*AvRAGE a average program*/
#include <iostream>
using namespace std;

int main(void)
{
system("TITLE AvRAGE++");
system("COLOR 3");
double dnumber1 = 0.0;
double dnumber2 = 0.0;
double dnumber3 = 0.0;
double daverage = 0.0;
cout << "Welcome, to AvRAGE++!" <<endl;
cout << "Please enter 3 numbers that you would like to average:"<<endl;
cin >> dnumber1;
cin >> dnumber2;
cin >> dnumber3;

daverage =(dnumber1 + dnumber2 + dnumber3) /3;
//need to loop program
cout << "the average for those numbers are:"<< daverage <<endl <<endl;
system("PAUSE");
return 0;
}
AvRAGE, interesting... It's an angry averaging program! :-P

You don't actually need to input all the numbers before you compute the average. In fact, you only need to input one at a time. Use a loop.

You can also use a stringstream (#include <sstream>) to make it so the user can just enter a blank line to finish:
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
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using namespace std;

int main()
  {
  double number = 0.0;
  string users_input;

  // you may want to define one or more variables here...

  cout << "Enter the numbers to average.\n"
       << " Press ENTER twice to stop entering numbers and view the average.\n";

  // Get a line of text from the user
  // as long as there is input
  // and as long as the user hasn't entered a blank line (that is, pressed ENTER twice).
  while (getline( cin, users_input ) and !users_input.empty())
    {
    // Convert the text string that the user input into an actual number.
    if (!(stringstream( users_input ) >> number))
      {
      // If he entered anything other than a number,
      // complain and go get another line from the user.
      cout << "what? try again: ";
      continue;
      }
  
    // do something with 'number' here...
    }

  // print the average here...

  // This does the same as system("PAUSE") [except better :-)]
  cout << "Press ENTER to continue...";
  cin.ignore( cin, numeric_limits<streamsize>::max(), '\n' );

  return 0;
  }


Hope this helps.
Last edited on
yes it would help if i wasnt soo new could you explain all of this for me?
You would want to use a dynamic array if you want the user to specify how many numbers he wants to average, I made this program for class a couple weeks ago, Duoas, your a mad genius you want to explain pointers to Venged or do you have a link that will let him know what a pointer is... but here is my code...
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* temps = NULL;
int* days = NULL;
int numDays;
int searchDay;

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "Please input the number of days you would like to input temperatures for: ";
	cin >> numDays;

	temps = new double[numDays];
	days = new int[numDays];

	for (int i = 0; i < numDays; ++i)
	{
		temps[i] = 0;
		days[i] = 1;
	}

	// Recieve inputs of temperatures for those days
	for (int i = 0; i < numDays; ++i)
	{
		cout << "\nPlease input a temperature for day " << i + 1 << ": ";
		cin >> temps[i];
		days[i] += i;
	}

	cout << "\nWhat city would you like to see your inputed temperature for: ";
	cin >> searchDay;

	
	// Find temperature for day they want temp for
	for (int i = 0; i < searchDay; ++i)
	{
		if (searchDay == days[i])
		{
			if (temps[i] == 0)
		{
			cout << "\nYou hav inputed a day that does not exist." << endl;
		}
			else
			{
			cout << "\nYour inputed temperature was " << temps[i] << " degrees." << endl;
			}
		}
		if (temps[i] == 0)
		{
			cout << "\nYou hav inputed a day that does not exist." << endl;
		}
	}

	return 0;
}


It tells the user to input a certain amount of days it want the computer to calculate for... It uses pointers because when you make an array it has to be a constant number you user for the array... With the help of pointers, you can work around that and let the user say how many days he/her wants to calculate :)
@Venged
You won't understand everything right away. I just gave you a template kind of thing. Copy it to some cpp file on your PC, then edit it and put your stuff in it and see what you can make it do.

Compile it and execute it and see what happens. Then edit it, change a thing or two, and repeat.

For example, you could replace the comment "do something with 'number' here" with the code cout << number;. Compile and run.

I've edited my last post to add a few more comments so you can get a better idea of what is going on.


Now, to calculate an average of some collection of numbers, you need only two things: the sum of the numbers, and the number of numbers. Now think about how you can use a loop:
If I ask, what is 2 + 3 you say 5.
If I ask, what is 2 + 3 + 7 you say 12.
If I ask, what is 2 + 3 + 7 + 1 you say 13.
If I ask, what is 2 + 3 + 7 + 1 + 8 you say 21.
Now I ask, what is the average?

Hope this helps.

@hartmannr76
I see you've been ruined by MS... ;->

This site has excellent tutorials: http://www.cplusplus.com/doc/tutorial/pointers.html

That's another good way to keep information. The only condition is that you must know beforehand how many items you plan to store. (The strength of the STL is that array containers can resize themselves dynamically as needed.)

BTW. Your days array is completely unnecessary, since the day is always i+1. Hence to find a day all you have to do is check:
1
2
3
4
5
6
7
8
if ((1 > searchDay) or (searchDay >= numDays))
  {
  cout << "\nYou did not provide information for that day.\n";
  }
else
  {
  cout << "\nYour inputed temperature was " << temps[ searchDay -1 ] << " degrees.\n";
  }


Anyway, nice idea!
Haha yea i did that project in VS but I hate it, i posted in another thread i prefer to use Dev much more because it doesnt throw in all of that unnecessary code for a simple console app. :)
Yeah, it seems to me that all the big IDEs like to do that... I like Borland's stuff OK, but a simple C++Builder project started me off with about 10 lines of boilerplate. Ack!
ok im going to use your work duoas and ill give you credit when i make it available for download( im using it to test my downloading service ;)) and ill give additional credit to hartmannr76
Well, I'm glad to see there are more than a few honest people out there in the world.

It is a sad commentary that people have to beware of copyrights on code that anyone with the proper knowledge would write. If you weren't so new to C++ you would have written the same darn thing without a second thought.

So, you hereby have my permission to do whatever you like with it without having to credit me for it. (That is typically the case with anything I post online that doesn't explicitly state otherwise.)

:-)
well yes but i would like to give credit because if it wasnt for your help i wouldnt have finished it lol and i dont completely understand it yet lol back to reading the tut.
Topic archived. No new replies allowed.