Problem with my vector
Apr 25, 2015 at 3:48pm UTC
The purpose of this code is read structured values of hour & temperature readings, like so:
(5, 81.7)
The program reads in the temperature then placed into a vector and then calculates the average.
The problem I'm having seems to be the the vector. I keep getting a range error. Any advice?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#include "std_lib_facilities.h"
int main(){
string file_name = "C:/Users/Admin/Desktop/raw_temps.txt" ";
ifstream ist(file_name);
if (!ist) error(" can't open input file", file_name);
char ch1;
char ch2;
char ch3;
int hour = 0;
string line;
double sum = 0;
double average = 0;
double temperture = 0;
vector<double>temp;
while (ist >> ch1 >> hour >> ch2 >> temperture >> ch3)
temp.push_back(temperture);
cout << ' \n';
for (int i = 0; temp.size(); i++){
sum = sum + temp[i];
}
average = sum / temp.size();
cout << "The Average is: " << average;
system("pause");
return 0;
}
Apr 25, 2015 at 3:55pm UTC
it's this line:
for (int i = 0; temp.size(); i++){
needs to be something like this:
for (int i = 0; i < temp.size(); i++){
Topic archived. No new replies allowed.