| | |
Help. This is a strange problem
![]() |
•
•
Join Date: Apr 2005
Posts: 27
Reputation:
Solved Threads: 0
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.
As always, I appreciate your help
Bob
The first time through, everything works fine.
C++ Syntax (Toggle Plain Text)
#include <iomanip> #include <iostream> #include <fstream> using namespace std; struct student_rec { char name[30]; char ss[9]; int grade_3cr[4]; int grade_1cr[1]; }; void initializeStudent (student_rec &any_student) { any_student.name[30] = ' '; any_student.ss[9] = ' '; any_student.grade_3cr[0] = 0; any_student.grade_3cr[1] = 0; any_student.grade_3cr[2] = 0; any_student.grade_3cr[3] = 0; any_student.grade_1cr[0] = 0; } void processStudent (student_rec &your_student, const int& CREDIT_3, const int& CREDIT_1) { cout.setf(ios::fixed); int i = 0, tot_credits = 0, tot_grade = 0; float gpa; cout<<setw(15)<<your_student.name <<setw(15)<<your_student.ss <<" "; for (i=0;i<=3;i++) { cout<<setw(3)<<your_student.grade_3cr[i] <<" "; tot_grade = tot_grade + (your_student.grade_3cr[i] * CREDIT_3); tot_credits += CREDIT_3; } cout<<setw(8)<<your_student.grade_1cr[0] <<endl<<endl; tot_grade = tot_grade + (your_student.grade_1cr[0] * CREDIT_1); tot_credits += CREDIT_1; gpa = tot_grade / tot_credits; cout<<"Students total credits this senester is " <<tot_credits<<endl<<endl; cout<<"students overall gpa is " <<setprecision(2)<<gpa<<endl<<endl; } int main() { /*-------------------------------------- * Initialize variables *------------------------------------*/ int i, rec_cnt = 1; const int CREDIT_3 = 3; const int CREDIT_1 = 1; const int STUDENTS_IN_CLASS = 2; student_rec your_student; student_rec first_student; student_rec second_student; initializeStudent(first_student); initializeStudent(second_student); cout<<"\n\n ....... Student Records ....... \n\n"; while (rec_cnt <= STUDENTS_IN_CLASS) { initializeStudent(your_student); cout<<" Enter in Name of Student: "; cin.getline(your_student.name, sizeof(your_student.name)); cout<<"\n Enter in Student SS#: "; cin>>your_student.ss; cout<<"\n Enter in Students Four 3 Cedit Gades: "; for (i = 0;i<=3;i++) cin>>your_student.grade_3cr[i]; cout<<"\n Enter in Students Single 1 Credit Grades: "; i = 0; cin>>your_student.grade_1cr[i]; if (rec_cnt == 1) first_student = your_student; else second_student = your_student; rec_cnt++; } system ("CLS"); cout<<"Student Name Soc. Sec. No. 3 Credit Grades 1 Credit Grade\n" <<"--------------- -------------- ---------------- --------------\n" <<endl<<endl; initializeStudent(your_student); your_student = first_student; processStudent (your_student, CREDIT_3, CREDIT_1); initializeStudent(your_student); your_student = second_student; processStudent (your_student, CREDIT_3, CREDIT_1); system("PAUSE"); return 0; }
As always, I appreciate your help

Bob
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.
>As always, I appreciate your help
Apparently not enough to use code tags.
I'm here to prove you wrong.
>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.
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.
>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:
Add this, since you seem incapable of solving a problem after being told what it is:
And don't forget to include <limits>.
cin.getline, of course. You're missing the point and focusing on your strings while the problem is here:
C++ Syntax (Toggle Plain Text)
cin>>your_student.grade_1cr[i];
C++ Syntax (Toggle Plain Text)
cin>>your_student.grade_1cr[i]; cin.ignore(numeric_limits<streamsize>::max(), '\n');
I'm here to prove you wrong.
Another way to purge all those wobbly '\n' ...
Goofy? Yes, these pitfalls make C++ one of the most difficult languages to learn. Kind of job security in the end.
C++ Syntax (Toggle Plain Text)
// clear the stream, purge any \n while (cin.get() != '\n') ;
May 'the Google' be with you!
>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:
Or using the C-style alternative:
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)
while (std::cin.get(ch) && ch != '\n') ;
C++ Syntax (Toggle Plain Text)
while ((ch = std::cin.get()) != EOF && ch != '\n') ;
I'm here to prove you wrong.
•
•
•
•
Originally Posted by bobr_1013
C++ Syntax (Toggle Plain Text)
void initializeStudent (student_rec &any_student) { any_student.name[30] = ' '; any_student.ss[9] = ' ';
I'm assuming (I'm hoping) this was your intention:
C++ Syntax (Toggle Plain Text)
memset(any_student.name,0,sizeof(any_student.name)); ..
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"?
![]() |
Similar Threads
- whoa... very strange problem.. you will want to read this.. (Viruses, Spyware and other Nasties)
- strange problem - cookies on desktop (Windows NT / 2000 / XP)
- strange keyboard/mouse problem (USB Devices and other Peripherals)
- Rather strange problem copying, need assistance. (Perl)
- Very Strange Word 2003 Problem (Windows Software)
- Strange keyboard & highliting problem. (USB Devices and other Peripherals)
- A strange problem: unplugged network cable !! (Networking Hardware Configuration)
- strange problem (C)
- HELP!!! very strange problem! (Troubleshooting Dead Machines)
Other Threads in the C++ Forum
- Previous Thread: need to run these programs
- Next Thread: PLEASE help with project - compile error
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ char class classes classified code coding compatible compile console conversion count date delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file filewrite forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper homeworksolutions iamthwee icon if...else ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node object output play pointer problem program programming project python random read recursion reference rpg string strings struct symbol temperature template test text text-file toolkit tree url values variable vector video win32 windows winsock wordfrequency wxwidgets






