Thursday, July 9, 2009

C++ reading from file into array using a function?

text file contains title of store then a product code, product name, then product price. so type string, string, long respectively. program compiles and runs fine until count is returned in main. something is wrong with the loop and i don't know what. this is the important stuff:


--------------------------- function: ----------------------------


long readFile (string code[], string product[], long price[])


{


const long ITEMS = 24;


string temp,


store;


long count;


ifstream fin ("program5.txt");





getline(fin,store);


count=0;


fin%26gt;%26gt;temp;





while (!fin.eof())


{


temp=code[count];


getline(fin,product[count]);


fin%26gt;%26gt;price[count];





count++;


fin%26gt;%26gt;temp;


}


fin.close();


return count;


}


----------------from main:-----------------


.......


count=readFile(code,product,price);


fout%26lt;%26lt;count%26lt;%26lt;endl;





for (pos=0; pos%26lt;count; pos++)


{


fout%26lt;%26lt;code[pos]%26lt;%26lt;endl;


fout%26lt;%26lt;product[pos]%26lt;%26lt;endl;


fout%26lt;%26lt;price[pos]%26lt;%26lt;endl;


}


....





if more info is needed i can provide it. reminder this is c++

C++ reading from file into array using a function?
count looks ok--not sure why count is giving you a problem.





Look at the line just following the while declaration. You have the left side and right side inverted. Use:





code[count] = temp;

survey monkey

No comments:

Post a Comment