Problem with c++ getline

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babe20042004
    New Member
    • Oct 2008
    • 8

    Problem with c++ getline

    I'm using the getline function to read the Degree Program of the user but for some reason the program skips the degree program and goes on to the next line which asks for the courses that the useris doing this semester.
    This is the code:

    #include <iostream>
    #include <string>

    using namespace std;

    int main()
    {

    char name[256];
    char id[256];
    string degree;
    char courses[256];

    cout <<"Please insert your name " << endl;
    cin.get(name,25 6);
    cout << "Please insert your ID# " << endl;
    cin>>id;
    cout<<endl;
    cout << "Please insert your Degree Program: " << endl;
    cin.getline(deg ree,(256));
    cout<<endl;
    cout << "Please insert your Courses: " <<endl;
    cin.getline(cou rses,256);

    return 0;
    }
  • joem86
    New Member
    • Oct 2008
    • 2

    #2
    I've always had troubles with cin.getline. Try the other getline (defined by <string>)

    Code:
    getline(cin, degree);
    I hope this helps!

    Joe

    Comment

    • boxfish
      Recognized Expert Contributor
      • Mar 2008
      • 469

      #3
      That's probably because when you cin to id, the newline character is left on the stream, and degree gets the empty line that id was on. Maybe you could do an extra getline or use ignore with the '\n' character.
      Hope this helps.

      Please use code tags around your code. Put [CODE] before it and [/CODE] after it so it shows up in a code box and the indentation isn't wrecked. Thanks.
      Last edited by boxfish; Oct 22 '08, 12:45 AM. Reason: Code tags.

      Comment

      • babe20042004
        New Member
        • Oct 2008
        • 8

        #4
        Thank you so much but i found the source of the problem . I had to include cin.get(); for some strange new reason. here's the new code.

        #include <iostream>
        #include <string>
        #include <fstream>
        using namespace std;

        int main()
        {

        char name[256];
        char id[256];
        string degree;
        string courses;

        cout <<"Please insert your name " << endl;
        cin.get(name,25 6);
        cout << "Please insert your ID# " << endl;
        cin>>id;
        cout<<endl;
        cin.get();
        cout << "Please insert your Degree Program " << endl;
        getline(cin,deg ree);
        cout<<endl;
        cout << "Please insert your Courses: " <<endl;
        getline(cin,cou rses);


        return 0;
        }

        Comment

        • babe20042004
          New Member
          • Oct 2008
          • 8

          #5
          Thanks. Yep. That was the problem.

          Originally posted by boxfish
          That's probably because when you cin to id, the newline character is left on the stream, and degree gets the empty line that id was on. Maybe you could do an extra getline or use ignore with the '\n' character.
          Hope this helps.

          Please use code tags around your code. Put [CODE] before it and [/CODE] after it so it shows up in a code box and the indentation isn't wrecked. Thanks.

          Comment

          Working...