C++: I'm using Microsoft Visual Studio 2012 and I am really confused and am getting errors.
Prompt: Make program that reads 20 integers, display that array, and find min, max, sum, and avg.
One method named "read from user file" which will ask the user to input a filename and will then read from 1 to 20 integers from that file, but not calculate any statistics. Second method will display the values read in, in the order they were found in the file. Third method will calculate the statistics and store these in the appropriately named variables that are also to be declared in the class. Finally a fourth method will display those statistics from those variables. Your int main function will create an object variable of the Numbers class type. It will then use that object to call those four methods, one after another, in order to demonstrate that the class and it's methods work correctly. No global variables should be used in this program. The Numbers class will be defined in its own .h file, and will use the Ifndef Trick. Make no system calls. User Interface Note that no debugging information (useful for testing a program's internal states) should be output in the submitted program
CODE:
#include <fstream>
#include <string>
#include "Class_Numbers.h"
#define x 20
using namespace std;
class Numbers
{
private:
int i,a[x];
public:
void m_min(int);
void m_max(int);
void sum_tot(int);
void avg(int);
void read_from_user_file(int);
void displays(int);
};
void Numbers::read_from_user_file(int m_arr)
{
ifstream f("Numbers.txt");
if(f.is_open())
{
cout << "File Open successful!" << endl;
while(!f.eof() && i<a[x])
{
f>>a[i];
i++;
}a[i-1] = '\0';
}
}
void Numbers::displays(int m_arr)
{
for(i=0; i<m_arr; i++)
cout<<a[i]<<" ";
}
void Numbers::m_min(int m_arr)
{
int min=a[0];
for(i=1; i<m_arr; i++)
if(a[i]<min)
min=a[i];
cout<<"\nMinimum= "<<min;
}
void Numbers::m_max(int m_arr)
{
int max=a[0];
for(i=1; i<m_arr; i++)
if(a[i]>max)
max=a[i];
cout<<"\nMaximum= "<<max;
}
void Numbers::sum_tot(int m_arr)
{
int sum=0;
for(i=0; i<m_arr; i++)
sum+=a[i];
cout<<"\nSum= "<<sum;
}
void Numbers::avg(int m_arr)
{
double avg=0;
for (i=0; i<m_arr; i++)
avg+=a[i];
avg/=m_arr;
cout << "\nAverage= " << avg;
}
int main()
{
Numbers obj;
fstream f;
int m_arr=20;
cout<<"\nNumbers: \n";
obj.read_from_user_file(m_arr);
obj.displays(m_arr);
obj.m_min(m_arr);
obj.m_max(m_arr);
obj.sum_tot(m_arr);
obj.avg(m_arr);
cout<<endl;
system("pause");
return 0;
}
Numbers.txt:
20 13 15 16 18 12 21 34 54 6 25 29 53 36 42 66 41 14 85 30
Class_Numbers.h :
#include<iostream>
#include <fstream>
#include <string>
#define x 20
using namespace std;
class Numbers
{
private:
int i,a[x];
public:
void m_min(int);
void m_max(int);
void sum_tot(int);
void avg(int);
void read_from_user_file(int);
void displays(int);
};
Even when I deleted the class from the main cpp program it didn't work I don't get why. When I ran it without the header file, I get some weird numbers and output looks like this:
Numbers:
-858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 -858993460
Minimum= -858993460
Maximum= -858993460
Sum= -16
Average= -8.58993e+008
Press any key to continue . . .
Should look something like this:
Numbers:
20 13 15 16 18 12 21 34 54 6 25 29 53 36 42 66 41 14 85 30
Minimum= 6
Maximum= 85
Sum= 630
Average= 31.5