Question: (C++) Getline is skipping, how can I fix this? Everything is my code is running well, except for the getline mentioned above. I've tried to
(C++) Getline is skipping, how can I fix this?
Everything is my code is running well, except for the getline mentioned above. I've tried to fix it a few different ways, but some problem always arises. I know its a simple fix but I just can't think of a solution at the moment. The problem is in bold below.
Code for reference:
#include
#include
#include
using namespace std;
//Constant for number of students
const int numStudents = 3;
//Function Identifiers
char getGrade(float value);
void displayData(struct StudentRecord *student, int hold);
//
struct StudentRecord {
string name;
float lecture_points;
float lab_points;
float total_points;
float average_grade;
char letter_grade;
};
// main function
int main()
{
int pointsPossible;
cout << "Enter the number of Students: ? " << numStudents << endl;
cout << "Enter the total possible points for the course: ";
cin >> pointsPossible;
cout << "" << endl;
struct StudentRecord student[numStudents];
float highest_average = 0;
float lowest_average = 100.0;
for (int i = 0; i < numStudents; i++) {
struct StudentRecord temp;
cout << "Enter the information for Student " << (i + 1) << ": ";
cout << " Name: ? ";
getline(cin,temp.name);
cout << "" << endl;
cout << " Lecture Points Earned: ";
cin >> temp.lecture_points;
cout << " Lab Points Earned: ";
cin >> temp.lab_points;
cout << "" << endl;
//Setprecision
cout << fixed << showpoint << setprecision(1);
temp.total_points = temp.lecture_points + temp.lab_points;
temp.average_grade = 100 * (temp.total_points / pointsPossible);
temp.letter_grade = getGrade(temp.average_grade);
student[i] = temp;
if (highest_average < temp.average_grade)
highest_average = temp.average_grade;
if (lowest_average > temp.average_grade)
lowest_average = temp.average_grade;
}
displayData(student, numStudents);
cout << "" << endl;
cout << "Highest Average: " << highest_average << endl;
cout << "Lowest Average: " << lowest_average << endl;
system("pause");
return 0;
}
// function to get character grade
char getGrade(float value) {
if (value >= 90)
return 'A';
else if (value >= 80)
return 'B';
else if (value >= 70)
return 'C';
else if (value >= 60)
return 'D';
else if (value >= 50)
return 'E';
else
return 'F';
}
// function to print student record
void displayData(struct StudentRecord *student, int hold)
{
cout << "Student Name\tLecture Pts\tLab Pts\Total Pts\tAverage\tLetter Grade" << endl;
for (int i = 0; i < hold; i++) {
cout << student[i].name << "\t" << student[i].lecture_points << "\t" << student[i].lab_points << "\t" << student[i].total_points << "\t"
<< student[i].average_grade << "\t" << student[i].letter_grade << endl;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
