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