Help Guys!!!

Hello I'm Pavlos from Greece I'm new in C++ i have an asignment for my exams and i have a problem in this programm(look down). most of the variables are named in greeklish so you might not understand the use. My problem is with the extern variables I have in line 10 (min and max). i have this error "106 C:\Users\pavlos\Desktop\lab_3_2.cpp.cpp `max' undeclared (first use this function)", BUT the funny think is that I'm using the variables p and f in the same way and I don't get an error about them. What's the problem with min and max?????????????? (It will help you if you copy it in Dev-C++ and you run it)
Thank you!!!







# include <iostream>
# include <stdlib.h>
# include <cstring>
using namespace std;


float mesos; //dhlwsh genikwn metavlhtwn
int p;
int f;
float min;
float max;

class student

{
private:
int am ;
char *onoma ;
int mathimata ;
float *vathmologia ;
float mo ;
int perase ;
int kopike ;
float minvathm ;
float maxvathm ;
public:
student ( int arithmos , char *name , int mathm , float *vathm ) {setstudent(arithmos,name,mathm,vathm);} //constructor
student () {mo=0, perase=0, kopike=0, minvathm=0, maxvathm=0;}
void setstudent(int arithmos , char *name , int mathm , float *vathm );
void calc(); //synarthshsh ypologismou
void emfanish(); //synarthsh ektypwshs
};


void student::setstudent(int arithmos , char *name , int mathm , float *vathm) //arxikopoihsh dedomenwn
{
am=0; onoma="NO NAME"; mathimata=0; vathmologia=0;

if (arithmos >=1 && arithmos <=9999 )
{
am = arithmos;
}

if (mathm >= 1 && mathm <=8)
{
mathimata = mathm;
}

onoma = new char [strlen(name)];

if (strlen(name)>=1 && strlen(name)<=40)
{
onoma = name ;
}

vathmologia = new float [mathm];

for (int i=0; i<=mathm ; i++)
{
if (vathm[i] >= 1 && vathm[i] <=10)
{
vathmologia[i] = vathm[i] ;
}
}
}

void student::calc()
{
float sum = 0 ; //ypologismos mesou orou
for (int i=0 ; i < mathimata ; i++ )
{
sum = sum + vathmologia[i];
}

mo = sum/mathimata;
mesos = mo ;

perase=0; kopike=0; //ypologismos mathimatwn pou perase kai kopike
for (int i=0 ; i < mathimata ; i++ )
{
if (vathmologia[i] < 5)
{
perase = perase +1;
}
else
{
kopike = kopike + 1;
}
}
p=perase;
f=kopike;

minvathm=10; maxvathm=0; //ypologismos min max
for (int i=0 ; i < mathimata ; i++ )
{
if (vathmologia[i] < minvathm)
{
minvathm = vathmologia[i];
}

if (vathmologia[i] > maxvathm )
{
maxvathm = vathmologia[i];
}
}
max=maxvathm;
min=minvathm;

}

void student::emfanish()
{
cout << "Name: " << onoma <<"\n" ;
cout << "id: " << am <<"\n" ;
cout << "Subjects Given: " << mathimata << "\n" ;
cout << "Grades Taken: " ;
for (int i=0 ; i < mathimata ; i++ )
{
cout << vathmologia[i] << " " ;
}
cout<<"\n";
}

int main()
{
student ant;
float vathm[] = {7.0, 3.0, 6.0, 10.0, 4.0};
ant.setstudent(1510, "James Stathatos", 5, vathm);
ant.emfanish();
//float min; float max;
ant.calc();
cout << "Passed in " << p << " Subjects, \n";
cout << "Failed in " << f << " Subjects \n";
cout << "Average Grade: " << mesos <<"\n";
cout << "Minimum Grade: " << min <<"\n";
cout << "Maximum Grade: " << max <<"\n";
system ("pause");
return 0 ;
}
posting with code tags so I can find line 106:
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# include <iostream>
# include <stdlib.h>
# include <cstring>
using namespace std;


