Hey there!
I am new on this forum and I have started with c++ some weeks ago, although I've had previous programming experiance.
I am trying to make a console application that gives out the average of the users prefered numbers, which I have so far found a way. But the current problem/issue I am having is that I dont know what code I need or how I should modify my code to enable its user to decide how manny numbers he wishes to find the average of, right now I can only choose between 1 and 2. In other words, how do I add 3 and 4?
here is the code, sorry im a noob =)
/*
A simple program to help finding the average of numbers
*/
#include <iostream>
#include <cmath>
using namespace std;
cout << "Average finder v.1.0 - LimePatch" << endl << endl;
int x;
cout << "Please enter how manny numbers you wish to find the average of" << endl << endl;
cin >> x;
cout << " " << endl;
if (x <= 1)
{
cout << "You need more than one number to find the average" << endl << endl;
}
else if (x <= 2)
{
cout << "Okay, please list your " << x << " numbers" << endl << endl;
cin >> dnumber1;
cin >> dnumber2;
cout << " " << endl;
daverage = (dnumber1 + dnumber2) / 2;
cout << "The average of your number is " << daverage << endl;
}
What you need is a loop. Read the tutorial on this website.
What I would do in your situation (you may want to read the tutorial before you try and understand this, especially if you don't understand loops yet) is set up a while loop. When a user inputs an EOF (end of file) they exit the loop. Until then, they keep on putting in numbers. I store the active number in one variable and I simply add that number to a running total. I also increase a counter of the number of numbers. When they exit the loop I divide the total by the counter (making sure to dodge divby0) and print.