Can anybody help explain why output isn't working correctly?

Mar 26, 2012 at 10:11pm
I'm working on a simple practice program and my program is not outputting the "cMajor" text in the paragraph at the end. I have looked my code over multiple times and can't seem to figure it out. Any help would be greatly appreciated.

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


int main()
{
//Variable definition
int age, gradYear;
double gpa;
string name, birthCity, cMajor, cMinor, finState;

//Data input
cout << "Let's gather some data about you: " << endl;
cout << "Enter your name: ";
getline (cin,name);
cout << "Enter your city of birth: ";
getline (cin,birthCity);
cout << "Enter your age: ";
cin >> age;
cout << "Enter your College major: ";
getline (cin,cMajor);
cin.ignore (255, '\n');
cout << "Enter your College minor: ";
getline (cin,cMinor);
cout << "Enter your GPA: ";
cin >> gpa;
cout << "Enter your Graduation year: ";
cin >> gradYear;
cin.ignore (255, '\n');
cout << "Enter the state you wish to live in after Graduation: ";
getline (cin,finState);
cout << endl;

//Narrative based off data
cout << "Here's a little bit about yourself: " << endl;
cout << "Once upon a time " << name << " was born in " << birthCity << " " << age << " years ago. Presently, they are in college trying to major in " << cMajor << " with a minor in " << cMinor << ". Hopefully they will maintain their " << gpa << " GPA until they graduate in " << gradYear << " and then move to " << finState << " and live happily ever after. The end." << endl;

return 0;
}
Last edited on Mar 27, 2012 at 7:58pm
Mar 26, 2012 at 10:25pm
You need to ignore() before you try to read in via getline(). The point of the ignore is to remove the newline left in the buffer by cin>>.
Mar 26, 2012 at 10:27pm
I'm still confused by what you're saying. I need to ignore() before every getline()?

I don't understand why the "cMajor" is the only part now showing in my output.
Topic archived. No new replies allowed.