float mesos; //dhlwsh genikwn metavlhtwn
int p; 
int f; 
float min; 
float max; 

class student

{
private:
int am ;
char *onoma ;
int mathimata ;
float *vathmologia ;
float mo ;
int perase ;
int kopike ;
float minvathm ;
float maxvathm ;
public:
student ( int arithmos , char *name , int mathm , float *vathm ) {setstudent(arithmos,name,mathm,vathm);} //constructor
student () {mo=0, perase=0, kopike=0, minvathm=0, maxvathm=0;}
void setstudent(int arithmos , char *name , int mathm , float *vathm );
void calc(); //synarthshsh ypologismou
void emfanish(); //synarthsh ektypwshs 
};


void student::setstudent(int arithmos , char *name , int mathm , float *vathm) //arxikopoihsh dedomenwn
{ 
am=0; onoma="NO NAME"; mathimata=0; vathmologia=0;

if (arithmos >=1 && arithmos <=9999 )
{
am = arithmos;
}

if (mathm >= 1 && mathm <=8)
{
mathimata = mathm;
}

onoma = new char [strlen(name)];

if (strlen(name)>=1 && strlen(name)<=40)
{
onoma = name ; 
}

vathmologia = new float [mathm];

for (int i=0; i<=mathm ; i++)
{
if (vathm[i] >= 1 && vathm[i] <=10)
{
vathmologia[i] = vathm[i] ; 
}
}
}

void student::calc()
{
float sum = 0 ; //ypologismos mesou orou
for (int i=0 ; i < mathimata ; i++ )
{
sum = sum + vathmologia[i];
}

mo = sum/mathimata;
mesos = mo ; 

perase=0; kopike=0; //ypologismos mathimatwn pou perase kai kopike
for (int i=0 ; i < mathimata ; i++ )
{
if (vathmologia[i] < 5)
{
perase = perase +1;
}
else
{
kopike = kopike + 1;
}
}
p=perase;
f=kopike;

minvathm=10; maxvathm=0; //ypologismos min max
for (int i=0 ; i < mathimata ; i++ )
{
if (vathmologia[i] < minvathm)
{
minvathm = vathmologia[i];
}

if (vathmologia[i] > maxvathm )
{
maxvathm = vathmologia[i]; 
}
}
max=maxvathm;
min=minvathm;

}

void student::emfanish()
{
cout << "Name: " << onoma <<"\n" ;
cout << "id: " << am <<"\n" ;
cout << "Subjects Given: " << mathimata << "\n" ;
cout << "Grades Taken: " ;
for (int i=0 ; i < mathimata ; i++ )
{
cout << vathmologia[i] << " " ;
}
cout<<"\n";
}

int main()
{
student ant;
float vathm[] = {7.0, 3.0, 6.0, 10.0, 4.0};
ant.setstudent(1510, "James Stathatos", 5, vathm);
ant.emfanish();
//float min; float max;
ant.calc();
cout << "Passed in " << p << " Subjects, \n";
cout << "Failed in " << f << " Subjects \n";
cout << "Average Grade: " << mesos <<"\n";
cout << "Minimum Grade: " << min <<"\n";
cout << "Maximum Grade: " << max <<"\n";
system ("pause");
return 0 ;
}
Last edited on
Found it.

The words max and min are already defined as a macro in stdlib.h.

In my stdlib.h I have the following:
1
2
3
4
#ifndef __cplusplus
#define max(a,b)    (((a) > (b)) ? (a) : (b))
#define min(a,b)    (((a) < (b)) ? (a) : (b))
#endif 


using #include <cstdlib> instead of #include <stdlib.h>

This resolves part of the issue but it also appears that it is also defined in <xutility> which is called by another include.

To fix this replace using namespace std; with:
using std::cout; using std::endl;.

This is the problem with using namespace std;. If you try and make a global variable that happens to have the same name as something in std, you get a conflict.

ANother option is to just rename min and max.
Last edited on
Thank you very much guys!!! that's a think I could ever imagine!!! I'll just remane the variables. thank you!
Topic archived. No new replies allowed.