Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-13-2004, 11:46 PM   #1
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
Creating a file with an information table

Howdy,

I have the following problem:

I need to create an information table with the following information:

student name (one line)
student number (one line)
marks (mid-term 1, mid-term 2, final-exam) (one line)

Create a student data file should contain at least records of 10 students.

So far I can only insert one pice of info to the file. Whenever I try to add code for another one, the program enters an infinite loop.


_________________________________________________
Here's the working version of the program:

#include (iostream)
#include (iomanip)
#include (string)
#include (fstream)

using namespace std;

main()
{
ofstream myout;
int student_number;
float mid_1, mid_2, final;
string student_name;

myout.open("students.dat");
if (myout)
{
cout << "Enter name of the student (type none to stop): ";
getline(cin, student_name);
while (student_name != "none")
{
myout << student_name << endl;
cout << "Enter name of the student (type none to stop): ";
getline(cin, student_name);
}
myout.close();
}
else
cout << "Could not create file." << endl;
}
__________________
Darum still, füg' ich mich, wie Gott es will. Nun, so will ich wacker streiten, und sollt' ich den Tod erleiden, stirbt ein braver Reitersmann.

Last edited by Nuclear Krusader; 03-13-2004 at 11:51 PM.
Nuclear Krusader is offline   Reply With Quote
Old 03-13-2004, 11:48 PM   #2
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
And here's the infinite looping-version:

#include (iostream)
#include (iomanip)
#include (string)
#include (fstream)

using namespace std;

main()
{
ofstream myout;
int student_number;
float mid_1, mid_2, final;
string student_name;

myout.open("students.dat");
if (myout)
{
cout << "Enter name of the student (type none to stop): ";
getline(cin, student_name);
while (student_name != "none")
{
myout << student_name << endl;
cout << "Enter student number: ";
cin >> student_number;
myout << student_number << endl;
cout << endl;
cout << "Enter Mid-term 1 mark: ";
cin >> mid_1;
myout << student_number << endl;
cout << endl;
cout << "Enter Mid-term 2 mark: ";
cin >> mid_2;
myout << student_number << endl;
cout << endl;
cout << "Enter Final mark: ";
cin >> final;
myout << student_number << endl;
cout << endl;
cout << "Enter name of the student (type none to stop): ";
getline(cin, student_name);
}
myout.close();
}
else
cout << "Could not create file." << endl;
}





What am I doing wrong?

TIA
Nuclear Krusader is offline   Reply With Quote
Old 03-14-2004, 09:19 AM   #3
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Hi Nuclear Krusader,

When something like "int i; std::cin >> i;" is executed, the user enters a number and hits enter, these numbers and the enter character '\n' are stored in a buffer, then cin gets the numbers, and leaves the '\n' character in the buffer.

This isn't usually a problem when you read another integer later, because cin skips white spaces when reading integers, and looks for numbers only, however, when reading a string after an integer (or a float in your program), cin finds the '\n' character, and sets the string to "\n", and the string that the user entered remains in the buffer, the next read (which reads a float), finds only characters in the buffer, the read fails, and the program gets stuck in an infinite loop trying to get a number.

To solve the problem, you need to clear the buffer before reading the string, so you remove the '\n' character, you can do so like this:
Code:
#include <limits>
// [...]
cin.ignore(numeric_limits::max(), '\n');
// numeric_limits::max() returns 
// the largest possible integer
The first param is the number of characters to clear, and the second param is the character that if encountered, will be removed from the buffer, and then ignore returns, leaving the remaining characters in the buffer.

When reading a number, you can make sure that the user entered numbers this way:
Code:
int i;
if (cin >> i) {
    // the user entered an integer
} else {
    // the user didn't enter an integer, 
    // we should clear the error bits in 
    // cin, clear the buffer, and try to read again
    cin.clear();
    cin.ignore(numeric_limits::max(), '\n');
}
As for your program, here is a version that works correctly: (changes are bold)
Code:
#include <iostream>
#include <string>
#include <fstream>

#include <limits>

using namespace std;

int main(void)
{
    ofstream myout;
    int student_number;
    float mid_1, mid_2, final;
    string student_name;

    myout.open("students.dat");
    if (myout)
    {
        cout << "Enter name of the student (type none to stop): ";
        getline(cin, student_name);
        while (student_name != "none")
        {
            myout << student_name << endl;
            cout << "Enter student number: ";
            cin >> student_number;
            myout << student_number << endl;
            cout << endl;
            cout << "Enter Mid-term 1 mark: ";
            cin >> mid_1;
            myout << mid_1 << ' ';
            cout << endl;
            cout << "Enter Mid-term 2 mark: ";
            cin >> mid_2;
            myout << mid_2 << ' ';
            cout << endl;
            cout << "Enter Final mark: ";
            cin >> final;
            myout << final;
            cout << endl;
            cout << "Enter name of the student (type none to stop): ";

            cin.ignore(numeric_limits::max(), '\n');

            getline(cin, student_name);           
        }
        myout.close();
    }
    else
        cout << "Could not create file." << endl;
}
HTH
aym is offline   Reply With Quote
Old 03-17-2004, 01:07 AM   #4
Mondsreitersmann
 
Nuclear Krusader's Avatar
 
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
Thanks a lot aym_7. Got it working.
Nuclear Krusader is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:49 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2