please help me with the following c++ problem


Consider the following class declaration:

class ExamType
{
public:
ExamType();
ExamType(string m, string v, int t, string d);
private:
string module;
string venue;
int time;
string date;
};

Explain what is wrong with the following code fragment and include code to correct it:

int main()
{
ExamType exams[12];
for (int i = 0; i < 12; i++)
if (exams.module[i] == "COS1512")
cout << "COS1512 will be written on "
<< exams.date << " at " << exams.time;
return 0;
}
1_ Please always use code tags, it makes a lot easier to read your code.
2_ If you're asking for a question you should formulate what you don't understand / what you've failed to do.

For people on here to be able to help you better, we'd like to know what your problem is, what's you've tried so far and what didn't work. Also showing your code helps in pointing out what you might have done wrong. However people won't do your homework/tasks *for* you.
Hello sjomelo01,

Like hoogo mentioned

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second lint to be the most help.

My first thought is did you compile the code and what errors did you receive?

As is your code would never compile missing the header files like "iostream: and "string".

The class has some problems because the ctors are prototype, but never defined. The same goes for the dtor.

The private member variables ate defined, but there is no function to retrieve these variables. Generally a "Get" function is defined to access the private variables.Example later.

Here is what the class could and should look more like:
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
class ExamType
{
	public:
		// <--- These ccould be done inside or outside the class.
		ExamType();
		ExamType(std::string m, std::string v, int t, std::string d);
		~ExamType();

		// <--- These could be done inside or outside the class.
		// <--- Example of inside the class.
		std::string GetModule() { return module; }
		std::string GetDate() { return date; }
		int GetTime() { return time; }

	private:
		std::string module;
		std::string venue;
		int time;
		std::string date;
};

// <--- Example of outside the class.
ExamType::ExamType()
{
	time = 0;
}

ExamType::ExamType(std::string m, std::string v, int t, std::string d)
{
	// <--- Your code here to set the private variables of the class.
}

ExamType::~ExamType()
{

}


Inside main line 17 defines an array of 12 elements of type "ExamType" with variable name of exams.

Line 19 the if statement is trying to access the private class variable "module" as an array when it is not, but "exams" is an array that needs to be indexed. Line 19 should read "if (exams[i].GetModule() == "COS1512")". As you can see you are accessing an individual element of the "exams" array to use the method "GetModule" to return the value of "module" stored at that element and compare it to "COS1512". This same concept will need to be used in the "std::cout" statement.

Generally it is better to post the whole code because sometimes the problem may be somewhere other than where you think.

Hope that helps,

Andy
Topic archived. No new replies allowed.