C++ Help. Will rate So I am reading data from a file telegram.txt and the genera
ID: 3833168 • Letter: C
Question
C++ Help. Will rate
So I am reading data from a file telegram.txt and the general format that is on the file is this:
Leslie Knope
1456 Plymouth Street
Pawnee, IN 47408
Amount Owed: $35.50
Tom Haveford
689 Lil Sebastian Avenue
Pawnee, IN 47408
Amount Owed: $11.00
April Ludgate
1123 9th Avenue
Wamapoke, IN 48034
Amount Owed: $18.00
I need to calculate the total of all the amounts owed in the file. and it must work no matter how many amounts owed there are. here is what i have so far that gets the data and displays it. but i cant figure out how to add them together
// Retrieve document and diplay needed values in program
{
ifstream inputFile;
string name1,addr1,city1,state1, blankLine;
int zip1, num_words1;
// To get this part to work smoothy I had to include the whole path to retrevie the file. but it does work.
inputFile.open("TelegramData.txt");
if (inputFile.fail())
{
cout << "Error opening file. ";
}
else
{
//Process the file.
}
while (!inputFile.eof())
{
getline (inputFile, name1);
getline (inputFile, addr1);
inputFile >> city1;
inputFile >> state1;
inputFile >> zip1;
inputFile >> num_words1;
blocks = (num_words1/5*BLOCK_RATE);
singles = (num_words1 % 5*SINGLE_RATE);
cost = blocks + singles;
cout<< name1<<endl;
cout<< addr1<< endl;
cout<< city1<< endl;
cout<< state1<<endl;
cout<< zip1<< endl;
cout<< "$"<<setprecision(4)<<showpoint<<cost<<endl;
cout<< " "<< endl;
// Get empty separator line and discard it
// Since switching between ">>" and getline() for input, must clear input buffer
inputFile.clear(); // resets stream flags
inputFile.ignore(10,' '); // reads buffer for up to 10 characters, exiting upon read of EOL character
getline (inputFile, blankLine); // now read the blank line in
}
inputFile.close();
cout<< "The total cost of all telegram bills is: " << cost << endl; //?????? This is where i need help i need a total of all the "cost" numbers in the file
}
Explanation / Answer
Hi, I wrote the code to extract cost part from line.
Since you have not posted whole program, so i did not test it.
{
ifstream inputFile;
string name1,addr1,city1,state1, blankLine, num_words1;
int zip1;
int cost = 0;
// To get this part to work smoothy I had to include the whole path to retrevie the file. but it does work.
inputFile.open("TelegramData.txt");
if (inputFile.fail())
{
cout << "Error opening file. ";
}
else
{
//Process the file.
}
while (!inputFile.eof())
{
getline (inputFile, name1);
getline (inputFile, addr1);
inputFile >> city1;
inputFile >> state1;
inputFile >> zip1;
inputFile.ignore(10,' '); // this is to ignore the line after reading zip code
getline (inputFile, num_words1);
int index = num_words1.find('$'); // finding the index of $
num_words1 = num_words1.substr(index+1, num_words1.length()-index);// finding cost part
int x = (int)atof(num_words1.c_str()); // converting into integer
blocks = (num_words1/5*BLOCK_RATE);
singles = (num_words1 % 5*SINGLE_RATE);
cost = blocks + singles;
cout<< name1<<endl;
cout<< addr1<< endl;
cout<< city1<< endl;
cout<< state1<<endl;
cout<< zip1<< endl;
cout<< "$"<<setprecision(4)<<showpoint<<cost<<endl;
cout<< " "<< endl;
// Get empty separator line and discard it
// Since switching between ">>" and getline() for input, must clear input buffer
inputFile.clear(); // resets stream flags
inputFile.ignore(10,' '); // reads buffer for up to 10 characters, exiting upon read of EOL character
getline (inputFile, blankLine); // now read the blank line in
}
inputFile.close();
cout<< "The total cost of all telegram bills is: " << cost << endl; //?????? This is where i need help i need a total of all the "cost" numbers in the file
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.