Trying to read from a file like this: Brand: Price: Amount: Toyota 2000 4 Honda
ID: 3633596 • Letter: T
Question
Trying to read from a file like this:Brand: Price: Amount:
Toyota 2000 4
Honda 3000 5
Ford 4000 6
Audi 5000 7
And calculate the total value of all the cars, as well as the average price for each car and output it to a file. Help is greatly appreciated; the first working program gets full points.
#include<fstream.h>
#include<stdlib.h>
#include<string>
using namespace std;
int main()
{
int n=4, i=0, j=0;
float price[n],amount[n], Value[n];
string Title[3],carbrand[4];
float Totalvalue=0;
//************************************
ifstream file("car.txt");
if(!open)
{
cout<<" Could not open ";
exit(-1);
}
else
{
for (i=0;i<4;i++)
{
file>>Title[i];
cout<<Title[i]<<" ";
}
cout<<" ";
for (i=0;i<4;i++)
{
cin>>carbrand[i]>>price[i]>>amount[i];
cout<<carbrand[i]<<" "<<price[i]<<" "<<amount[i]<<" "<<endl;
}
}
file.close();
//************************
Totalvalue=0;
for(i=1;i<4;i++)
{
Value[i]=price[i]*amount[i];
Totalvalue=Totalvalue+Value[i];
}
//*************************************
ofstream Write("Summary.txt");
if(!Write)
{
cout<<" Could not open ";
exit(-1);
}
else
{
for(i=0;i<4;i++);
{
Summary<<carbrand[i]<<" ";
Write<<price[i] <<" ";
}
Summary<<" ";
Summary<<Title[0]<<" ";
Write<<" ";
Write<<Title[2]<<" ";
for(i=1;i<5;i++)
{
Write<<amount[i]<<" ";
}
Write<<" ";
Write<<"Value"<<" ";
for(j=1;j<5;j++);
{
Write<<Value[i]<<" ";
}
Write<<" ";
Write<<"The Total Value of all these cars is: ";
Write<<Totalvalue<<endl;
}
return 0;
Explanation / Answer
please rate - thanks
hope this is good. you just gave code, didn't explain what it's supposed to do
#include <iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int main()
{
int n=4, i=0, j=0;
float price[n],amount[n], Value[n];
string Title[3],carbrand[4];
float Totalvalue=0;
//************************************
ifstream file;
file.open("car.txt");
if(!file)
{
cout<<" Could not open ";
exit(-1);
}
else
{
for (i=0;i<3;i++)
{
file>>Title[i];
cout<<Title[i]<<" ";
}
cout<<" ";
for (i=0;i<4;i++)
{
file>>carbrand[i]>>price[i]>>amount[i];
cout<<carbrand[i]<<" "<<price[i]<<" "<<amount[i]<<" "<<endl;
}
}
file.close();
//************************
Totalvalue=0;
for(i=0;i<4;i++)
{
Value[i]=price[i]*amount[i];
Totalvalue=Totalvalue+Value[i];
}
//*************************************
ofstream Summary;
Summary.open("Summary.txt");
if(!Summary)
{
cout<<" Could not open ";
exit(-1);
}
else
{
for(i=0;i<3;i++)
Summary<<Title[i]<<" ";
Summary<<"Value ";
for(i=0;i<4;i++)
{
Summary<<carbrand[i]<<" ";
Summary<<price[i] <<" ";
Summary<<amount[i]<<" "<<Value[i];
Summary<<" ";
}
Summary<<"The Total Value of all these cars is: $";
Summary<<Totalvalue<<endl;
}
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.