Help. This is a strange problem

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

Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Help. This is a strange problem

 
0
  #1
May 6th, 2005
When this program runs, I can enter in the name the first time through the while loop (using "cin.getline). When I go through the second time, it skips the name and asks to enter the ss#.

The first time through, everything works fine.
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. struct student_rec
  7. {
  8. char name[30];
  9. char ss[9];
  10. int grade_3cr[4];
  11. int grade_1cr[1];
  12. };
  13.  
  14. void initializeStudent (student_rec &any_student)
  15. {
  16. any_student.name[30] = ' ';
  17. any_student.ss[9] = ' ';
  18. any_student.grade_3cr[0] = 0;
  19. any_student.grade_3cr[1] = 0;
  20. any_student.grade_3cr[2] = 0;
  21. any_student.grade_3cr[3] = 0;
  22. any_student.grade_1cr[0] = 0;
  23. }
  24.  
  25. void processStudent (student_rec &your_student, const int& CREDIT_3, const int& CREDIT_1)
  26. {
  27. cout.setf(ios::fixed);
  28. int i = 0,
  29. tot_credits = 0,
  30. tot_grade = 0;
  31. float gpa;
  32.  
  33. cout<<setw(15)<<your_student.name
  34. <<setw(15)<<your_student.ss
  35. <<" ";
  36. for (i=0;i<=3;i++)
  37. {
  38. cout<<setw(3)<<your_student.grade_3cr[i]
  39. <<" ";
  40. tot_grade = tot_grade + (your_student.grade_3cr[i] * CREDIT_3);
  41. tot_credits += CREDIT_3;
  42. }
  43.  
  44. cout<<setw(8)<<your_student.grade_1cr[0]
  45. <<endl<<endl;
  46. tot_grade = tot_grade + (your_student.grade_1cr[0] * CREDIT_1);
  47. tot_credits += CREDIT_1;
  48. gpa = tot_grade / tot_credits;
  49.  
  50. cout<<"Students total credits this senester is "
  51. <<tot_credits<<endl<<endl;
  52. cout<<"students overall gpa is "
  53. <<setprecision(2)<<gpa<<endl<<endl;
  54. }
  55.  
  56. int main()
  57. {
  58. /*--------------------------------------
  59.   * Initialize variables
  60.   *------------------------------------*/
  61. int i,
  62. rec_cnt = 1;
  63.  
  64. const int CREDIT_3 = 3;
  65. const int CREDIT_1 = 1;
  66. const int STUDENTS_IN_CLASS = 2;
  67.  
  68. student_rec your_student;
  69. student_rec first_student;
  70. student_rec second_student;
  71. initializeStudent(first_student);
  72. initializeStudent(second_student);
  73.  
  74. cout<<"\n\n ....... Student Records ....... \n\n";
  75. while (rec_cnt <= STUDENTS_IN_CLASS)
  76. {
  77. initializeStudent(your_student);
  78. cout<<" Enter in Name of Student: ";
  79. cin.getline(your_student.name, sizeof(your_student.name));
  80. cout<<"\n Enter in Student SS#: ";
  81. cin>>your_student.ss;
  82. cout<<"\n Enter in Students Four 3 Cedit Gades: ";
  83. for (i = 0;i<=3;i++)
  84. cin>>your_student.grade_3cr[i];
  85. cout<<"\n Enter in Students Single 1 Credit Grades: ";
  86. i = 0;
  87. cin>>your_student.grade_1cr[i];
  88.  
  89. if (rec_cnt == 1)
  90. first_student = your_student;
  91. else
  92. second_student = your_student;
  93. rec_cnt++;
  94. }
  95. system ("CLS");
  96. cout<<"Student Name Soc. Sec. No. 3 Credit Grades 1 Credit Grade\n"
  97. <<"--------------- -------------- ---------------- --------------\n"
  98. <<endl<<endl;
  99.  
  100. initializeStudent(your_student);
  101. your_student = first_student;
  102. processStudent (your_student, CREDIT_3, CREDIT_1);
  103.  
  104. initializeStudent(your_student);
  105. your_student = second_student;
  106. processStudent (your_student, CREDIT_3, CREDIT_1);
  107.  
  108. system("PAUSE");
  109. return 0;
  110. }

As always, I appreciate your help

Bob
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
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: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help. This is a strange problem

 
0
  #2
May 6th, 2005
It's hardly a strange problem since everybody sees it when learning C or C++. getline reads characters until a newline character is encountered, but cin>> leaves newlines on the stream. So a cin>> followed by a getline will typically cause problems.

>As always, I appreciate your help
Apparently not enough to use code tags.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Help. This is a strange problem

 
0
  #3
May 6th, 2005
Narue,

the cin.getline is mentioned in two C++ books I have as a way to enter in a string (where you can't with just "cin".

Also, what do you mean by "code tags"?

Thanks,
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
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: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help. This is a strange problem

 
0
  #4
May 6th, 2005
>the cin.getline is mentioned in two C++ books I have as a way to enter in a string (where you can't with just "cin".
You can enter a string with cin's operator>>, just not one with whitespace by default. And that has no bearing on the problem at hand. You use cin.getline to read a string, then you use cin>> to read a number. That leaves a newline in the stream and the next call to cin.getline appears to be skipped because it immediately sees the termination character.

>Also, what do you mean by "code tags"?
Code tags. Use them when posting code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Help. This is a strange problem

 
0
  #5
May 7th, 2005
Narue,

What would you use to enter text into an array (such as a name)?

Thanks,


Bob
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
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: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help. This is a strange problem

 
0
  #6
May 7th, 2005
>What would you use to enter text into an array (such as a name)?
cin.getline, of course. You're missing the point and focusing on your strings while the problem is here:
  1. cin>>your_student.grade_1cr[i];
Add this, since you seem incapable of solving a problem after being told what it is:
  1. cin>>your_student.grade_1cr[i];
  2. cin.ignore(numeric_limits<streamsize>::max(), '\n');
And don't forget to include <limits>.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,954
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 917
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Help. This is a strange problem

 
0
  #7
May 7th, 2005
Another way to purge all those wobbly '\n' ...
  1. // clear the stream, purge any \n
  2. while (cin.get() != '\n')
  3. ;
Goofy? Yes, these pitfalls make C++ one of the most difficult languages to learn. Kind of job security in the end.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
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: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help. This is a strange problem

 
0
  #8
May 7th, 2005
>Another way to purge all those wobbly '\n' ...
That example is incompete, which is why cin.ignore is a superior alternative. It forces you to provide all of the requisite information whereas with the loop approach it's easy to miss a very important test for end-of-file. The correct loop would be:
  1. while (std::cin.get(ch) && ch != '\n')
  2. ;
Or using the C-style alternative:
  1. while ((ch = std::cin.get()) != EOF && ch != '\n')
  2. ;
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Help. This is a strange problem

 
0
  #9
May 7th, 2005
Originally Posted by bobr_1013
  1. void initializeStudent (student_rec &any_student)
  2. {
  3. any_student.name[30] = ' ';
  4. any_student.ss[9] = ' ';
You have to be kidding me.

I'm assuming (I'm hoping) this was your intention:
  1. memset(any_student.name,0,sizeof(any_student.name));
  2. ..

I'm assuming this barbaric system("PAUSE") is a Windows thing, but because we all know that good C++ code is interoperable C++ code, make sure you run ispell when you compile on a *nix system (and naturally, remove system("PAUSE"), or wrap it in __WINDOWS__).

What the heck is a "senester"?
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 27
Reputation: bobr_1013 is an unknown quantity at this point 
Solved Threads: 0
bobr_1013 bobr_1013 is offline Offline
Light Poster

Re: Help. This is a strange problem

 
0
  #10
May 7th, 2005
The system("PAUSE") is a method of using the PC system commands.

This is what we do with each class assignment to see the displayu from the cout's

Thanks,
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