Array Help

Why doesn't my newmaster.txt file have any data in it. Even when I cout << Fname[count]; after my while loop I don't get anything printed on screen. If I do cout << Fname; I get number printed on the screen. Please help. Thank you,


#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


ofstream PayrollOut;

int _tmain(int argc, _TCHAR* argv[])
{
ifstream PayrollIn;

PayrollIn.open("master.txt");
PayrollOut.open("newmaster.txt");

if(!PayrollIn || !PayrollOut)
{
cout << "Input/Output file(s) not opened\n";
cout << "Program ending\n";
exit(1);
}


string Fname[25], Lname[25], ID[25];
float Pay[25];
int Benefit[25];
int count = 0;

PayrollIn >> Fname[count] >> Lname[count] >> ID[count] >>
Pay[count] >> Benefit[count];


while(PayrollIn)
{
count++;
PayrollIn >> Fname[count] >> Lname[count] >> ID[count] >>
Pay[count] >> Benefit[count];

}



for(count=0; count < 0; count++)
{
PayrollOut << Fname[count] << Lname[count] << ID[count] <<
Pay[count] << Benefit[count];
}

system("pause");

return 0;
}
In this loop

for(count=0; count < 0; count++)

you assign zero to count. So the loop will be executed never and you will lose the current number of entered data.
Ok that makes sense. Thank you for that. So I changed it to this:

for(int i = 0; i < count; i++)
{
PayrollOut << Fname << Lname << ID <<
Pay << Benefit;
}

and now my output file is still all garbage.

0047F4B80047F1900047EE680047EDFC0047ED900047F4B80047F1900047
Last edited on
and now my output file is still all garbage.


That might be because in the following:

1
2
3
4
5
for(int i = 0; i < count; i++)
{
PayrollOut << Fname << Lname << ID <<
Pay << Benefit;
} 


you're storing the same memory addresses over and over again.
You might try using i to index the arrays.

You forgot to use indexes for arrays' elements.

PayrollOut << Fname[i] << Lname[i] << ID[i] <<
Pay[i] << Benefit[i];
Last edited on
Got it! thank you very much for your help.
Topic archived. No new replies allowed.