getline not working

In my program the getline doesn't seem to be working.

source.cpp
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
  #include "critter.h"

int main()
{
	critter Animal;
	std::string name;
	int choice;

	std::cout << "What would you like to do: " << endl;
	std::cout << "1: Set name" << endl;
	std::cout << "2: Set bordom" << endl;
	std::cout << "3: Set hunger" << endl;
	std::cout << "4: Get name" << endl;
	std::cout << "5: Get bordom" << endl;
	std::cout << "6: Get hunger" << endl;
	std::cout << "7: Talk" << endl;
	std::cout << "8: Feed" << endl;
	std::cout << "9: Play" << endl;
	std::cout << "10: Quit" << endl;

	std::cin >> choice;

	switch (choice)
	{
	case 1:
		std::cout << "What would you like to name your critter?";
		std::getline (std::cin,name);
		Animal.SetName(name);
		break;
	case 2:
		std::cout << "What Would you like to set the bordom to?" << endl;
		break;
	}

	return 0;
}

critter.h
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
//Critter Class definition is within header file
#ifndef _CRITTER_H
#define _CRITTER_H
#include <iostream>
#include <string>
using namespace std;

class critter
{
public:            // begin public section
    // constructor
    critter();   

    // member functions 
    int GetHunger(); 
	int GetBordom();
	string GetName();
    void SetHunger(int hunger);
	void SetBordom(int bordom);
	void SetName(string name);
	
    
private:           // begin private section
    int myHunger;
	int myBordom;
	string myName;
};

critter::critter()
{
    myHunger = 0;
    cout << "A new critter has been born!" << endl;
}

int critter::GetHunger() 
{
    return myHunger;
}

int critter::GetBordom()
{
	return myBordom;
}

string critter::GetName()
{
	return myName;
}

void critter::SetHunger(int hunger)
{
    if (hunger < 0)
        cout << "You can't set a critter's hunger to a negative number.\n\n";
    else
        myHunger = hunger;
}

void critter::SetBordom(int bordom)
{
	if (bordom < 0)
        cout << "You can't set a critter's bordom to a negative number.\n\n";
    else
        myBordom = bordom;
}
void critter::SetName(string name)
{
	myName = name;
}

#endif 

output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
A new critter has been born!
What would you like to do:
1: Set name
2: Set bordom
3: Set hunger
4: Get name
5: Get bordom
6: Get hunger
7: Talk
8: Feed
9: Play
10: Quit
1
What would you like to name your critter?Press any key to continue . . .
That's because you're using std::cin::operator>> and std::getline interchangeably.

If you enter "1 5", 1 will be read by std::cin::operator>>, but " 5" will still be in the stream.

Try using getline exclusively. You'll need to convert strings to the type you want, which should be trivial with std::stringstream.
After you enter 1 and press enter there is a newline in the stream which stops getline. Change your getline call to std::getline (std::cin>>std::ws,name); http://www.cplusplus.com/reference/istream/ws/
Topic archived. No new replies allowed.