Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads the amount of coins collected at the end of each day

ID: 3690055 • Letter: W

Question

Write a program that reads the amount of coins collected at the end of each day of a month in the CS1 Supermarket and calculates the total amount in dollars. The program reads the data for the entire month from the file coinsCoins.txt (assume the month has 30 days). The file coinsCoins.txt has one line for each day where each line has: number of pennies, space, number of Nickels, space, number of Dimes, space, number of Quarters. Make sure that the total amount is displayed with exactly two decimals. 1 Nickle = 5 pennies, 1 Dime = 10 pennies, 1 Quarter = 25 pennies, and 1 Dollar = 100 pennies.

For the sake of testing your program consider a coinsCoins.txt file where the first 28 lines, each contains 0 0 0 0 and the last two lines are 0 117 430 705 and 0 320 414 833. The file is for a month where no coins where collected in the first 28 days.

Explanation / Answer

#include <sstream>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{

double sum = 0;
int tp = 0, tn = 0, td = 0, tq = 0;
ifstream infile("coinsCoins.txt");
string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
int p,n,d,q;
if (!(iss >> p >> n >> d >> q)) { break; } // error
  
tp = tp + p;
tn = tn + n;
td = td + d;
tq = tq + q;
  
}
  
sum = (tp) + (tn*5) + (td*10) + (tq*25);
  
sum = sum/100;
  
cout << "Total amount in Dollars = " << flush;
cout << setprecision(5) << sum << endl;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote