954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

cin.getline not working in for loop or...?

i need to write a program that includes student names, test scores, and grades... but the cin.getline doesn't work, it simply skips to the next line... where did i do wrong?
Thanks.

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

char getLetterGrade(float grade);

struct course
{
	char name[30];
	char *letterG;
	int *IDnum;
	int numtests;
	int numstudents;
	float *total;
	float *tests;
	float *average;
	float grade;
};

int main()
{

	course student;
	cout<< "Number of Test Scores for each student: ";
	cin >> student.numtests;
	cout<< "Number of Students: ";
	cin>> student.numstudents;
	student.tests= new float[student.numtests]; // allocate memory
	student.IDnum= new int[student.numtests];
	student.total= new float[student.numtests];
	student.average= new float[student.numtests];
	student.letterG= new char[student.numtests];



	//get the ID number & test scores
	for (int count=0; count< student.numtests; count++)
	{
		cout<< "Enter the student's ID number. \n";
		cout<< "Student #" << (count+1)<< ": ";
		cin>> student.IDnum[count];
		cout<< "Enter the student's name. \n";
		cout<< "Student #" << (count +1)<< ": ";
//this is the part where user should enter the student name	
                         cin.getline(student.name[count],30);
		cout<< "Enter the test scores below.\n";
		cout<< "Test #" << (count +1) << ": ";
		cin>> student.tests[count];
		student.total[count] += student.tests[count];
		student.average[count] = student.total[count]/ student.numtests;	

		cout<< "Name: \t\t ID Number: \t Average Test Score:    Grade: \n";
		cout<< "---------------------------------------------------------";
		cout<< student.name[count] << "   ";
		cout<< student.IDnum[count] << "   ";
		cout<< student.average[count] << "   ";
		student.letterG[count] = getLetterGrade(student.average[count]);
		cout<< student.letterG[count] << endl;
	}


	//Free dynamically allocated memory
	delete [] student.tests;
	return 0;

}

char getLetterGrade(float grade) 
{

	if ( grade >= (float) 90 ) return 'A';
	else if (grade >= (float) 80) return 'B';
	else if (grade >= (float) 70) return 'C';
	else if (grade >= (float) 60) return 'D';
	else return 'F';

}
aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

cin's >> operator and getline don't play well together. Formatted input treats whitespace differently than nonformatted input. More precisely, cin's >> operator leaves a newline character in the stream for getline to terminate immediately on, so it appears as if the call is being skipped. You can fix it by searching the forum for the other bazillion times this question is asked every week.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanks but where do i modify the getline member function?

aismm
Newbie Poster
10 posts since Jul 2005
Reputation Points: 10
Solved Threads: 0
 

>thanks but where do i modify the getline member function?
The point seems to be just beyond your grasp. Search the forum, and you will find the solution because it's a common question, and the answer is always the same. You don't need to modify getline at all. You need to clear the stream of crap before the call to getline.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

use this between cin and getline
cin.ignore( 2, '\n');

Aztech
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

>cin.ignore( 2, '\n');
Feel free to replace 2 with any arbitrary number. Someone invariably pipes in with something like this, but fails to do it properly. Once again, search the forum for a better solution.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

just put a ' cin.get (); ' at the end of the loop

patrickyeow
Newbie Poster
2 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

> just put a ' cin.get (); ' at the end of the loop
This was not worth waiting 2 YEARS for.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You