how to make the program print out more than a word at once

can someone help with the "cin.getline()" problem am facing. thanks

#include <iostream>
#include <cstring>
#include <string>
int main()
{
using namespace std;


char lastname [20];
char firstname [20];
char major[20];
char major2[20];
int age;
float gpa;


cout<<" STUDENT #1 \n"<<endl;
cout<<"What is your major?"<<endl;
cin.getline(major, 20);
cout<<"What is your first name?"<<endl;
cin>> firstname;
cout<<"What is your last name?"<<endl;
cin>> lastname;
cout<<"How old are you?"<<endl;
cin>> age;
cout<<"What is your GPA?"<<endl;
cin>> gpa;
if(gpa <2 )
{ cout<<"INVALID!"<<endl;
cout<<" Enter GPA, where GPA cannot be less than 2 or greater than 4";
cin>> gpa;}

if(gpa >4 )
{ cout<<"INVALID!"<<endl;
cout<<" Enter GPA, where GPA cannot be less than 2 or greater than 4\n";
cin>> gpa;}
cout<<"\n";

cout<<"Hello "<<firstname<<" "<<lastname<<"\n"<<endl;
cout<<"You are "<< age<<" years of age"<<" and your major is "<<major<<"\n"<<endl;
cout<<"Your present GPA is:"<<gpa<<"\n"<<endl;
cout<<"\n"<<endl;



cout<<" STUDENT #2 \n"<<endl;
cout<<"What is your major?"<<endl;
cin.getline(major2, 20);
cout<<"What is your first name?"<<endl;
cin>> firstname;
cout<<"What is your last name?"<<endl;
cin>> lastname;
cout<<"How old are you?"<<endl;
cin>> age;
cout<<"What is your GPA?"<<endl;
cin>> gpa;
if(gpa <2 )
{ cout<<"INVALID!"<<endl;
cout<<" Enter GPA, where GPA cannot be less than 2 or greater than 4";
cin>> gpa;}

if(gpa >4 )
{ cout<<"INVALID!"<<endl;
cout<<" Enter GPA, where GPA cannot be less than 2 or greater than 4\n";
cin>> gpa;}
cout<<"\n"<<endl;

cout<<"Hello "<<firstname<<" "<<lastname<<"\n"<<endl;
cout<<"You are "<< age<<" years of age"<<" and your major is "<<major2<<"\n"<<endl;
cout<<"Your present GPA is:"<<gpa<<"\n"<<endl;
cout<<"\n";




system("pause");
return 0;
}
can someone help with the "cin.getline()" problem am facing. thanks


A more precise description of the problem would be useful in the future, especially since cin has nothing to do with output as described in the subject.

I would guess your problem results from the fact that you are mixing formatted and unformatted input. The formatted input (using the extraction operator (>>)) will leave the terminating whitespace in the input stream, which probably means when you use getline it extracts only a newline that was left in the stream by the previous formatted extraction operation.

The simplest solution is to toss in a cin >> ws; before a getline call. It consumes any leading whitespace.

Oh, and btw, prefer to use C++ strings to C strings.
Last edited on
Since you're using a combination of cin and cin.getlines, you're going to need to use cin.ignores to get the pointer off the null character.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
char major[20];
	char firstname[20];
	char lastname[20];

	cout << " STUDENT #1 \n" << endl;

	cout << "What is your major?" << endl;
	cin.getline(major, 20);

	cout << "What is your first name?" << endl;
	cin >> firstname;

	cin.ignore();

	cout << "What is your last name?" << endl;
	cin.getline(lastname, 20); //Use a cin.getline here, since some last names have a space, like de Carte 


Here's part of your code with cin.ignore().

A general rule of thumb, if you have a " cin >> variableName " statement, and the next cin statement uses cin.getline(), you will need cin.ignore() before using cin.getline().
the cin.ignore() worked!. thank you soo much.
You're welcome. See you on the forums.
Topic archived. No new replies allowed.