My file is not being created
Feb 14, 2017 at 7:17pm UTC
Write your question here.
how do I create a file?!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <fstream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
using namespace std;
struct data
{
int ID; //000-1000
char gender; //m for male f for female
int credits; //0-124
};
char randChar();
int main()
{
fstream records("findme.txt" );
records.open("findme.txt" );
if (!records.is_open())
{
cout << "File did not open!" << endl << endl;
}
srand(time(NULL));
int R;
data cred;
cout << "Enter the number of records (20-60): " ;
cin >> R;
if (R < 20 || R > 60)
{
cout << "A number between 20 through 60: " ;
cin >> R;
}
records << "Number of records: " << R << endl << endl;
for (int ix = 0; ix < R; ix++)
{
cred.credits = rand()%125;
cout << endl;
cred.ID = rand()%1001;
cout << endl;
//cred.gender = randChar();
//cout << endl;
records << "ID: " << cred.ID << " Gender: " << cred.gender << " Credits: " << randChar() << endl;
}
records.close();
int credarr[100];
records.open("findme.txt" );
int u;
double avg=0,avgstorage;
for (int jx = 0; jx < R;jx++)
{
avg += cred.credits;
}
avgstorage = avg / R;
records.close();
cout << "The file data.bin processed " << R << " records and the average number of credits is " << avgstorage << "." << endl;
return 0;
}
char randChar()
{
if ( rand() % 2 ) return 'M' ;
else return 'F' ;
}
Feb 14, 2017 at 7:38pm UTC
What problem are you having?
Line 20: This line is unnecessary. The file is implicitly opened because a filename was specified on line 19.
Line 24: If the file failed to open, you display a message, but then continue as if nothing was wrong. Not a good idea. You should call exit() or return 1.
Line 34: An if statement will only detect a single mistake. What if the user enters an invalid R multiple times? You won't detect it. This should be a
while
statement.
Line 52: You're writing cred.gender, but cred.gender was never initialized.
Line 52: You're writing randChar() for the number of credits.
Line 64: You are repeatedly adding the contents of cred.credits. credit was a temporary variable used in line 45-52. At this point it contains only the data for the last entry made.
Last edited on Feb 14, 2017 at 7:41pm UTC
Feb 14, 2017 at 7:39pm UTC
updated
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
#include <iostream>
#include <fstream>
#include <ctime> // Needed for the true randomization
#include <cstdlib>
using namespace std;
struct data
{
int ID; //000-1000
char gender; //m for male f for female
int credits; //0-124
};
char randChar();
int main()
{
srand(time(NULL));
int R;
data cred;
ofstream records("Data.bin" , ios::binary);
records.write((char *) &cred, sizeof (data));
if (!records.is_open())
{
cout << "File did not open!" << endl << endl;
}
//records.open("data.bin");
cout << "Enter the number of records (20-60): " ;
cin >> R;
if (R < 20 || R > 60)
{
cout << "A number between 20 through 60: " ;
cin >> R;
}
records << "Number of records: " << R << endl << endl;
for (int ix = 0; ix < R; ix++)
{
cred.credits = rand()%125;
cout << endl;
cred.ID = rand()%1001;
cout << endl;
//cred.gender = randChar();
//cout << endl;
records << "ID: " << cred.ID << " Gender: " << cred.gender << " Credits: " << randChar() << endl;
}
records.close();
int credarr[100];
records.open("Data.bin" );
int u;
double avg=0,avgstorage;
for (int jx = 0; jx < R;jx++)
{
avg += cred.credits;
}
avgstorage = avg / R;
records.close();
cout << "The file data.bin processed " << R << " records and the average number of credits is " << avgstorage << "." << endl;
return 0;
}
char randChar()
{
if ( rand() % 2 ) return 'M' ;
else return 'F' ;
}
Feb 14, 2017 at 7:46pm UTC
Line 22: cred is uninitialized.
Line 25: You're writing an uninitialized data structure.
Line 60: records was declared as an ofstream. You're going to be opening the file for output.
All my previous comments still apply.
Topic archived. No new replies allowed.