Saturday, May 22, 2010

C++ HELP, I can get the program running, but I can figure out how to add things together from a inFile.?

#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int main ()


{ double sum;


int count ;


int val;


ifstream infile;


char a,b,c;





infile.open ("p3_data.txt");


if (infile.is_open())


{


while (infile.good())


cout %26lt;%26lt; (char) infile.get();


infile.close();


}


else


{


cout %26lt;%26lt; "Error opening file\n";


}





cout %26lt;%26lt; "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=...


cout %26lt;%26lt; "\nPlease enter either A, B, or C to be calculated!\n";


cin %26gt;%26gt; a%26gt;%26gt;b%26gt;%26gt;c;


cout %26lt;%26lt; "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-...


cout %26lt;%26lt; "The bills add up to "%26lt;%26lt;endl;


system("pause");


return 1;


}


/*My program is only adding up 3 different things A B C. I can do everything else I just cant figure this one out. */


Thanks for the help

C++ HELP, I can get the program running, but I can figure out how to add things together from a inFile.?
I'm not exactly sure about what you are trying to do, but if you are trying to add up three numbers, each separated by spaces, one per line, then this might get you started:





#include %26lt;iostream%26gt;


#include %26lt;fstream%26gt;


using namespace std;





int main ()


{


ifstream infile;


int a,b,c;





infile.open ("p3_data.txt");


if (infile.is_open())


{


while (!infile.eof())


{


infile %26gt;%26gt; a %26gt;%26gt; b %26gt;%26gt; c;


cout %26lt;%26lt; a %26lt;%26lt; " + " %26lt;%26lt; b %26lt;%26lt;


" + " %26lt;%26lt; c %26lt;%26lt; " = " %26lt;%26lt;


a+b+c %26lt;%26lt; endl;


}


}


else


{


cout %26lt;%26lt; "Error opening file" %26lt;%26lt; endl;


return 1;


}





infile.close();


return 0;


}
Reply:Use "write()" method of fstream class to write the data into file.


No comments:

Post a Comment