Skip to content

Instantly share code, notes, and snippets.

@ChrisOrozco97
Created November 26, 2015 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ChrisOrozco97/1a592db27c1b41d7a474 to your computer and use it in GitHub Desktop.
Save ChrisOrozco97/1a592db27c1b41d7a474 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
int main (){
string Read;
string MidPrice;
string CityMpg;
string HighMpg;
int n=57;
int i;
float city;
float AverageCity=0.0;
float high;
float AverageHigh=0.0;
float price;
float AveragePrice=0.0;
ifstream read_file("93cars.dat.txt");
if (read_file.is_open()){
for (i=0;i<93;i++){
getline(read_file,Read);
MidPrice=Read.substr(42,4);
istringstream buffer(MidPrice);
buffer>>price;
CityMpg=Read.substr(52,2);
istringstream buffer2(CityMpg);
buffer2>>city;
HighMpg=Read.substr(55,2);
istringstream buffer3(HighMpg);
buffer3>>high;
AverageHigh= AverageHigh+high;
AveragePrice= AveragePrice+price;
AverageCity= AverageCity+city;
getline(read_file,Read);
}
read_file.close();
}
else{
cout<<"Cannot open the file, try again"<<endl;
}
AveragePrice= AveragePrice/93;
AverageCity= AverageCity/93;
AverageHigh= AverageHigh/93;
cout<<"Average midprice = "<<AveragePrice<<endl;
cout<<"Average City MPG = "<<AverageCity<<endl;
cout<<"Average Highway MPG = "<<AverageHigh<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment