Why is this coming up with undeclared identifier errors?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 5945 Views - Last Post: 09 July 2012 - 12:06 AM Rate Topic: ***-- 2 Votes

#1 SaltyPeaches  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 11

Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 12:14 AM

So, I am supposed to create a program that creates a "classroom" structure, which contains a bunch of data about a specific room. It's then meant to allow the user to input data, print data, and compare two classrooms and output which classroom has more seating.

My problem is, when I try to call the specific properties of a room, I get an error. I am also getting a lot of "undeclared identifier" errors, but I've checked every variable in my code and I put a data type in front of each one.

I'll post the entire code, because the problems occur throughout the entire program. I even get an error when I try to use ">>" with "cin". It comes up with "no operator found which takes a left-hand operand...". I haven't done anything in C++ for over a year, so this is all really confusing to me at the moment.

Sorry if these seem like really simple and borderline annoying questions for you guys/girls. I'm just stuck and don't know what to do.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct classroom{
	int room_number;
	string lecture_name;
	vector<string> students;
	int chairs;
	bool window;
	bool projector;
	bool available;
} room;

void addData();
void printData();

int main()
{

	cout << "How many classrooms are there?" << endl;
	cin >> int number_of_classrooms;
	classroom Rooms[number_of_classrooms];

	for (int i = 0; i < number_of_classrooms+1; ++i){
		cout <<"Please add data for classroom "<< i << endl;
		addData(Rooms[i]);
	}

	cout << "Would you like to compare classroom sizes?" << endl;
	cin >> bool compare;
	if (compare == TRUE){
		cout << "What is the first classroom you would like to compare?" << endl;
		cin >> int Class1;
		cout << "What is the second classroom you would like to compare?" << endl;
		cin >> int Class2;

		if (room[Class1].chairs > room[Class2].chairs){
			cout << "Classroom " << Class1 << " is larger." << endl;
		}
		else if (Rooms[Class1].chairs < Rooms[Class2].chairs){
			cout << "Classroom " << Class2 << " is larger." << endl;
		}
		else if (Rooms[Class1].chairs == Rooms[Class2].chairs){
			cout << "These classrooms have the same number of chairs" << endl;
		}
	}
	else if (compare == FALSE){
		cout << "Fine, be that way." << endl;
	}
	
	cout << "Would you like to print classroom data?" << endl;
	cin >> bool print;
	if (print == TRUE){
		cout << "Which classroom would you like to print data for?" << endl;
		cin >> int classroom_choice;
		if (classroom_choice <= number_of_classrooms){
			printData(Rooms[classroom_choice]);
		}
		else if (classroom_choice > number_of_classrooms){
			cout << "ERROR" << endl;
			cout << "INPUT VALUE EXCEEDS NUMBER OF CLASSROOMS" << endl;
		}
	}
	else if (print == FALSE){
		cout << "Thank you and have a pleasant day!" << endl;
	}

    return 0;
}

void addData(classroom& room){
	cin >> room.room_number;
	cin >> room.lecture_name;
	cin >> room.chairs;
	cin >> room.window;
	cin >> room.projector;
	cin >> room.available;

	string student_name;
	cout<< "Please enter student names: " << endl;
	cout<< "If there are no more students, enter 'void'" << endl;
	while (student_name != "void"){
		getline(cin, student_name);
		room.students.push_back(student_name);
	}
}

