Trying to debug, stuck!!

Pages: 12
Trying to debug for an assignment. I'm stuck at the line "MyStruct aStruct = { 4,3.5, MyEnum[],"Dogs" };" Not sure if I'm inputting something wrong or what.

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
  // Week 2 Assignment-1
// Description: Compile time errors in C Strings, Arrays, std:string,
//  struct, enum, pointers, and std::array.
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <cstring> // provides access to std:array
#include <string> // required for getline

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
const int arraySize = 4; // **there is a subtle bug here (needs "const")
enum MyEnum // Needs to be before the struct that uses it
{
	Dog, Cat, Fish, Squirrel
};

struct MyStruct
{
	int a;
	float b;
	string c;
	MyEnum d;
};

//--end of global constants---------
//----------------------------------


//**begin main program**************
int main()
{
	// Initialization
	char myCString[arraySize] = { 0 };
	char myOtherCString[] = { "Yet another string" };
	int myInt[4] = { 27, 39, 0, 42 };
	string myString;
	string MyEnum[] = { "Dog","Cat","Fish","Squirrel" };
	MyStruct aStruct = { 4,3.5, MyEnum[],"Dogs" };
	int x;
	int * pX = &x;
	array <MyStruct, arraySize> Animals;
	// Storing values in uninitialized variables
	myCString[0] = 'A';
	myString = "A third string";
	x = 4;
	for (int i = 0; i<arraySize; i++)
	{
		Animals[i].a = rand() % 10;
		Animals[i].b = rand() % 100 / 100.0;
		Animals[i].c = MyEnum(rand() % 4);
		Animals[i].d = 
		cout << "Enter a name: ";
		getline(cin, Animals[i].d);
	}
	// Display the data
	cout << "myCString = " << myCString << endl;
	cout << "myOtherCString = " << myOtherCString << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << "myInt[" << i << "] = " << myInt[i] << endl;
	}
	cout << "aStruct data: a = " << aStruct.a << " b = " << aStruct.b << "  c = " << aStruct.c << " d = " << aStruct.d << endl;
	cout << "x = " << x << endl;
	cout << "value pointed to by pX = " << *pX << endl;
	for (int i = 0; i< arraySize; i++)
	{
		cout << "Animals[" << i << "].a = " << Animals[i].a << endl;
		cout << "Animals[" << i << "].b = " << Animals[i].b << endl;
		cout << "Animals[" << i << "].c = " << Animals[i].c << endl;
		cout << "Animals[" << i << "].d = " << Animals[i].d << endl;
	}
	// Wait for user input to close program when debugging.
	cin.get();
	return 0;
}
//--end of main program-------------
//----------------------------------
Put the code you need help with here.
Hi,
Firstly, you have two identifiers with the same name. They are :
1
2
enum MyEnum;
string MyEnum[] = {}; 


Make changes to them so that they have different names, like this :
1
2
enum MyEnum;
string MyEnumName[] = {}; 
OK I changed the second one to MyEnumName and I'm still getting an error by the second bracket []
Secondly,
1
2
3
4
5
6
7
struct MyStruct
{
	int a;
	float b;
	string c;
	MyEnum d;
};


You initialise the members of MyStruct in order, but this one is messed up :
1
2
MyStruct aStruct = { 4, 3.5, MyEnum[], "Dogs" };
// int a, double b, MyEnum (d), string (c) 


The correct order should be : int a, double b, string c, MyEnum d

Also, use MyEnum::something to access a desired member in the enum that you want

MyStruct aStruct = { 4, 3.5, "Dogs", MyEnum:: Dog};
Does that help you? :)
Did I do the pointer towards the x correctly? I think I put &x
The next error is now showing up on Animals. Do I have to identify that?
Thanks for your help by the way.

I tried to change Animals to Animals[i] and now the error is under the i saying I need to identify it even though it gets identified?!? So confused.
Last edited on
Can you show us the full complier error message?
This comment is messed up :
#include <cstring> // provides access to std:array

You actually forgot to include the <array> header.
#include <array> // provides access to std:array
Does that help you? :)
Last edited on
Boy that helps a ton, lol!
I have two more errors but I'll try to figure them out myself.
Just ask us when you're stuck again!
Glad it helped :)
I'm sorry, one more, and to add to my code troubles, the electricity went out. The last error is the getline error. Do I need to change the arraySize?
> The last error is the getline error.
getline(cin, Animals[i].d);

The getline() function only accepts a std::string, not an enum

==>
getline(cin, Animals[i].c);
Does that help? :)
The code ran fine but the animal d line is coming up with a funky negative number. I think I need to write a line so the code changes from number to word so the output will be theasier type of animal. This code is so different from the one I practised with.
So can you let us see your program output please?
So here is the code I have, that runs.
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
// Week 2 Assignment-1
// Description: Compile time errors in C Strings, Arrays, std:string,
//  struct, enum, pointers, and std::array.
//----------------------------------

//**begin #include files************
#include <iostream> // provides access to cin and cout
#include <array> // provides access to std:array
#include <string> // required for getline

//--end of #include files-----------
//----------------------------------

using namespace std;
//----------------------------------

//**begin global constants**********
const int arraySize = 4; // **there is a subtle bug here (needs "const")
enum MyEnum // Needs to be before the struct that uses it
{
	Dog, Cat, Fish, Squirrel
};

struct MyStruct
{
	int a;
	float b;
	string c;
	MyEnum d;
};

//--end of global constants---------
//----------------------------------


//**begin main program**************
int main()
{
	// Initialization
	char myCString[arraySize] = { 0 };
	char myOtherCString[] = { "Yet another string" };
	int myInt[4] = { 27, 39, 0, 42 };
	string myString;
	string MyEnumName[] = { "Dog","Cat","Fish","Squirrel" };
	MyStruct aStruct = { 4,3.5, "Dogs", MyEnum::Dog };
	int x;
	int * pX = &x;
	array <MyStruct, arraySize> Animals;
	// Storing values in uninitialized variables
	myCString[0] = 'A';
	myString = "A third string";
	x = 4;
	for (int i = 0; i<arraySize; i++)
	{
		Animals[i].a = rand() % 10;
		Animals[i].b = rand() % 100 / 100.0;
		Animals[i].c = MyEnum(rand() % 4);
		cout << "Enter a name: ";
		getline(cin, Animals[i].c);
	}
	// Display the data
	cout << "myCString = " << myCString << endl;
	cout << "myOtherCString = " << myOtherCString << endl;
	for (int i = 0; i < 4; i++)
	{
		cout << "myInt[" << i << "] = " << myInt[i] << endl;
	}
	cout << "aStruct data: a = " << aStruct.a << " b = " << aStruct.b << "  c = " << aStruct.c << " d = " << aStruct.d << endl;
	cout << "x = " << x << endl;
	cout << "value pointed to by pX = " << *pX << endl;
	for (int i = 0; i< arraySize; i++)
	{
		cout << "Animals[" << i << "].a = " << Animals[i].a << endl;
		cout << "Animals[" << i << "].b = " << Animals[i].b << endl;
		cout << "Animals[" << i << "].c = " << Animals[i].c << endl;
		cout << "Animals[" << i << "].d = " << Animals[i].d << endl;
	}
	// Wait for user input to close program when debugging.
	cin.get();
	return 0;
}
The value of Animal[i].d is not set. It should be :
Animals[i].d = MyEnum(rand() % 4);
Does that help you? : )
But that's the value for c. Can it be the same value for both?
Pages: 12