Program skips over cin >> and starts an infinite loop.

I figured it out by changing cin >> eventName2; to getline(cin, eventName2); . However, I'd still like to know why the cin doesn't work?

I'm trying to make a program that declares objects inside classes. For some reason when I try to do this with an event called "Birthday" or "Halloween" it works fine. The problem occurs when I enter an event that has a space in it like "My Birthday" or "Halloween Party". Suddenly the program will ignore the cin >> month and jump straight into the while loop, and for some reason it also skips the user input there and causes an infinite loop of my error message. Can anyone tell me why this is happening. I tried to debug it, and I'm sure there's a way I could use getline. The problem is I've completely forgotten how to use getline (I've only been coding for 3 months or so). I've been staring at this for hours and it's starting to look like gibberish, so I figured I'd ask you guys.

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

void Event::eventSetup()
{
    int leapYearMod = 4;
	string eventName2;
    int monthDays[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31};
    cout << "\n\n\n                   Create an event!                   ";
    cout << "\n\n\nEnter the name of the event: ";
    cin >> eventName;
    cout << "\nEnter the month of the event (number format): ";
    cin >> months;
    while (months > 12 || months < 1)
    {

        if (months > 12 || months < 1)
        {
            cout << "Invalid month. Try again. :";
            cin >> months;
        }
    }
    do
    {
        cout << "\nEnter the day of the event (number format): ";
        cin >> dy;
        if ((leapYearMod % months) == 0)
        {
            if (dy > 29 || dy < 1)
            {
                cout << "invalid day";
            }
        }
        else if (dy > monthDays[months] )
        {
            cout << "Day exceeds number of days in month";
        }
        if (dy < 1)
        {
            cout << "invalid day";
        }
    }
    while (dy >29 || dy < 1);
    cout << "\nEnter the year of the event: ";
    cin >> yr;
    do
    {
        cout << "\nEnter the hour of the event: ";
        cin >> hr;
        if (hr > 12 || hr < 1)
        {
            cout << "Invalid time frame";
        }
    }
    while (hr > 12 || hr < 1);
    do
    {
        cout << "\nEnter the minute of the event: ";
        cin >> mi;
        if (mi > 59 || mi < 0)
        {
            cout << "Invalid time frame";
        }
    }
    while (mi > 59 || mi < 0);
    eventTime.setTime(hr,mi);
    eventDate.setDate(months,dy,yr);

}


If you need more code just ask. I have everything done, just this section is causing me errors.
Last edited on
You should probably change the topic of your post to question or people don't know what your asking from them.
Last edited on
The >> operator stops reading from the input buffer when he encounters a space. When you enter "My Birthday" eventName is set to "My" and then your program moves to the next cin >> statement with the input buffer holding "Birthday". To fix this, get your string using getline, like this: getline(cin,eventName,'\n');

EDIT: Oh, I just saw that you knew that already...
Last edited on
Topic archived. No new replies allowed.