C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-18-2004, 02:42 PM   #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;

}
Bitphire is offline   Reply With Quote
Old 10-18-2004, 02:47 PM   #2
Banned
 
Join Date: Aug 2004
Posts: 128
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.
JarJarBinks is offline   Reply With Quote
Old 10-18-2004, 03:02 PM   #3
Registered User
 
jlou's Avatar
 
Join Date: Jul 2003
Posts: 1,088
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.
jlou is offline   Reply With Quote
Old 10-18-2004, 03:10 PM   #4
Registered User
 
jlou's Avatar
 
Join Date: Jul 2003
Posts: 1,088
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.
jlou is offline   Reply With Quote
Old 10-18-2004, 03:20 PM   #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.
Bitphire is offline   Reply With Quote
Old 10-18-2004, 04:42 PM   #6
Registered User
 
jlou's Avatar
 
Join Date: Jul 2003
Posts: 1,088
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');

}
jlou is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 06:47 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22