Function Trouble

This is probably a long shot but I'll ask anyway because I can't figure it out.

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
  #include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

void advyesorno(void);

int main()
{
	advyesorno();
	return 0;
}

void advyesorno (void)
{
	string adv;
	cout << "Now that we have been introduced, would you like to go on an "
		 << "adventure with me?" << endl;
	getline(cin, adv);

	if (adv == "Yes")
		cout << "That's great! Let's go!" << endl;

	else if (adv == "No")
		cout << " I won't take no for an answer! Let's go!" << endl;
}

This is the function I'm having trouble with. When I run it as it's own program it works fine but when I put it in my program:
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

void getage(void);								//function prototypes
void getgender(void);
void advyesorno(void);

string strusername;

int main()
{
	cout << "Loading";
	for(char Ldprd = 0; Ldprd < 3; Ldprd++)		//Ldprd shorthand for Loading Period...
	{cout << '.';}
	cout << endl;

	cout << "Hello! My name is Maximillian! What is your name?" << endl;
	string strusername;
	getline(cin, strusername);
	cout << "Nice to meet you," << strusername << "!\n" << endl;

	getgender();

	getage();
	
	cout << " Well it is very nice to meet you " << strusername << "!" << endl;
	
	advyesorno();

	



	

	return 0;
}

void getage(void)
{
	 int age;
	cout << "How old are you, if I may ask? (Please, only numbers, no letters)" << endl;
	do
	{
		cin >> age;

		if (age < 0)
			cout << "You can't be younger than zero! Try again." << endl;
		else if (age > 100)
			cout << "That would be too old to play this game! Put in your real age." << endl;
		
	} while (age < 0 || age > 100);
		cout << age << "! Is that right?";
			
}

void getgender(void)
{
	string cGender;
	cout << "Are you a Boy or a Girl, my child?" << endl;
	do
	{

		getline(cin, cGender);

		if (cGender == "Boy")
			cout << "Oh boy! A boy you are indeed!\n" << endl;
		
		else if (cGender == "boy")
			cout << "Oh boy! A boy you are indeed!\n" << endl;
		
		else if (cGender == "Girl")
			cout << "Oh alright, you are a girl I see!\n" << endl;

		else if (cGender == "girl")
			cout << "Oh alright, you are a girl I see!\n" << endl;
		
		else
			cout << "That's not true! Please tell, Boy or Girl?" << endl;
		 
	} while (cGender != "Boy" && cGender != "Girl" 
			&& cGender != "boy" && cGender != "girl"); 
	cout << endl;
}

void advyesorno (void)
{
	string adv;
	cout << "Now that we have been introduced, would you like to go on an "
		 << "adventure with me?" << endl;
	getline(cin, adv);

	if (adv == "Yes")
		cout << "That's great! Let's go!" << endl;

	else if (adv == "No")
		cout << " I won't take no for an answer! Let's go!" << endl;
}

It will not ask for user input, it will just cout the sentence and then say "Press any key to continue." Try compiling it and you will see what I mean. Any help and advice is appreciated.
This is because the getine() on line 93 will just eat up the newline left by the cin on line 47. To fix this, you can use this little snippet to ignore everything else:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <limits>
//...

void advyesorno(void) {
    // ...
    // Ignore all characters up to and including the next newline.
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::getline(std::cin, adv);
    
    if (adv == "Yes")
        std::cout << "..." << std::endl;
    else if (adv == "No")
        std::cout << "..." << std::endl;
    else {
        // process alternate inputs here,
        // maybe loop back to the input stage again?
    }
}
Topic archived. No new replies allowed.