Thread: getline problem

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    39

    getline problem

    I believe this is a getline problem. I am trying to get a string in with a space. EX: getline(cin, fullname); I have about 4 of these getline functions. I use cin once to input a double and so I use cin.ignore() to clear the last getline. The problem occurs when I try to output this information. Some of the data is lost or outputed in the wrong spot for some reason. Have no clue why. Also the first getline function makes hit enter twice for it to take in the string i entered. This is for a friend or mine. thanks.

    Code:
    /*
    
    =========================================================================
    
    Class Program:
    
    This program asks you questions and outputs the answers
    
    Author:  CPTR 212 Fall 2004 Class
    
    =========================================================================
    
    */
    
     
    
    #include <iostream>
    
    #include <string>
    
    using namespace std;
    
     
    
    class Class
    
    {
    
                public:
    
                            void obtainName();
    
                            void obtainFavoriteClass();
    
                            void obtainNotFavoriteClass();
    
                            void obtainGPA();
    
                            void obtainFavoriteTeacher();
    
                            void obtainNotFavoriteTeacher();
    
                            void showClass();
    
                private:
    
                             string Name,
    
                                                    FavoriteClass,
    
                                                    NotFavoriteClass,
    
                                                    FavoriteTeacher,
    
                                                    NotFavoriteTeacher;
    
                             double GPA;
    
     
    
    };
    
     
    
    int main()
    
    {
    
                int loop = 1;
    
                while (loop == 1)
    
                {
    
                            Class college;
    
                            college.obtainName();
    
                            college.obtainFavoriteClass();
    
                            college.obtainNotFavoriteClass();
    
                            college.obtainGPA();
    
                            college.obtainFavoriteTeacher();
    
                            college.obtainNotFavoriteTeacher();
    
                            college.showClass();
    
                            cout << "\n Would you like to do it again?\n(1)Yes\n(2)No\n";
    
                            cin >> loop;
    
                } //end while
    
                return 0;
    
    } //end main
    
     
    
    void Class::obtainName()
    
    {
    
                cout << "What is your name? ";
    
                getline(cin, Name);
    
    }
    
     
    
    void Class::obtainFavoriteClass()
    
    {
    
                cout << "What is your favorite class? ";
    
                getline(cin, FavoriteClass);
    
    }
    
     
    
    void Class::obtainNotFavoriteClass()
    
    {
    
                cout << "What is your least favorite class? ";
    
                getline(cin, NotFavoriteClass);
    
    }
    
     
    
    void Class::obtainGPA()
    
    {
    
                cout << "What is your grade point average? ";
    
                cin.ignore(1024, '\n');
    
                cin >> GPA;
    
    }
    
     
    
    void Class::obtainFavoriteTeacher()
    
    {
    
                cout << "Who is your favorite teacher? ";
    
                getline(cin, FavoriteTeacher);
    
    }
    
     
    
    void Class::obtainNotFavoriteTeacher()
    
    {
    
                cout << "Who is your least favorite teacher? ";
    
                getline(cin, NotFavoriteTeacher);
    
                cin.ignore(1024, '\n');
    
    }
    
     
    
    void Class::showClass()
    
    {
    
                cout << "\nName: "
    
                             << Name
    
                             << endl
    
                             << "Favorite Class: "
    
                             << FavoriteClass
    
                             << endl
    
                             << "Least Favorite Class: "
    
                             << NotFavoriteClass
    
                             << endl
    
                             << "Grade Point Average: "
    
                             << GPA
    
                             << endl
    
                             << "Favorite Teacher: "
    
                             << FavoriteTeacher
    
                             << endl
    
                             << "Least Favorite Teacher: "
    
                             << NotFavoriteTeacher
    
                             << endl;
    
    }

  2. #2
    could try

    Code:
    cin.getline()
    or

    Code:
    getline(stdin, myString);
    or

    Code:
    scanf("%s",myString);
    just a suggestion i didnt compile it and try it.

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Quote Originally Posted by Bitphire
    Also the first getline function makes hit enter twice for it to take in the string i entered.
    If you are using MSVC++ 6.0, there is a bug in that getline. Go to this site:

    http://www.dinkumware.com/vc_fixes.html

    scroll down to the part that says "Fix to <string>", and follow the instructions there.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    The problem you are having is that your ignores are in the wrong place. Inside of obtainGPA(), you should run the ignore after you get the double, because you want to ignore the newline (and any garbage text) from after the user input the number. Also, in obtainNotFavoriteTeacher(), I'm not sure why you have an extra ignore in there, you should probably remove it completely.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    39
    If i move the ignore after i take in the GPA i get an infinite loop. That is why it is where it is to kill the garbage left over from the previous getline. Also the very last ignore you mention, if i remove it i get an infinite loop also, not sure why but I do. Thanks for the help.

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Strange, I copied and pasted your code to my machine, made those two changes, and it worked perfectly.

    Besides, it doesn't make sense logically to put the ignore before you get the double. The reason you need it is because the operator>> leaves the newline in the stream (I assume you are inputting a number like 4.0 for GPA), so to ignore that newline you must put it after you read the number.

    I have a feeling those ignores are what are giving you your problems, so maybe you should code it the logically correct way and then figure out why that isn't working. Maybe post what compiler/platform you are using so others can try to reproduce the problem. Also, if you are using VC++ 6 then make sure you check that link out.

    By the way, here is what the functions I changed looked like after I made the fixes:
    Code:
    void Class::obtainGPA()
    
    {
    
                cout << "What is your grade point average? ";
    
                cin >> GPA;
    
                cin.ignore(1024, '\n');
    }
    
    
    void Class::obtainNotFavoriteTeacher()
    
    {
    
                cout << "Who is your least favorite teacher? ";
    
                getline(cin, NotFavoriteTeacher);
    
       //         cin.ignore(1024, '\n');
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 01:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 11:59 AM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM