error: was not declared in this scope

Hey all!

I am getting the following errors for this code:

C:\Users\main.cpp|19|error: expected ‘,’ or ‘...’ before ‘&’ token
C:\Users\main.cpp||In function ‘void tPrint(std::string*)’:
C:\Users\main.cpp|20|error: ISO C++ forbids comparison between pointer and integer
C:\Users\main.cpp|21|error: ‘pArray’ was not declared in this scope

Here is my code:
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

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



using namespace :: std;


#define sNUMBER 50

void tPrint(string*student, int);
void sPrint(string*student, int);
void alpha(string*student);
int generate();
//Function Definitions
void tPrint(string*student pArray){
	if(alpha != '1'){
		alpha(pArray);
	}
	int i;
	for(i=0; i<sNUMBER;i++){
		pArray->tPrint();
		pArray+=1;
	}
}
void sPrint(string*student pArray){
	if(alpha != 0){
		alpha(pArray);
	}
	int i;
	for(i=0; i<sNUMBER;i++){
		pArray->sPrint();
		pArray+=1;
	}
}
void alpha(string*student pArray){
	int i;//counter
	int j;//counter
	for(i=0;i<sNUMBER-1;i++){
		for(j=0;j<sNUMBER-1-i;j++){
			//combine first and last names into full names
			if(strcmp((pArray->GetsName().GetfName())+=" " +=(pArray->GetsName().GetlName()),((pArray+1)->GetsName().GetfName())+=" "+=((pArray+1)->GetsName().GetlName()))>0){
				swap(pArray[j],pArray[j+1]);
			}
		}
	}
}




These errors also occur with sPrint and alpha.

I have googled these errors multiple times, and I haven't come across any information that would be helpful in my situation.

Any help would be appreciated.Thank you in advance!
Last edited on
Hi CPlusPlusBeginner,

I have been looking at your code, trying to ascertain the errors since your debugging output isn't formatted so that I can read it. Anyway after trying to debug it myself, there are several problems.

Firstly, you declare functions tPrint, sPrint and alpha (generate doesn't appear to be used), then define them below.

Your definitions for tPrint and sPrint indicate that it takes a string* and an int. However your definition of these functions are written improperly, since you use your dummy string*, student and a previously undeclared variable pArray. Do you intend pArray to be the string* you pass to tPrint and sPrint? If so, what int do you want to pass to these functions?

Also, since you declare alpha to be a function taking a string*, what is the variable alpha you declare on lines 20 and 30 that you compare to '1' and '0' respectively (obviously it cant be the return value from sPrint and tPrint since they have none!).

Try these out, then it might help some of the other problems.
dangrr888- Thank you for your response. Let me post all of my code along with the errors so that you can get a better understanding of where I am trying to go with this. I obviously didn't think this through very well when I posted this, and for that I am sorry.

Here is my entire main function code:
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "address.h"
#include "student.h"
#include "date.h"
#include "record.h"
#include "name.h"

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



using namespace :: std;



#define sNUMBER 50


void tPrint(string*student, int);
void sPrint(string*student, int);
void alpha(string*student);
int generate();
//Function Definitions
void tPrint(string*student pArray){
	if(alpha != '1'){
		alpha(pArray);
	}
	int i;
	for(i=0; i<sNUMBER;i++){
		pArray->tPrint();
		pArray+=1;
	}
}
void sPrint(string*student pArray){
	if(alpha != 0){
		alpha(pArray);
	}
	int i;
	for(i=0; i<sNUMBER;i++){
		pArray->sPrint();
		pArray+=1;
	}
}
void alpha(string*student pArray){
	int i;//counter
	int j;//counter
	for(i=0;i<sNUMBER-1;i++){
		for(j=0;j<sNUMBER-1-i;j++){
			//combine first and last names into full names
			if(strcmp((pArray->GetsName().GetfName())+=" " +=(pArray->GetsName().GetlName()),((pArray+1)->GetsName().GetfName())+=" "+=((pArray+1)->GetsName().GetlName()))>0){
				swap(pArray[j],pArray[j+1]);
			}
		}
	}
}


int generate () {
  srand(sNUMBER);
  ofstream data;
  data.open ("data.txt");
  if(data.is_open()){
	 int i; // counter
	 int j; // counter
	//Name info
	 string new fName[sNUMBER];
	 string new lName[sNUMBER];
	//Address info
	 string new add1[sNUMBER];
	 string new add2[sNUMBER];

	//variables to generate names
	 string new fName1[5] = {"Alfred","Robert","Ryan","Hank","Richard"};
	 string new fName2[5] = {"Hannah","Lacie","Crystal","Kayce","Susan"};
	 string new lName1[5] = {"Smith","Jones","Allison","Johnson","Moore"};
	 string new lName2[5] = {"Howard","Williams","Pinkston","Cooley","Vernon"};
	 string new fNames[2] = {fName1,fName2};
	 string new lNames[2] = {lName1,lName2};

	 // variables to generate addresses
	 string new adds1[5] = {"159 faternity st.","399 Dorm Blvd.","222 faternity st.", "777 Dorm Blvd", "82 Canal street"};
	 string new adds2[10] = {"Apartment A", "Apartment B", "Apartment C", "Apartment D", "Apartment E","Apartment F", "Apartment G", "Apartment H", "Apartment I"};

	 for(i=0;i<sNUMBER;i++){
		 for(j=0; j<5; j++){
			strCopy(fName[i],fNames[i>25][j]);
			strCopy(lName[i],lNames[i>25][j + floor(i/5)-(i>25)*(floor(i/5)-6)- 4*((j+i/5)>5)]);

		 }
	 }





	for(i=0;i<sNUMBER;i++){
		 for(j=0; j<10; j++){
			strCopy(add1[i],adds1[j]);
			strCopy(add2[i],adds2[j + floor(i/10)-(i>25)*(floor(i/10)-11)- 4*((j+i/10)>10)]);

		 }
	 }


	//deallocate unneeded variables
	delete [] fName1;
	delete [] fName2;
	delete [] lName1;
	delete [] lName2;
	delete [] fNames;
	delete [] lNames;

	 for(i=0; i<sNUMBER;i++){
			// name info
			Data << fName[i] << "\n"; // first name
			Data << lName[i] << "\n"; // last name
			// address info
			Data << add1[i] <" \n"; // add 1
			Data << add2[i] <" \n"; // add 2
			Data << "Indianapolis  \n"; // city
			Data << "IN  \n"; // state
			Data << "46168 \n"; // zip
			// birthdate
			Data << rand()%28 + 1  << " \n"; //day

			Data << rand()%12 + 1 << " \n";//month
			Data << 2011 -(rand()%49 + 12)<< "\n";//year
			//graddate
			Data << rand()%28 + 1 << " \n";//day

			Data << rand()%12 + 1 << " \n"; // month
			Data << 2011 + (rand()%6+2)<< " \n"; // year
			//performance info
		    Data << (rand()%16)/(float rand()%4)<<" \n"; //gpa
			Data << (rand()%300)<< " \n"; //credits

	 }

	 //Deallocate rest of memory
		delete [] fName;
		delete [] lName;
		delete [] add1;
		delete [] add2;


	 data.close();
	 } else {
		cout << "Your file did not open. Sorry \n";
	 }
  return 0;
}


int main(){
		ifstream data;
		data.open("data.txt",ios::in);
		student new students[sNUMBER];
		string sInfo;
		char request;
		int i = 0; // counter
		int alpha=0;

		if(!data.is_open()){ //creates a data.txt file if needed
			data.close("data.txt");
			generate();
			data.open("data.txt");
		}
		if(data.is_open()){
			while ( data.good() || i<sNUMBER){


				//AssignNames
				getline(data,sInfo);
				students[i].GetsName().SetfName(sInfo);
				getline(data,sInfo);
				students[i].GetsName().SetlName(sInfo);
				//AssignAddress
				getline(data,sInfo);
				students[i].GetsAddress().Setadd1();
				getline(data,sInfo);
				students[i].GetsAddress().Setadd2(sInfo);
				getline(data,sInfo);
				students[i].GetsAddress().Setcity(sInfo);
				getline(data,sInfo);
				students[i].GetsAddress().Setstate(sInfo);
				getline(data,sInfo);
				students[i].GetsAddress().Setzip(int(sInfo));
				//AssignBirthDate
				getline(data,sInfo);
				students[i].GetbDate().Setday(int(sInfo));
				getline(data,sInfo);
				students[i].GetbDate().Setmon(int(sInfo));
				getline(data,sInfo);
				students[i].GetbDate().Setyr(int(sInfo));
				//AssignGradDate
				getline(data,sInfo);
				students[i].GetgDate().Setday(int(sInfo));
				getline(data,sInfo);
				students[i].GetgDate().Setmon(int(sInfo));
				getline(data,sInfo);
				students[i].GetgDate().Setyr(int(sInfo));
				//AssignPerformance
				getline(data,sInfo);
				students[i].GetsRecord().Setgpa(float(sInfo));
				getline(data,sInfo);
				students[i].GetsRecord().Setcredit(int(sInfo));
				i++

			}

			cout << "Would you like to print a full list? Press Y for Yes, N for No, and press enter. \n";
			cin >> request;
			if(request == 'y' || Request =='Y'){
				cout << " Would you like the list to be alphabatized? Press Y for Yes, N for No, and press enter. \n";
				cin >> request;
				if(request == 'y' || Request =='Y'){
					alpha = 1;
				}
				cout << " Would you like the list to be Simple? In other words this list only comes back with the student's name. Press Y for yes, N for no, and press enter. \n";
				cin >> request;
				if(request == 'y' || Request =='Y'){
					sPrint(*students,alpha);
				} else {
					tPrint(*students,alpha);
				}
			}
			cout << "Thank you for using the Student Record System! \n";
		} else {
			cout << "Your file did not open. Sorry.";
		}
		data.close("data.txt");
		//deallocate memory
		delete [] students;
	return 0;
}

I've been thinking I might need to re-write this code as I have so many errors that I cannot figure out.
Here are the errors that I am getting (Yes, it is a mess!):

C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|26|error: expected ‘,’ or ‘...’ before ‘pArray’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp||In function ‘void tPrint(std::string*)’:|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|27|error: ISO C++ forbids comparison between pointer and integer|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|28|error: ‘pArray’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|32|error: ‘pArray’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|36|error: expected ‘,’ or ‘...’ before ‘pArray’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp||In function ‘void sPrint(std::string*)’:|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|38|error: ‘pArray’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|42|error: ‘pArray’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|46|error: expected ‘,’ or ‘...’ before ‘pArray’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp||In function ‘void alpha(std::string*)’:|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|52|error: ‘pArray’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|52|error: ‘strcmp’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp||In function ‘int generate()’:|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|68|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|69|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|71|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|72|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|75|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|76|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|77|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|78|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|79|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|80|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|83|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|84|error: expected unqualified-id before ‘new’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|88|error: ‘fName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|88|error: ‘fNames’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|88|error: ‘strCopy’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|89|error: ‘lName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|89|error: ‘lNames’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|89|error: ‘floor’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|100|error: ‘add1’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|100|error: ‘adds1’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|100|error: ‘strCopy’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|101|error: ‘add2’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|101|error: ‘adds2’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|101|error: ‘floor’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|108|error: ‘fName1’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|109|error: ‘fName2’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|110|error: ‘lName1’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|111|error: ‘lName2’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|112|error: ‘fNames’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|113|error: ‘lNames’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|117|error: ‘Data’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|117|error: ‘fName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|118|error: ‘lName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|120|error: ‘add1’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|121|error: ‘add2’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|136|error: expected primary-expression before ‘float’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|136|error: expected `)' before ‘float’|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|142|error: ‘fName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|143|error: ‘lName’ was not declared in this scope|
C:\Users\Linzleel-Laptop2\Desktop\CSCI 240\Heap2\main.cpp|144|error: ‘add1’ was not declared in this scope|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build finished: 50 errors, 0 warnings ===|
Hi CpluPlusBeginner,

yes its a mess indeed! If you haven't rewritten the code already, please try to invoke my above suggestions and see how that helps. Also, if you could somehow make it that your error messages appear with the characters on the terminal that would help a lot.

Cheers
Topic archived. No new replies allowed.