Optional Input Amount

Hey Guys

I'm pretty new and I have started making an application for pc that allows you to input numbers into it and it will tell you the average, although I can only figure out how to make a set input amount. Basically what I am asking is how can I make my application so that you can input as many or as little numbers as you want an average for. Here is my code 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
#include<iostream>
#include <cstdlib>

using namespace std;

int main()
{
    char c;
    do
    {


    system ( "TITLE 5 Number Averager" );
    system ( "COLOR 2" );
    double dnumber1 = 0.0;
    double dnumber2 = 0.0;
    double dnumber3 = 0.0;
    double dnumber4 = 0.0;
    double dnumber5 = 0.0;
    double daverage = 0.0;

    cout << "Please enter 5 numbers (One on each line!)" << endl;
    cin >> dnumber1;
    cin >> dnumber2;
    cin >> dnumber3;
    cin >> dnumber4;
    cin >> dnumber5;

    daverage = (dnumber1 + dnumber2 + dnumber3 + dnumber4 + dnumber5) / 5;

    cout << "The average of your 5 numbers is: " << daverage << endl << endl;

            cout << "Do you want to average numbers again (Y/N)?";
        cin >> c;
    } while (c == 'y'  ||  c == 'Y');


    system("PAUSE");
    return 0;
}


Could someone please help me so I can make it so I can input as many numbers as I want instead of just 5?

(Please explain as simple as possible as I am going to make a calculator as well an I will want to add as many symbols and numbers as I want!)
Hey Zolo skullu,

To answer your question there are many different ways to go about doing this. Most of them though will force you to use a certain number of variables ( int number(s) ) because there can only be so many numbers allowed to be typed in at one time. If you think about calculators. the simple ones only allow you to type in 8 digits while the scientific ones allow you to type in 100 and more digits plus math signs at one time. even for those that do this they will calculate your numbers one time and you are then allowed to add on more to the numbers that were already calculated.

For example;

Scientific calculators

2+2+2+2+2 then I would press the equal sign button and get 10

However, Simple calculators don't let you do this. They automatically calculate the numbers but you can keep added on more numbers to it

For example:

2 I must now press the + button and the screen may flash on some calculators.
2 then I press = which gives me 4

However to add anything to it I must press add again or subtract or any other symbol.

Now if you think about it using what you were shown. What would work? Using the simple calculator way of doing it will work, right?

Note:( you would have to put a limit of how many numbers they can type in before calculating and if you didn't the computer it self has a limit on how many digits it will take.)


If we are going to do the simple calculator way then think about how many variables ( int numbers we need)

If you thought 2 then yes that is the least we can use, but how do we only use 2 variables to take tons of numbers?

It is actually pretty simple and we already know how looking at the simple calculator

Int number1
int number2

We will ask for the person to type in their first number. Then we will ask for the math symbol they want to used( in the case of your average you will just add them without asking). Followed by asking for the next number and then we will add number1 and 2 together and give that answer for number 2 to hold onto for us. now we can just do an easy add on from here on out. We will just keep adding on number 1 to number 2. Everytime you do this number 2 will keep getting bigger and bigger until they tell you when they are ready to see the average. You then just calculate the average number using just number 2.

Now think this over to make sure you understand.


Hold up a second!!! how do we keep track of all the numbers so we know how much to average by if we don't ask them for it. Well that is the part you have to find out.

Hint: Read the C++ tutorial.
closed account (o3hC5Di1)
Hi there,

What you need in this case, if you just want to calculate the average / sum / etc. is to store the entries in a std::vector. This is a containertype which can be dynamically resized, so you don't have to worry about the amount of numbers being entered. If it's only for averages / sums, you can just add the number to a total every time and keep a counter of the amount of numbers entered.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
double sum, tmp;
int count;

do  
{
    std::cout << " Enter a number: ";
    std::cin >> tmp;

    if (tmp > 0)
    {
        sum += tmp;  //increase sum by amount entered
        count++;
    }
} while (tmp >0); //enter -1 to stop

std::cout << "Average " << sum/count;


All the best,
NwN
Last edited on
Topic archived. No new replies allowed.