Sorry, I was offline for a while there.
Originally Posted by
Stunner
Wow thanks a lot dude. Your code works, but whenever I change my code to match yours i get errors... such as:
error C2086: 'inmaster' : redefinition
error C2086: 'intrans' : redefinition
at the places where your define ifstream inmaster and intrans.
You missed the braces that I added. The indentation is really bad, but I added some {'s and }'s in. Look closely, or just copy my code in entirety.
Also, you are able to declare int i at both for loops while I cannot. The thing that stumps me is that when I copy and paste your code it works, but when I change it myself I get errors... why?
I just explained this above. I should have made the extra braces clearer. They're blue, but the eye tends to skip over things that look familiar, I guess.
Here is my entire code with all your suggestions and the changes you made to it:
I've added the braces, marked very conspicuously.
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct record
{
char name[15];
double amt;
};
int main()
{
record master[10] = {{"Helen", 10.},{"Julie", 20.},{"Lena", 30.},{"Alan", 40.},{"Annie", 50.},{"May", 60.},{"Lee", 70.},{"Sam", 80.},{"June", 90.},{"Bill", 100.}};
record trans[7] = {{"Lena", 10.},{"Julie", 5.75},{"Lee", 15.02},{"Ed", 40.},{"Julie", 10.00},{"Art", 5.00},{"Bill", 7.32}};
record temp, temp1, temp2;
int i;
ofstream outmaster;//creating master rec
outmaster.open("Master.dat", ios::binary);
for(i = 0; i < 10; i++)
{
outmaster.write((char*)&master[i], sizeof(record));
}//for
outmaster.close();
ofstream outtrans;//creating trans rec
outtrans.open("Trans.dat", ios::binary);
for(i = 0; i < 7; i++)
{
outtrans.write((char*)&trans[i], sizeof(record));
}//for
outtrans.close();
//Arranging Data
ifstream inmaster;
inmaster.open("Master.dat", ios::binary);
ifstream intrans;
intrans.open("Trans.dat", ios::binary);
intrans.read((char*)&temp1, sizeof(record));
while(!intrans.eof())
{
inmaster.read((char*)&temp2, sizeof(record));
while(inmaster)
{
if(strcmp(temp1.name, temp2.name) == 0)
{
temp1.amt = temp1.amt + temp2.amt;
}//if
inmaster.read((char*)&temp2, sizeof(record));
}//while
intrans.read((char*)&temp1, sizeof(record));
}//while
inmaster.close();
intrans.close();
{ /* begin new block */
//Printing Data
cout << "Master.dat data: \n\n";
ifstream inmaster;
inmaster.open("Master.dat", ios::binary);
inmaster.read((char*)&temp, sizeof(record));
while(inmaster)
{
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
cout << temp.name;
cout.width(15 - strlen(temp.name));
cout.setf(ios::right);
cout << temp.amt << endl;
inmaster.read((char*)&temp, sizeof(record));
}//while
inmaster.close();
cout << "\nTrans.dat data: \n\n";
ifstream intrans;
intrans.open("Trans.dat", ios::binary);
intrans.read((char*)&temp, sizeof(record));
while(intrans)
{
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
cout << temp.name;
cout.width(15 - strlen(temp.name));
cout.setf(ios::right);
cout << temp.amt << endl;
intrans.read((char*)&temp, sizeof(record));
}//while
intrans.close();
} /* end block */
return 0;
}//main
It's not a good idea to do that, though. I just did it to get it to compile because I suspected the streams were in an error state. The better solution is below:
> and also consider clearing the error state of those filestreams before you re-use them.
What do you mean by this?
If you're going to use the same variable, call
Of course, if you fix the 7 bug (I explain it lower down), then you don't have to worry about streams in error states, because it shouldn't happen. Still, it could happen, so it's best to handle it.
Also, I am confused as to why there would be a 7 element bug, since the loops should stop when there are no more names to read; also I fixed the 7 element counter that you pointed out. Thanks a lot for your help this is greatly appreciated!
Since the first loop went until 10 where it should have gone until 7, three garbage structures were written to the file. When reading from the file, the last three are messed up, not because of any error on the part of the code but because the actual data in the file is messed up. Here's the output I get:
Code:
Master.dat data:
Helen 10.00
Julie 20.00
Lena 30.00
Alan 40.00
Annie 50.00
May 60.00
Lee 70.00
Sam 80.00
June 90.00
Bill 100.00
Trans.dat data:
Lena 10.00
Julie 5.75
Lee 15.02
Ed 40.00
Julie 10.00
Art 5.00
Bill 7.32
©ËP€d[6..Helen0.00
0.00
0.00
I think you can see what I mean.
So here are the changes I suggest all applied to your program. Enjoy. (I was going to run it through codeform, but my computer's crashed so badly that I can't. Maybe once I reboot.)
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
struct record
{
char name[15];
double amt;
};
int main()
{
record master[10] = {{"Helen", 10.},{"Julie", 20.},{"Lena", 30.},{"Alan", 40.},{"Annie", 50.},{"May", 60.},{"Lee", 70.},{"Sam", 80.},{"June", 90.},{"Bill", 100.}};
record trans[10] = {{"Lena", 10.},{"Julie", 5.75},{"Lee", 15.02},{"Ed", 40.},{"Julie", 10.00},{"Art", 5.00},{"Bill", 7.32},{"empty",0.0},{"empty",0.0},{"empty",0.0}};
record temp, temp1, temp2;
int i;
ofstream outmaster;//creating master rec
outmaster.open("Master.dat", ios::binary);
for(i = 0; i < 10; i++)
{
outmaster.write((char*)&master[i], sizeof(record));
}//for
outmaster.close();
ofstream outtrans;//creating trans rec
outtrans.open("Trans.dat", ios::binary);
for(i = 0; i < 7; i++)
{
outtrans.write((char*)&trans[i], sizeof(record));
}//for
outtrans.close();
//Arranging Data
ifstream inmaster;
inmaster.open("Master.dat", ios::binary);
ifstream intrans;
intrans.open("Trans.dat", ios::binary);
intrans.read((char*)&temp1, sizeof(record));
while(!intrans.eof())
{
inmaster.read((char*)&temp2, sizeof(record));
while(inmaster)
{
if(strcmp(temp1.name, temp2.name) == 0)
{
temp1.amt = temp1.amt + temp2.amt;
}//if
inmaster.read((char*)&temp2, sizeof(record));
}//while
intrans.read((char*)&temp1, sizeof(record));
}//while
inmaster.close();
intrans.close();
//Printing Data
cout << "Master.dat data: \n\n";
inmaster.clear();
inmaster.open("Master.dat", ios::binary);
inmaster.read((char*)&temp, sizeof(record));
while(inmaster)
{
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
cout << temp.name;
cout.width(15 - strlen(temp.name));
cout.setf(ios::right);
cout << temp.amt << endl;
inmaster.read((char*)&temp, sizeof(record));
}//while
inmaster.close();
cout << "\nTrans.dat data: \n\n";
intrans.clear();
intrans.open("Trans.dat", ios::binary);
intrans.read((char*)&temp, sizeof(record));
while(intrans)
{
cout.setf(ios::showpoint);
cout.setf(ios::fixed);
cout.precision(2);
cout << temp.name;
cout.width(15 - strlen(temp.name));
cout.setf(ios::right);
cout << temp.amt << endl;
intrans.read((char*)&temp, sizeof(record));
}//while
intrans.close();
return 0;
}//main