C++ arrays, fstream

C++: I'm using Microsoft Visual Studio 2012 and I am not sure what to do about this error:
IntelliSense: identifier "x" is undefined
It looks like it should work but don't understand.

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

//numbers1.cpp//
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
#include <fstream>
#include <string>
#include "numbers1.h"
#define x 20
using namespace std;
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;
}


//numbers1.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef NUMBERS_H
#define NUMBERS_H
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);
};
#endif 


**//numbers1.txt// **
20 13 15 16 18 12 21 34 54 6 25 29 53 36 42 66 41 14 85 30
Last edited on
1
2
#include "numbers1.h"
#define x 20 
the compiler process from top to bottom, you define `x' too late.
OK, thanks. I got it compile but now I am having problems with numbers1.txt.
\
Last edited on
I'm getting this error with txt file:
1 IntelliSense: expected a declaration
¿that's all? ¿no line number?
it's in the text file line 1
you don't want to compile your data file. You want to remove numbers1.txt from your compilation list. I don't use Visual Studio, so I don't know how to do that.
Topic archived. No new replies allowed.