943,965 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3058
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 6th, 2005
0

Help. This is a strange problem

Expand Post »
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.
C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
May 6th, 2005
0

Re: Help. This is a strange problem

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

Re: Help. This is a strange problem

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,
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
May 6th, 2005
0

Re: Help. This is a strange problem

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

Re: Help. This is a strange problem

Narue,

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

Thanks,


Bob
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 2005
May 7th, 2005
0

Re: Help. This is a strange problem

>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:
C++ Syntax (Toggle Plain Text)
  1. cin>>your_student.grade_1cr[i];
Add this, since you seem incapable of solving a problem after being told what it is:
C++ Syntax (Toggle Plain Text)
  1. cin>>your_student.grade_1cr[i];
  2. cin.ignore(numeric_limits<streamsize>::max(), '\n');
And don't forget to include <limits>.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 7th, 2005
0

Re: Help. This is a strange problem

Another way to purge all those wobbly '\n' ...
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
May 7th, 2005
0

Re: Help. This is a strange problem

>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:
C++ Syntax (Toggle Plain Text)
  1. while (std::cin.get(ch) && ch != '\n')
  2. ;
Or using the C-style alternative:
C++ Syntax (Toggle Plain Text)
  1. while ((ch = std::cin.get()) != EOF && ch != '\n')
  2. ;
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 7th, 2005
0

Re: Help. This is a strange problem

Quote originally posted by bobr_1013 ...
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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"?
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
May 7th, 2005
0

Re: Help. This is a strange problem

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,
Reputation Points: 10
Solved Threads: 0
Unverified User
bobr_1013 is offline Offline
27 posts
since Apr 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: need to run these programs
Next Thread in C++ Forum Timeline: PLEASE help with project - compile error





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


Follow us on Twitter


© 2011 DaniWeb® LLC