![]() |
| | #1 |
| Registered User Join Date: Sep 2004
Posts: 39
| getline problem 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 | |
| | #2 |
| Banned Join Date: Aug 2004
Posts: 128
| could try Code: cin.getline() Code: getline(stdin, myString); Code: scanf("%s",myString);
|
| JarJarBinks is offline | |
| | #3 | |
| Registered User Join Date: Jul 2003
Posts: 1,088
| Quote:
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 | |
| | #4 |
| Registered User 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 | |
| | #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 | |
| | #6 |
| Registered User 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |