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


Recommended Answers

All 7 Replies

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.

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

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.