For my c++ Programming class, I have to write a program that will read a student's name and then read 10 grades from them and then display the student name, along with the average for those 10 grades and the letter grade. It will keep reading students until the user types "done".
I haven't gotten to the average part yet (although it's going to be easy), but I'm having a problem. I have to use cin.getline for the student name because it has to be first and last name (he is making us use c-style strings instead of just using <string>), but when I loop it (It works fine the first time), it doesn't read it. The first time, it says "Student name: " and I type it and it works fine, but the second time, it says "Student name: " but then jumps to the next part that asks for a grade. I have no idea why the cin.getline doesn't work the second time around. Here is my code, I KNOW it's terrible, I'm going to clean it up and organize it once I figure out this problem, I just need to know why this won't work first. Thanks in advance:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main()
{
int null = 0;
char doneString[5] = "done";
do
{
char testString[30];
cout<<"Student Name?: ";
cin.getline(testString,25);
if(strcmp(testString , doneString) != 0)
{
int gradeSum = 0;
int currentGrade = 0;
for(int gradeNum = 1; gradeNum <= 10; gradeNum++)
{
cout<<"Please enter the grade for test #"<<gradeNum<<": ";
cin>>currentGrade;
gradeSum += currentGrade;
}
cout<<endl<<"The Student is:"<<testString<<endl;
cout<<"The total grade is:"<<gradeSum<<endl;
}
else
{
cout<<"User typed Done, program is terminating!";
null = 1;
exit;
}
} while(null<1);
}
Just a note: If I use cin>> to get the input for the student name instead of cin.getline - it runs perfectly and does everything that I want it to do (It keeps reading students and their grades until the user types done). Unfortunately I HAVE to use cin.getline because he told us it was a requirement because we have to read the first and last name with the space in the middle. I'm not sure why there is only an issue when I use cin.getline