Having trouble looping with getline()

I'm having a problem while trying to loop with the getline function. The program compiles but after being prompted for number of repetitions, it displays:

Name: Month:

when it should display

Name:
Month:

I've tried looking around in the forums and on Google but I'm not getting any help out of them.

Here is my code

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

struct BDay
{
    char    name[50];
    int     month;
    int     day;
    int     year;
};

int main (void)
{
    auto    ofstream    outFile;
    auto    int         reps;
    auto    int         index;
    auto    BDay        info;

    cout << "How many birthdays would you like to save? ";
    cin >> reps;

    outFile.open("bdays.txt");
    if (outFile.fail())
        {
        cout << "Error...\n";
        exit(EXIT_FAILURE);
        }

    for (index = 1; index <= reps; ++index)
        {
        cout << "Birthday #" << index << endl;
        cout << " Name: ";
        cin.getline(info.name, 50);
        cout << " Month: ";
        cin >> info.month;
        cout << " Day: ";
        cin >> info.day;
        cout << " Year: ";
        cin >> info.year;
        outFile << "Name: " << info.name << endl;
        outFile << "Birthday: " << info.month << '/' << info.day << '/'
                << info.year << endl;
        }

    outFile.close();

    return 0;
} 


Any help or pointers would be appreciated.
If you mean lines 34 - 37, then no. C++ will stop processing on a cin.xxx command until it recieves input from the user.
EDIT: Nvm, it's not related to end line. When you use cin the enter key is still in the input buffer and the first getline is taking it in. You need to flush the buffer before getting input.
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

struct BDay
{
    char    name[50];
    int     month;
    int     day;
    int     year;
};

int main (void)
{
    ofstream    outFile;
    int         reps;
    int         index;
    BDay        info;

    cout << "How many birthdays would you like to save? ";
    cin >> reps;

    outFile.open("bdays.txt");
    if (outFile.fail())
        {
        cout << "Error...\n";
        exit(EXIT_FAILURE);
        }

    for (index = 1; index <= reps; ++index)
        {
        cout << "Birthday #" << index << endl;
        cout << " Name: ";
	fflush(stdin);
        cin.getline(info.name, 50);
        cout << " Month: ";
        cin >> info.month;
        cout << " Day: ";
        cin >> info.day;
        cout << " Year: ";
        cin >> info.year;
        outFile << "Name: " << info.name << endl;
        outFile << "Birthday: " << info.month << '/' << info.day << '/'
                << info.year << endl;
        }

    outFile.close();

    return 0;
} 


Works for me in C++ 2010 Express
Last edited on
I know that the program stops to wait for input, but my problem is that it won't let me input a string. It skips the 'name' completely and asks for a 'month'.
Ok, then you need to sync\flush your input. hibbijo has the right idea.
The fflush() function isn't working for me, it does the same thing it was doing before.
Try "std::cin.sync()", this makes sure that any data sitting in the buffer is syncronized with the stream.
I can't seem to get it to work. Thanks for your help guys but I haven't even heard of fflush() or sync() till today. I'll just have to ask my teacher.
*SMACK* Get back here. Repost your code dude, nobody here has given up on you yet :)
Here's the code, the only change a cin statement before the getline

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

struct BDay
{
    char    name[50];
    int     month;
    int     day;
    int     year;
};

int main (void)
{
    auto    ofstream    outFile;
    auto    int         reps;
    auto    int         index;
    auto    BDay        info;

    cout << "How many birthdays would you like to save? ";
    cin >> reps;

    outFile.open("bdays.txt");
    if (outFile.fail())
        {
        cout << "Error...\n";
        exit(EXIT_FAILURE);
        }

    for (index = 1; index <= reps; ++index)
        {
        cout << "Birthday #" << index << endl;
        cout << " Name: ";
        cin >> info.name;
        cin.getline(info.name, 50);
        cout << " Month: ";
        cin >> info.month;
        cout << " Day: ";
        cin >> info.day;
        cout << " Year: ";
        cin >> info.year;
        outFile << "Name: " << info.name << endl;
        outFile << "Birthday: " << info.month << '/' << info.day << '/'
                << info.year << endl;
        }

    outFile.close();

    return 0;
} 


My teacher also mentioned inheritance but I lost my notes for it.
Inheritence isn't going to help your buffer issue, unless you're willing to rewrite "cstdio" :P !

I don't see where you tried to use "std::cin.sync()" or "fflush(stdin)". Is this your current iteration?
Anywhere I've tried to put cin.sync() or fflush(stdin) it wouldn't work. the program will compile but It won't run the way I want it to or it will output the wrong data.
Now that I've compiled your code and see what you're trying to do, we should address the first issue in that you don't have an vector to actually store all of the birthdays you're user asks for.
Well the only things I'm supposed to be working with in this is file I/O. We haven't learned vectors yet. I have a text file saved in the same directory so the birthdays will be saved to that.
You're entering an integer when you're asked for the month right? I compiled your app without paying much attention and it kept warping through because I was typing out the name of the month.

EDIT: I also added "cin.sync();" after every input just to be sure.
Last edited on
My problem to begin with was when trying to input the name, getline would not be recognized by the loop. (Refer back to my original post). I fixed that problem but now the problem is that in the output file, only the last names are copied. After I put a ' ' character in the array, it disregards anything written before it.
Last edited on
Topic archived. No new replies allowed.