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<iostream>
#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

This could be a first step (compile all 3 files as a project) :

file 1)

// file name: numbers.h //

/*
    No global variables should be used in this program.

    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

    Second method will display the values read in

    Third method will calculate the statistics
    and store these in the appropriately named variables
    that are also to be declared in the class

    Fourth method will display those statistics from those variables
*/

#ifndef NUMBERS_H
#define NUMBERS_H

class Numbers
{
private:
    int* ary;
    int size;
    static const int capacity;
    int min, max, sum;
public:
    Numbers() : ary(new int[capacity]), size(0), sum(0) {}
    ~Numbers() { delete [] ary; size = 0; sum = 0; }

    bool read_from_user_file();
    void show_ary() const;
    void cal_stats();
    void show_stats() const;
} ;

#endif

file 2)

// file name:  numbers.cpp //

#include "numbers.h"

#include <iostream>
#include <fstream>
#include <string>


const int Numbers::capacity = 20;


// returns true if file opened ok, else returns false
bool Numbers::read_from_user_file()
{
    std::cout << "Enter a file name to read (ex. \"numbers.txt\"): "
              << std::flush;
    std::string name;
    getline( std::cin, name );
    std::ifstream fin( name.c_str() );
    if( fin )
    {
        while( size < capacity && fin >> ary[size] )
        {
            sum += ary[size];
            ++size;
        }
        fin.close();
        return true;
    }
    // else
    std::cout << "There was a problem reading from file " << name << '\n';
    return false;
}


void Numbers::show_ary() const
{
    std::cout << "Numbers:n";
    for( int i = 0; i < size; ++ i )
        std::cout << ary[i] << ' ';
    std::cout << std::endl;

}

file 3)

// test_class_Numbers.cpp //

/*
    Output 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
*/


#include "numbers.h"


int main()
{
    Numbers obj;
    if( obj.read_from_user_file() )
    {
        obj.show_ary();
    }
}

Or with NO dynamic memory (and a private cal_stats() member called by show_stats() ... at the top before printing the stats) :

#ifndef NUMBERS_H
#define NUMBERS_H

class Numbers
{
private:
    static const int capacity = 20;
    int ary[capacity];
    int size;
    int min, max, sum;
    void cal_stats();
public:
    Numbers() : size(0), sum(0) {}
    void clear()
    {
        size = sum = 0;
    }

    bool read_from_user_file();
    void show_ary() const;
    void show_stats();
} ;

#endif

I tried it like that actually but I'm still getting errors saying that it can't read the .h file?

Do you know how to compile 'as a project' ?

If not ... then change the included file near the top of the main program file to read:

#include "numbers.cpp" // this firstly calls include numbers.h //

Then make sure the 2 files have correct file names
numbers.cpp
numbers.h

Then make sure all 3 files (the above 2 and the main program file) are in the same folder/directory where your compiler can easily find then ... when it starts to compile the main program

Oh I think it was because I just clicked file and added new header and text file, I didn't do it it in the project.
When I built it, it succedded but when I debug it all I get is this:
Enter a file name to read (ex. "numbers1.txt"):

Yes :)

That was the first step :)

If you enter a file name of a pre-existing text file ... a file that you already made and placed there in the same folder as where the executable file is running ... it will then, read that file, a then display it ... as per the code ... Recall I said: this demo is a a demo of just some first steps.)

So, maybe with a text editor program that comes with Windows OS, like notepad.exe, place a text file, with say the (very original) name of: "numbers.txt" in your working folder ... the folder where the .exe file will be placed after you compile your program.

In that .txt file, you could place a 'copy/paste' of the numbers you were supplied in your problem's example output.

( i.e. the numbers are spaced out by 'white space(s) )

Note:

If you have NEVER BEFORE coded a solution to the problem of reading in some numbers from a text file of numbers ... and then displaying the data in the file ... and then, doing some processing of the data and displaying the results ... WITHOUT using a C++ class to handle ...

then FIRSTLY ...

you probably, BEST see/learn/practice ...

the use of functions and structured programming,
as demo'd at the link I supplied:
http://developers-heaven.net/forum/index.php/topic,2019.0.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.