Log in

View Full Version : Getting a display for calculating average value


Bamaboy53
Oct 29, 2009, 12:32 PM
Below you can see that I have a C++ program that should calculate and display the average value to the user, but after entering numbers for calculation the output will not display average. Help!

#include<iostream>
using namespace std;

class average
{
private:
int arr[100]; // Variable to hold the numbers entered by the user
int total; //To hold the total value of the numbers entered
float avg; //To hold the average value of the entered numbers
int count; //To hold the total number of elements to be entered by the user
//Constructor
public:
average()
{
cout<<"How Many Numbers do you want to Enter?"<<endl<<"NUMBER : ";
cin>>count;
total = 0;
}
//Member Functions
void get_input()
{
cout<<endl<<"Please Enter The Numbers : "<<endl;
for(int I=0;i<count;i++)
{
cout<<"Number "<<(I+1)<<" : ";
cin>>arr[i];
}
}
void calculate_avg()
{
for(int I=0;i<count;i++)
{
total += arr[i];
}
avg = (float)total/count;
}
void display()
{
cout<<endl<<"Average of Entered Numbers are : "<<avg<<endl;
}
};

//Main Function
int main()
{
average avrg;
avrg.get_input();
avrg.calculate_avg();
avrg.display();
return 0;
}

Scleros
Oct 29, 2009, 03:56 PM
Help!

I copied and pasted your code into a Visual Studio 2005 command-line project and it worked for me with ad-hoc int number entry for sets of 5, 10, and 20 numbers. So, what set of numbers did you enter that didn't provide the correct average?

A typical gotcha with this type of thing is dividing ints and not properly casting and the result getting truncated somewhere in the operation. Also, program fails ungraciously if something other than an int is entered. Input validation is a critical feature of any real-world application. Best to get in the habit.

Bamaboy53
Oct 29, 2009, 04:03 PM
I am using a DEV C++ program and using various combinations of numbers and whenever I enter the last set of numbers the output still disappears.

Bamaboy53
Oct 29, 2009, 04:10 PM
Hey I think I figured this thing out. At the bottom just before return 0;
}
I inserted a system ("pause"); to hold the output to display the average.

Scleros
Oct 29, 2009, 04:46 PM
Yeah, when you're running a command-line app in a Window, when the program exits the Window disappears too fast to see the final output. Another option would be to add a user prompt to either re-run the program or exit.

Bamaboy53
Oct 29, 2009, 05:04 PM
I am fairly new at C++, how would I insert a re-run or user prompt?

Scleros
Oct 29, 2009, 10:08 PM
how would I insert a re-run or user prompt?

One way to do it is to place the whole program in a FORever loop and then present a prompt for a certain key. Read the key and check it. If the key is correct, BREAK out of the FOR loop, otherwise the loop repeats.


int main()
{
char cKey;

for( ; ; ) {
average avrg;
avrg.get_input();
avrg.calculate_avg();
avrg.display();

// Output prompt
cout << endl << "Press 'Q' to quit or any other key to continue..." << endl;

// Read key
cin >> cKey;

// Is key a 'Q'?
if(cKey == 'Q')
// Exit FOR
break;

// Output some spacing to visually separate output of next loop cycle
cout << endl << endl;
}

return 0;
}