I'm starting a small project with input/output of name, mid-term and final grade in C++, however, I met some input/output erros. Currently, my code is:
#include <iostream>
#include <string>
using namespace std;
int main() {
// declear the class size and studnet name and grade
// student number as variable, student name & grade as array
int studentNo = 2;
string studentName[studentNo];
double studentGrade[studentNo][2];
// this loop reads name and grade into the program
for (int i = 0; i < studentNo; ++i) {
cout << "Please enter student name: ";
getline(cin, studentName[i]);
cout << studentName[i] << endl;
// for each row of 2D array, [i][0] stores the mid-term grade
cout << "Please enter mid-term grade: ";
cin >> studentGrade[i][0];
// while (studentGrade[i][0] < 0 || studentGrade[i][0] > 100) {
// cout << "Wrong grade input, please enter again!" << endl;
// }
cout << studentGrade[i][0] << endl;
// for each row of 2D array, [i][1] stores the final exam grade
cout << "Please enter final exam grade: ";
cin >> studentGrade[i][1];
cout << studentGrade[i][1] << endl;
}
}
If my input is:
Ryan Smith
60.0
61.0
Steve Cole
50.0
40.0
My output will be:
Please enter student name: Ryan Smith
Please enter mid-term grade: 60
Please enter final exam grade: 61
Please enter student name:
Please enter mid-term grade: 0
Please enter final exam grade: 6.90234e-310
But what I want is:
Please enter student name: Ryan Smith
Please enter mid-term grade: 60
Please enter final exam grade: 61
Please enter student name: Steve Cole
Please enter mid-term grade: 50
Please enter final exam grade: 40
It seems like my declaration and loop are correct, hence I don't know which part of my code make the wrong output. Please help me out, thanks.
Edited: when I initiate this problem, I tried to comment other I/O code and getline() works fine. Thus, I do not know it was issue related getline() function. Thanks to @NathanOliver, now I can have a better understanding of this problem.