Hello,
How do you read a file in C++?
For example, I have a text file data.txt contains:
hammer#12.20
saw#20.50
shovel#9.25
Now I need to write a program to read all the data in data.txt and totals the price of all items. The output print out should look like:
hammer 12.20
saw 20.50
shovel 9.25
Total: 71.2
I haven't study array yet, just use loops to write this program. This is my code, but It's not work.
PHP Code:
-------------------------------------------------------
#include <iostream>
#include <fstream>
using namespace std;
int main( void )
{
string newPart;
char ch;
ifstream inData;
inData.open("data.txt" );
inData.get(ch);
while (!inData.eof()) {
while(ch != '#') {
newPart += ch;
}
inData.get(ch);
}
cout << newPart;
inData.close();
return 0;
}
-------------------------------------------------
Please help me! Thanks