117k views
0 votes
Please code this in c++

5.19 Farmer's market - files

Get the file name from the user and open it using code. The file has the product name and price/lb in each line.

Until the EOF is reached:

Read the product name from the file

prompt the user for entering the weight of that product. (Points will be taken off if you hard-code the product name inside your code).

Once the user enters the weight(0 if user does not buy that item), calculate the cost of that item by reading the price of that item from the file and multiplying by the weight entered by the user.

Maintain a running total of the cost until all the items have been entered by user.

Once EOF is reached, display the total cost of the purchase

product.txt // contains the following

apple 1.59
orange 0.99
banana 0.69
grapes 2.99

User Tarks
by
4.9k points

1 Answer

6 votes

Answer:

See explaination

Step-by-step explanation:

#include<iostream>

#include<fstream>

using namespace std;

int main(){

double price, totalPrice = 0, weight;

string product, filename;

cout<<"Enter filename: ";

cin>>filename;

ifstream fin;

fin.open(filename.c_str());

while(fin>>product>>price){

cout<<"Enter weight for "<<product<<": ";

cin>>weight;

totalPrice+=price*weight;

}

cout<<"\\The total cost of the purchase: $"<<totalPrice<<endl;

return 0;

}

User Bullionist
by
5.0k points