Hello,
I have having some trouble with my code not working.
In my book I am in the file i/o part so this is what the exercise is on.
My program is one where the user can:
1) make a record in the categories model, make, year, and mileage,
2) read a record for those same categories
3)exit
I have two different functions for the writing and the reading.
My problem is that when the user enters to write or read a file the program immediately skips the line that allows the user to type in a file and skips to "the file could not be open" My only clue to what the problem might be is that I use getline and cin in the same program, which I have heard can cause problems.
Your help would be much appreciated,
styles
Below is the code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int get_int(int default_value);
void write_bin();
void read_bin();
char model[21];
char make[21];
char year [6];
int main() {
while (1) {
int choice;
cout << "Welcome to the car file system" << endl;
cout << "******************************" << endl;
cout << "Press 1 to write a record." << endl;
cout << "Press 2 to read a record." << endl;
cout << "Press 3 to exit." << endl;
cin >> choice;
if (choice == 1) {
write_bin();
}
if (choice == 2) {
read_bin();
}
if (choice == 3){
system("PAUSE");
return 0;
}
}
}
void write_bin() {
char filename[81];
int n;
int mileage;
int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
cout << "Enter file name: ";
cin.getline(filename, 80);
// Open file for binary read and write.
fstream fbin(filename, ios::binary | ios::in | ios::out);
if (!fbin) {
cout << "Could not open file " << filename;
}
// Get record number to write to.
while (1) {
cout << "Enter file record number: ";
n = get_int(0);
if (n == -1) {
break;
}
// Get data from end user.
cout << "Enter model: ";
cin.getline(model, 20);
cout << "Enter make: ";
cin.getline(make, 20);
cout << "Enter year: ";
cin.getline(year, 5);
cout << "Enter mileage: ";
mileage = get_int(0);
// Write data to the file.
fbin.seekp(n * recsize);
fbin.write(model, 20);
fbin.write(make, 20);
fbin.write(year, 5);
fbin.write(reinterpret_cast<char*>(&mileage), sizeof(int));
fbin.close();
}
}
void read_bin() {
char filename[81];
int n;
int mileage;
int recsize = sizeof(model) + sizeof(make) + sizeof(year) + sizeof(int);
cout << "Enter file name: ";
cin.getline(filename, 80);
// Open file for binary read-write access.
fstream fbin(filename, ios::binary | ios::in | ios::out);
if (!fbin) {
cout << "Could not open file " << filename;
}
while(1) {
// Get record number and go to record.
cout << "Enter file record number (-1 to quit): ";
n = get_int(-1);
if (n == -1)
break;
fbin.seekp(n * recsize);
// Read data from the file.
fbin.read(model, 20);
fbin.read(make, 20);
fbin.read(year, 5);
fbin.read(reinterpret_cast<char*>(&mileage), sizeof(int));
// Display the data and close.
cout << "The model is: " << model << endl;
cout << "The make is: " << make << endl;
cout << "The year is: " << year << endl;
cout << "The mileage is: " << mileage << endl;
}
fbin.close();
}
int get_int(int default_value) {
char s[81];
cin.getline(s, 80);
if (strlen(s) == 0)
return default_value;
return atoi(s);
}