void printData(classroom& room){
	cout<< "Room Number is: " << room.room_number << endl;
	cout<< "Lecture Name is: " << room.lecture_name << endl;
	cout<< "Number of Chairs is: " << room.chairs << endl;
	cout<< "Window: " << room.window << endl;
	cout<< "Projector: " << room.projector << endl;
	cout<< "Available: " << room.available << endl;
	cout<< "The students are: " << endl;
	for (vector<string>::iterator i = room.students.begin(); i != room.students.end(); ++i){
		cout<< *i << endl;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Why is this coming up with undeclared identifier errors?

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,174
  • Joined: 08-January 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 12:37 AM

Here are a few of your mistakes - the declaration and definition of addData do not match. In declaration, it has no parameters while in definition it has one parameter. Also, you CANNOT declare variables in an cin statement. So, for instance, line 24 should be broken down into two lines -
int number_of_classrooms;
cin >> number_of_classrooms;


You will need to change this everywhere in your code. Also, somewhere you have written
if(compare==TRUE)

TRUE is no value. It is true, in lowercase.
If you have any more problems, please post your updated code and ask questions.

This post has been edited by aresh: 08 July 2012 - 12:37 AM

Was This Post Helpful? 1
  • +
  • -

#3 Jimmy Rose  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 02-July 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 12:37 AM

cin >> int number_of_classrooms;

You have to declare the variable before putting it into the stream.
Was This Post Helpful? 1
  • +
  • -

#4 Jimmy Rose  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 02-July 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 12:50 AM

classroom Rooms[number_of_classrooms];

arrays have to be constant expressions.
Maybe this instead:
vector<classroom> Rooms(number_of_classrooms);

Your function prototypes need arguments
Was This Post Helpful? 2
  • +
  • -

#5 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,174
  • Joined: 08-January 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 12:53 AM

Jimmy, you are wrong. Arrays need not be constant expressions. If you use some outdated compiler like TC++ then they need to be constant. But as far as most modern compilers are concerned, variable length arrays are perfectly legal.
Was This Post Helpful? 1
  • +
  • -

#6 Jimmy Rose  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 02-July 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 01:16 AM

View Postaresh, on 08 July 2012 - 12:53 AM, said:

Jimmy, you are wrong. Arrays need not be constant expressions. If you use some outdated compiler like TC++ then they need to be constant. But as far as most modern compilers are concerned, variable length arrays are perfectly legal.

The number of elements of a named array must be known at compile time. If you want the number of elements to be a variable, you must put it on the free store and access it through a pointer. That's what vector does with its array of elements.
- Bjarne Stroustrup
Was This Post Helpful? 2
  • +
  • -

#7 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,174
  • Joined: 08-January 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 01:19 AM

This was a topic which I started some time ago. As you can see, C99 onwards, variable length arrays are legal. What you are quoting must be REALLY old.

P.S. Please read upto reply #6 to get the proper answer.

This post has been edited by aresh: 08 July 2012 - 01:20 AM

Was This Post Helpful? 1
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1678
  • View blog
  • Posts: 3,180
  • Joined: 30-May 10

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 02:29 AM

> As you can see, C99 onwards, variable length arrays are legal. What you are quoting must be REALLY old.
Ah, but the C99 standard only relates to C.
Variable length arrays are not supported (AFAIK) in any C++ standard.
Was This Post Helpful? 2
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,174
  • Joined: 08-January 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 02:33 AM

Hmm.. It appears you are right. But if it has been added to C, the why no to C++ ??
Was This Post Helpful? 1
  • +
  • -

#10 Jimmy Rose  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 02-July 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 02:40 AM

View Postaresh, on 08 July 2012 - 01:19 AM, said:

This was a topic which I started some time ago. As you can see, C99 onwards, variable length arrays are legal. What you are quoting must be REALLY old.

P.S. Please read upto reply #6 to get the proper answer.


The book I'm quoting is: Programming Principles and Practice Using C++ (2008)
I get an error in C++:
error C2057: expected constant expression

It seems to work with some people but a lot of experts refer to VLA as unsafe code because it's subject to undefined behavior many prefer to use dynamically-allocated arrays instead, either way here's a link to anyone who's interested on the C99 standard I would suggest reading:
http://www.open-std..../docs/n1124.pdf
Here's a link to Incompatibilities Between ISO C and ISO C++: http://david.tribble...text/cdiffs.htm
It states "C++ does not support VLAs" under Variable-length arrays (VLAs).
Was This Post Helpful? 1
  • +
  • -

#11 Jimmy Rose  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 19
  • Joined: 02-July 12

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 02:59 AM

Hello SaltyPeaches are you still working on the code above.
If you are change:
while (student_name != "void"){
		getline(cin, student_name);
		room.students.push_back(student_name);
}


to
while (getline(cin, student_name) && student_name != "void")
		room.students.push_back(student_name);

This way it checks student_name before adding to the array so void won't be added.

While using bool your answers have to be 1(for true) or 0(for false) otherwise you'll get undefined behavior.
Was This Post Helpful? 1
  • +
  • -

#12 SaltyPeaches  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 11

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 03:12 PM

Thank you, guys! You have been incredibly helpful! I changed everything you mentioned. Stupid of me to try declare a variable in the same statement as cin! :oops:

I just have one more quick question. After adjusting all of those and switching my "Rooms" array to a vector, I still get a few errors. Only this time, they are "unresolved token" errors, with a whole lot of symbols rather than words. It's almost like Visual Studio is swearing at me for being so bad at this, and then censoring it out!

Here are the errors I am getting:

1>project.obj : error LNK2028: unresolved token (0A000421) "void __cdecl printData(struct classroom)" (?printData@@$$FYAXUclassroom@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>project.obj : error LNK2028: unresolved token (0A000422) "void __cdecl addData(struct classroom)" (?addData@@$$FYAXUclassroom@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>project.obj : error LNK2019: unresolved external symbol "void __cdecl printData(struct classroom)" (?printData@@$$FYAXUclassroom@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>project.obj : error LNK2019: unresolved external symbol "void __cdecl addData(struct classroom)" (?addData@@$$FYAXUclassroom@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Users\%USER%\Desktop\My Classes\Summer 2012\Programming II\project\Debug\project.exe : fatal error LNK1120: 4 unresolved externals


And here is my updated code:

// project.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct classroom{
	int room_number;
	string lecture_name;
	vector<string> students;
	int chairs;
	bool window;
	bool projector;
	bool available;
} room;

void addData(classroom room);
void printData(classroom room);

int main()
{

	cout << "How many classrooms are there?" << endl;
	int number_of_classrooms;
	cin >> number_of_classrooms;
	vector<classroom> Rooms(number_of_classrooms);

	for (int i = 0; i < number_of_classrooms+1; ++i){
		cout <<"Please add data for classroom "<< i << endl;
		addData(Rooms[i]);
	}

	cout << "Would you like to compare classroom sizes?" << endl;
	cout << "0: No" << endl;
	cout << "1: Yes" << endl;
	bool compare;
	cin >> compare;
	if (compare == true){
		cout << "What is the first classroom you would like to compare?" << endl;
		int Class1;
		cin >> Class1;
		cout << "What is the second classroom you would like to compare?" << endl;
		int Class2;
		cin >> Class2;

		if (Rooms.at(Class1).chairs > Rooms.at(Class2).chairs){
			cout << "Classroom " << Class1 << " is larger." << endl;
		}
		else if (Rooms.at(Class1).chairs < Rooms.at(Class2).chairs){
			cout << "Classroom " << Class2 << " is larger." << endl;
		}
		else if (Rooms.at(Class1).chairs == Rooms.at(Class2).chairs){
			cout << "These classrooms have the same number of chairs" << endl;
		}
	}
	else if (compare == false){
		cout << "Fine, be that way." << endl;
	}
	
	cout << "Would you like to print classroom data?" << endl;
	bool print;
	cin >> print;
	if (print == true){
		cout << "Which classroom would you like to print data for?" << endl;
		int classroom_choice;
		cin >> classroom_choice;
		if (classroom_choice <= number_of_classrooms){
			printData(Rooms.at(classroom_choice));
		}
		else if (classroom_choice > number_of_classrooms){
			cout << "ERROR" << endl;
			cout << "INPUT VALUE EXCEEDS NUMBER OF CLASSROOMS" << endl;
		}
	}
	else if (print == false){
		cout << "Thank you and have a pleasant day!" << endl;
	}

    return 0;
}

void addData(classroom& room){
	cout << "Please enter the number of rooms:" << endl;
	cin >> room.room_number;
	cout << "Please enter the name of the lecture:" << endl;
	cin >> room.lecture_name;
	cout << "Please enter the number of chairs:" << endl;
	cin >> room.chairs;
	cout << "Is there a window?" << endl;
	cout << "0: No" << endl;
	cout << "1: Yes" << endl;
	cin >> room.window;
	cout << "Is there a projector?" << endl;
	cout << "0: No" << endl;
	cout << "1: Yes" << endl;
	cin >> room.projector;
	cout << "Is the room available?" << endl;
	cout << "0: No" << endl;
	cout << "1: Yes" << endl;
	cin >> room.available;

	string student_name;
	cout<< "Please enter student names: " << endl;
	cout<< "If there are no more students, enter 'void'" << endl;
	while (getline(cin, student_name) && student_name != "void"){
		room.students.push_back(student_name);
	}
}

void printData(classroom& room){
	cout<< "Room Number is: " << room.room_number << endl;
	cout<< "Lecture Name is: " << room.lecture_name << endl;
	cout<< "Number of Chairs is: " << room.chairs << endl;
	cout<< "Window: " << room.window << endl;
	cout<< "Projector: " << room.projector << endl;
	cout<< "Available: " << room.available << endl;
	cout<< "The students are: " << endl;
	for (vector<string>::iterator i = room.students.begin(); i != room.students.end(); ++i){
		cout<< *i << endl;
	}
}


Was This Post Helpful? 0
  • +
  • -

#13 SaltyPeaches  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 11

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 03:44 PM

Oops! Figured that out! I forgot to include the "&" in my function parameters. It now will display the console window, and I'm just working through a few minor glitches (like how to reference a specific classroom by room number rather than by vector position). Also, I added a utilization calculator (just a percentage of students in relation to the number of chairs), but it keeps coming up with 0%. I'll post the code here if you guys have time to look at it. Thanks again for all your help!

	cout << "What is the first classroom you would like to compare?" << endl;
	int Class1;
	cin >> Class1;
	cout << "What is the second classroom you would like to compare?" << endl;
	int Class2;
	cin >> Class2;

	double utilization_1 = Rooms.at(Class1).students.size() / Rooms.at(Class1).chairs;
	double utilization_2 = Rooms.at(Class2).students.size() / Rooms.at(Class2).chairs;

	if (Rooms.at(Class1).chairs > Rooms.at(Class2).chairs){
		cout << "Classroom " << Class1 << " is larger." << endl;
	}
	else if (Rooms.at(Class1).chairs < Rooms.at(Class2).chairs){
		cout << "Classroom " << Class2 << " is larger." << endl;
	}
	else if (Rooms.at(Class1).chairs == Rooms.at(Class2).chairs){
		cout << "These classrooms have the same number of chairs" << endl;
	}
		
	cout << "Room " << Class1 << " has a utilizaiton of: " << utilization_1 << "%" << endl;
cout << "Room " << Class2 << " has a utilization of: " << utilization_2 << "%" << endl;

Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1348
  • View blog
  • Posts: 4,643
  • Joined: 19-February 09

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 04:52 PM

Hi, if there was 50 students and 100 chairs how would you calculate the percentage?
Was This Post Helpful? 0
  • +
  • -

#15 SaltyPeaches  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-May 11

Re: Why is this coming up with undeclared identifier errors?

Posted 08 July 2012 - 04:56 PM

View Post#define, on 08 July 2012 - 04:52 PM, said:

Hi, if there was 50 students and 100 chairs how would you calculate the percentage?


I want to calculate it by taking the size of the "students" vector and dividing that by the number of chairs. So a class of 50 students and 100 chairs would have a 50% utilization. I'm wondering if I am accessing the size() wrong?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2