Reading selected data from a file

Ok, here goes... How I would access certain elements in my array to perform calculations??? i.e the elements in my array pressure[0]to[7]

#include <iostream>
#include <string>
#include <fstream>
#include<iomanip>
using namespace std;

int date[30];
int size=30;
double pressure[30];
double highTemp[30];
double lowTemp[30];
int windSpeed[30];
int batteryPower[30];
string solarPanelOpen[30];
int k=0;
string month;
double totalHighTemp;
double totalLowTemp;
double totalSpeed;
double totalPressure;
double result1;
double wk1Pressure;
double wk1avg;
int size1=7;
int size3=2;
int sum;
int main()
{
ifstream inCuriosityRoverFile("CuriosityRover.txt", ios::in );
if(!inCuriosityRoverFile)
{cerr<< "File could not be opened"<<endl;
exit(1);}// exit if statement
cout<<"***************************************************************"<<endl;
cout<<"Curiosity Data Collected"<<endl;
while(getline(inCuriosityRoverFile.seekg(0), month, ','))

{cout<<"\n\nMonth: " << month<<endl;
cout<<"***************************************************************"<<endl;
while(inCuriosityRoverFile >> date[k] >> pressure[k] >> highTemp[k]>>lowTemp[k]>> windSpeed[k]>>batteryPower[k]>>solarPanelOpen[k])
{cout<<setw(2)<<date[k]<<" " <<setw(6)<<pressure[k] <<" " << setw(6)<<highTemp[k] <<" " << setw(5)<<lowTemp[k] <<" "
<< windSpeed[k]<<setw(5)<<" " << batteryPower[k] <<" " << setw(5)<<solarPanelOpen[k] << endl;
++k;

cout<<"---------------------------------------------------------------"<<endl;
}//end end while loop

cout<<"\n**********Data Collected for April 2013 by the Rover*********"<<endl;

cout<<"\n**********************Monthly Summary************************"<<endl;
double lowest=100;
double highest=0.0;

for (int k=0;k<size;k++){
totalHighTemp=totalHighTemp+highTemp[k]/size;
totalLowTemp=totalLowTemp+lowTemp[k]/size;
totalSpeed=totalSpeed+windSpeed[k]/size;
totalPressure= totalPressure+pressure[k]/size;
if(batteryPower[k] > highest) highest = batteryPower[k];
if(batteryPower[k] < lowest) lowest = batteryPower[k];
}

double avg1=totalHighTemp;
double avg2=totalLowTemp;
double avg3=totalSpeed;
double avg4=totalPressure;

cout<<"Monthly average High Temperature: "<<avg1<<" Deg. Cel."<<endl;
cout<<"Monthly average Low Temperature: "<<avg2<<" Deg. Cel."<<endl;
cout<<"Monthly average Wind Speed: "<<avg3<<" Meters per second"<<endl;
cout<<setprecision(3)<<"Monthly average Pressure: "<<avg4<<" Kilo Pascal"<<endl;
cout<<"The highest battery power available: "<<highest<<"%"<<endl;
cout<<"The lowest battery power available: "<<lowest<<"%"<<endl;



system("pause");
}}//end main
Last edited on
Making your question more specific would help but you can access pressure[0] - pressure[7] with this:

1
2
3
4
5
6
7
8
9
void accessPressure(double& pressure[], int startElement, int endElement)
{
    vector<double> pressures;
    for ( ; startElement <= endElement ; startElement++)
    {
        pressures.push_back(pressure[startElement]);
        cout << pressure[startElement] << endl;
    }
}
Last edited on
Topic archived. No new replies allowed.