| | |
cin.getline not working in for loop or...?
![]() |
•
•
Join Date: Jul 2005
Posts: 10
Reputation:
Solved Threads: 0
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.
Thanks.
C++ Syntax (Toggle Plain Text)
#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'; }
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.
I'm here to prove you wrong.
>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.
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.
I'm here to prove you wrong.
![]() |
Similar Threads
- cin.getline (C++)
- need help problem with cin.getline method (C++)
Other Threads in the C++ Forum
- Previous Thread: help with polynomials
- Next Thread: recursion - how to calculate sum?
| Thread Tools | Search this Thread |
ace_thread api array assignment based binary bitmap borland c++ c/c++ char class classes code coding compile connect console conversion count create() delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multidimensional multiple news node output pointer problem program programming project python random read reading recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






