getline help

I am doing some homework and they want me to fix the errors in the code but I can’t find out what is wrong with the getline statement from what I have read the getline uses cin to get the input from the user and then stores it in the Animals[I].d but I am getting error c2780 which says I need 3 arguments. And I am also getting error c2784 that says could not deduce template argument I don’t understand what is wrong could someone help me.


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
 // 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;
	MyStruct aStruct = { 4,3.5,"Dog" ,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].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-------------
//----------------------------------
Getline reads a string from cin and stores it in the variable provided.
In this case it has to store an string in a Enum (integer).

http://www.cplusplus.com/reference/string/string/getline/

Looking at line 56 and 57, I think your mistake is in line 28 and 29.
I knew it was something simple so the fix wasn’t a syntax thing it was that it was using the wrong variable in the wrong place thanks I was working on that all day.
Topic archived. No new replies allowed.