Getline, Structutres

I am building a small practice program for fun containing structures to help me learn them. I made the program but when it comes to the drink section it skips the part where it asks you what is your favourite drink and goes straight to the quantity bit, here is the code, Thanks in advance.
P.S. i know i can make this program smaller in text size and more economical, but i am not interested in that, i just want to know why it wont work.

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
#include <iostream>
using namespace std;

struct test {
    int num1;
    string name;
} lolly,drink;

int lollies ();
int drinks ();

int main ()
{
    lollies ();
    drinks ();
}

int lollies ()
{
    int a;
    cout << "What is your favourite lolly: ";
    getline (cin,lolly.name);
    cout << "And when you get " << lolly.name << "s, how many do you get? ";
    cin >> lolly.num1;
    a = lolly.num1;
    a = a-2;
    if ((lolly.num1 == 1) || (lolly.num1 == 2)) 
    {
        a = lolly.num1;
        cout << endl << "You're not too bad I only have " << lolly.num1 << " " << lolly.name << "s too.";
        cout << endl << "But just dont go out and eat too many!!";
    } else {
        cout << endl << "WOW!! You must be huge. I cant beleive you have ";
        cout << lolly.num1 << " " << lolly.name << "s, I only get " << a << " of them.";
    }
    cout << endl << endl << endl << endl;
    return 0;
}

int drinks ()
{
    int a;
    cout << "Now, what's your favourite drink: ";
    getline (cin,drink.name);
    cout << "And how many " << drink.name << "s do you have a day? ";
    cin >> drink.num1;
    a = drink.num1;
    a = a-2;
    if ((drink.num1 == 1) || (drink.num1 == 2))
    {
        a = drink.num1;
        cout << endl << "You're not too bad I only have " << lolly.num1 << " " << lolly.name << "s too.";
        cout << endl << "But if you keep drinking those" << drink.name << "s you are going to get HUGE!!";
    } else {
        cout << endl << "WOW!! You must be huge. I cant beleive you have ";
        cout << drink.num1 << " " << drink.name << "s, I only get " << a << " of them.";
    }
    cout << endl << endl << endl << endl;
    return 0;
}



and this is the output i receive



What is your favourite lolly: lolly1
And when you get lolly1s, how many do you get? 4

WOW!! You must be huge. I cant beleive you have 4 lolly1s, I only get 2 of them.



Now, what's your favourite drink: And how many s do you have a day? 5

WOW!! You must be huge. I cant beleive you have 5 s, I only get 3 of them.



[Press Enter to close window]




As you can see up above the program doesn't wait for a reply for drink.name and it is recorded and nothing.
Last edited on
Add cin.ignore(); before the second getline. I think what's happening is that there's a trailing newline character from when the integer is inputted, so the stream needs to be flushed.
Last edited on
Thanks a lot, it worked perfectly
You can also use gets(drinks.name) to input the character string headerfile for this is stdio
Topic archived. No new replies allowed.