|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Mondsreitersmann
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. |
|
|
|
|
|
#2 |
|
Mondsreitersmann
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 |
|
|
|
|
|
#3 |
|
Registered User
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 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
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
|
|
|
|
|
|
#4 |
|
Mondsreitersmann
Join Date: Jul 1999
Location: Skingrad
Posts: 8,781
|
Thanks a lot aym_7. Got it working.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|