Array Help

Mar 27, 2013 at 6:11pm
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;
}
Mar 27, 2013 at 6:28pm
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.
Mar 27, 2013 at 6:50pm
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 Mar 27, 2013 at 6:51pm
Mar 27, 2013 at 6:56pm
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.

Mar 27, 2013 at 6:56pm
You forgot to use indexes for arrays' elements.

PayrollOut << Fname[i] << Lname[i] << ID[i] <<
Pay[i] << Benefit[i];
Last edited on Mar 27, 2013 at 6:56pm
Mar 27, 2013 at 6:59pm
Got it! thank you very much for your help.
Topic archived. No new replies allowed.