cin.getline not working in for loop or...?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2005
Posts: 10
Reputation: aismm is an unknown quantity at this point 
Solved Threads: 0
aismm aismm is offline Offline
Newbie Poster

cin.getline not working in for loop or...?

 
0
  #1
Jul 27th, 2005
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.


  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. char getLetterGrade(float grade);
  6.  
  7. struct course
  8. {
  9. char name[30];
  10. char *letterG;
  11. int *IDnum;
  12. int numtests;
  13. int numstudents;
  14. float *total;
  15. float *tests;
  16. float *average;
  17. float grade;
  18. };
  19.  
  20. int main()
  21. {
  22.  
  23. course student;
  24. cout<< "Number of Test Scores for each student: ";
  25. cin >> student.numtests;
  26. cout<< "Number of Students: ";
  27. cin>> student.numstudents;
  28. student.tests= new float[student.numtests]; // allocate memory
  29. student.IDnum= new int[student.numtests];
  30. student.total= new float[student.numtests];
  31. student.average= new float[student.numtests];
  32. student.letterG= new char[student.numtests];
  33.  
  34.  
  35.  
  36. //get the ID number & test scores
  37. for (int count=0; count< student.numtests; count++)
  38. {
  39. cout<< "Enter the student's ID number. \n";
  40. cout<< "Student #" << (count+1)<< ": ";
  41. cin>> student.IDnum[count];
  42. cout<< "Enter the student's name. \n";
  43. cout<< "Student #" << (count +1)<< ": ";
  44. //this is the part where user should enter the student name
  45. cin.getline(student.name[count],30);
  46. cout<< "Enter the test scores below.\n";
  47. cout<< "Test #" << (count +1) << ": ";
  48. cin>> student.tests[count];
  49. student.total[count] += student.tests[count];
  50. student.average[count] = student.total[count]/ student.numtests;
  51.  
  52. cout<< "Name: \t\t ID Number: \t Average Test Score: Grade: \n";
  53. cout<< "---------------------------------------------------------";
  54. cout<< student.name[count] << " ";
  55. cout<< student.IDnum[count] << " ";
  56. cout<< student.average[count] << " ";
  57. student.letterG[count] = getLetterGrade(student.average[count]);
  58. cout<< student.letterG[count] << endl;
  59. }
  60.  
  61.  
  62. //Free dynamically allocated memory
  63. delete [] student.tests;
  64. return 0;
  65.  
  66. }
  67.  
  68. char getLetterGrade(float grade)
  69. {
  70.  
  71. if ( grade >= (float) 90 ) return 'A';
  72. else if (grade >= (float) 80) return 'B';
  73. else if (grade >= (float) 70) return 'C';
  74. else if (grade >= (float) 60) return 'D';
  75. else return 'F';
  76.  
  77. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: cin.getline not working in for loop or...?

 
0
  #2
Jul 27th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 10
Reputation: aismm is an unknown quantity at this point 
Solved Threads: 0
aismm aismm is offline Offline
Newbie Poster

Re: cin.getline not working in for loop or...?

 
0
  #3
Jul 27th, 2005
thanks but where do i modify the getline member function?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: cin.getline not working in for loop or...?

 
0
  #4
Jul 27th, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 3
Reputation: Aztech is an unknown quantity at this point 
Solved Threads: 0
Aztech Aztech is offline Offline
Newbie Poster

Re: cin.getline not working in for loop or...?

 
0
  #5
Jul 29th, 2005
use this between cin and getline
cin.ignore( 2, '\n');
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: cin.getline not working in for loop or...?

 
0
  #6
Jul 29th, 2005
>cin.ignore( 2, '\n');
Feel free to replace 2 with any arbitrary number. Someone invariably pipes in with something like this, but fails to do it properly. Once again, search the forum for a better solution.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 2
Reputation: patrickyeow is an unknown quantity at this point 
Solved Threads: 0
patrickyeow patrickyeow is offline Offline
Newbie Poster

Re: cin.getline not working in for loop or...?

 
0
  #7
Oct 12th, 2007
just put a ' cin.get (); ' at the end of the loop
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: cin.getline not working in for loop or...?

 
0
  #8
Oct 12th, 2007
> just put a ' cin.get (); ' at the end of the loop
This was not worth waiting 2 YEARS for.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC