Getline doesn't work

Hello everyone! I am new to c++ and i am working on a program to practise string and getline, but unfortunately the getline doesnt even ask for input. Here is the code
1
2
3
4
5
6
7
8
9
10
   cout<<"What's the name of the killer in the movie called 'Halloween'"<<endl;
    string answer;
    getline(cin, answer);
    if (answer=="Michael Myers")
        cout<<"Correct! Michael Myers is the killer in 'Halloween'!"<<endl;
    else
    {
        cout<<"Incorrect! The killer in 'Halloween' is called Michael Myers!";
        return 1;
    }
http://www.cplusplus.com/forum/articles/40071/#msg216313
The problem may be some lines above what the error message points to
Oh, sorry. The program is big, but there it is.

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
SetConsoleTextAttribute(h,15);
    cout << "Hi There! This is a Horror Movie Quiz #1. Hope you enjoy it. Let's get started!" << endl;
    cout<<"We all know the movie called 'Friday The 13th' aren't we?Tell me the first and the last name of the killer in this movie"<<endl;
    string answer1;
    getline(cin, answer1);
    if (answer1=="Pamela Voorhees")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"True! Jason was ruled by his mother, so she is the real killer!"<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"False! Jason was ruled by his mother, so this make her the real killer in the movie!"<<endl;
    return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"Next question! What's the name of the killer in the movie called 'Nightmares on Elm street'?"<<endl;
    string answer2;
    getline(cin, answer2);
    if (answer2=="Freddy Krueger")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"True! This scorched man with the sharp knives instead of fingers. Can't forget him."<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"False! The correct answer is Freddy Krueger. He is the killer in this movie!";
    return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"Good job. Next question! How many movies about Jason Voorhees were made?"<<endl;
    string answer3;
    cin>>answer3;
    if (answer3=="12")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"True! There are 12 movies about Jason, one of them includes Freddy Krueger."<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"False! There are 12 movies including Jason, one of them includes Freddy Krueger."<<endl;
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"You are doing great!Next question! How many movies about Freddy Krueger were made?"<<endl;
    string answer4;
    cin>>answer4;
    if (answer4=="9")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"Correct! There are 9 movies about Freddy Krueger, one of them as i told you earlier contains Jason Voorhees."<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"Incorrect! There are 9 movies about Freddy Krueger, one of them as i told you includes Jason Voorhees."<<endl;
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"Nice! Let's try something harder!What's the name of the murderer who lives inside of the Chucky doll's body?"<<endl;
    string answer5;
    cin>>answer5;
    if (answer5=="Charles")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"True! Charles is the name of the murderer inside Chucky's body."<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"False! Charles is the name of the murderer inside Chucky's body."<<endl;
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"You're amazing. The questions are getting harder and harder. How many 'Saw' movies were made so far?"<<endl;
    string answer6;
    cin>>answer6;
    if (answer6=="8")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"Correct! There are 8 'Saw' movies out there, but i think the first one was the best."<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"Incorrect! There are 8 'Saw' movies out there.";
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"Nice! Next question. How many 'Texas Chainsaw Massacre' movies were made?"<<endl;
    string answer7;
    cin>>answer7;
    if (answer7=="8")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"You're right! There are 8 'Texas Chainsaw Massacre' movies out there, all of them are full with bloody scenes, brutal killing and more"<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"Wrong! There are 8 'Texas Chainsaw Massacre' movies out there!"<<endl;
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"Good Job! Let's check if you know the new movies as the old classics!"<<endl;
    cout<<"Next question! As we all know there are 3 'It' movies so far, but in what year was released the one that have the most expensive budget?"<<endl;
    string answer8;
    cin>>answer8;
    if (answer8=="2019")
    {
        SetConsoleTextAttribute(h,10);
        cout<<"Correct! It 1990 took 12 Million, It Chapter 1 took 35 and It Chapter 2 took 79 Million and was released in 2019!"<<endl;
    }
    else
    {
        SetConsoleTextAttribute(h,4);
        cout<<"Incorrect! The most expensive 'It' movie is It Chapter 2 with 79 million dollars and it was released in 2019!"<<endl;
        return 1;
    }
    SetConsoleTextAttribute(h,15);
    cout<<"You are doing amazing, but let's see if you are able to answer the last question!"<<endl;
    cout<<"What's the name of the killer in the movie called 'Halloween'"<<endl;
    string answer;
    getline(cin, answer);
    if (answer=="Michael Myers")
        cout<<"Correct! Michael Myers is the killer in 'Halloween'!"<<endl;
    else
    {
        cout<<"Incorrect! The killer in 'Halloween' is called Michael Myers!";
        return 1;
    }
    cout<<"You are a legend! Your horror movie knowledge is amazing. Thanks for trying out Horror movie Quiz and have a nice day!";
    return 0;
}
say that this is your input buffer"2019\nMichael Myers\n"
then you do cin>>answer8;, so 2019 is put into the string,
now the input buffer is "\nMichael Myers\n"
so the first character is an end-of-line, and getline() will give you an empty string.

you need to discard that '\n', there are several ways
1
2
cin>>answer8;
cin.ignore(); //discard one character 


getline(cin >> ws, answer); //ignores all whitespace before

1
2
while(getline(cin, answer) and answer.empty()) //loops until the user inputs something
   ;
What do i have to put in the brackets at the first method with cin.ignore?
Nothing.
Thanks. It worked!
Topic archived. No new replies allowed.