C++: I'm using Microsoft Visual Studio 2012 and I am not sure what to do about these two errors:

             2  IntelliSense: identifier "x" is undefined   
            Error   1   error C2065: 'x' : undeclared identifier

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


#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");
cout << "File Open successful!" << endl;
while(!f.eof() && i<a[x])
}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++)
cout<<"\nMinimum= "<<min;
void Numbers::m_max(int m_arr)
int max=a[0];
for(i=1; i<m_arr; i++)
cout<<"\nMaximum= "<<max;
void Numbers::sum_tot(int m_arr)
int sum=0;
for(i=0; i<m_arr; i++)
cout<<"\nSum= "<<sum;
void Numbers::avg(int m_arr)
   double avg=0;
   for (i=0; i<m_arr; i++)
   cout << "\nAverage= " << avg;
int main()
Numbers obj;
fstream f;
   int m_arr=20;
cout<<"\nNumbers: \n";
   return 0;


#ifndef NUMBERS_H
#define NUMBERS_H
class Numbers
int i, a[x];
void m_min(int);
void m_max(int);
void sum_tot(int);
       void avg(int);
void read_from_user_file(int);
void displays(int);

**//numbers1.txt// **
20 13 15 16 18 12 21 34 54 6 25 29 53 36 42 66 41 14 85 30

Recommended Answers

All 6 Replies

Did you try to put your #define x 20 in the .h file instead of the .cpp file?
I would rather use const here, instead of #define

x is not defined prior to the #include of numbers.h so it cannot yet be substituted at the statement:

int i, a[x];

in your header

You must either move your #define prior to the #include for numbers.h or, as suggested, put the #define inside the header itself.

My own preference would not be to use a statically allocated array but to use a container from the standard template library such as std::vector since then you don't need to recompile your program for files with differing numbers of numbers.

How would it look like if I used const cause I think I tried it but didn't work?
I think I tried moving #define inside header but it didn't change anthing, I don't know I did it properly or not but thats what happened.

I got that part actually now, thanks.

1   IntelliSense: expected a declaration    

I'm getting this error for text file though?

Try to be more specific, which line, what variable, etc.

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.