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?

#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	
		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';

11 Years
Discussion Span
Last Post by Salem

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.


thanks but where do i modify the getline member function?


>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.


>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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.