944,184 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 8325
  • C++ RSS
Jul 27th, 2005
0

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

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aismm is offline Offline
10 posts
since Jul 2005
Jul 27th, 2005
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 27th, 2005
0

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

thanks but where do i modify the getline member function?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aismm is offline Offline
10 posts
since Jul 2005
Jul 27th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 29th, 2005
0

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

use this between cin and getline
cin.ignore( 2, '\n');
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Aztech is offline Offline
3 posts
since May 2005
Jul 29th, 2005
0

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

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Oct 12th, 2007
0

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

just put a ' cin.get (); ' at the end of the loop
Reputation Points: 10
Solved Threads: 0
Newbie Poster
patrickyeow is offline Offline
2 posts
since Oct 2007
Oct 12th, 2007
0

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

> just put a ' cin.get (); ' at the end of the loop
This was not worth waiting 2 YEARS for.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: help with polynomials
Next Thread in C++ Forum Timeline: recursion - how to calculate sum?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC