Helios093
Helios093

Reputation: 1

ifstream giving me trouble

im using fstream to get visual studio to read a file with about 100 lines of repeated info, just different values every line. Im using a count variable to keep track of how many times this is read, but it keeps saying 0. i know the file is being opened because i placed an If statement to check for me. and yes the file im reading from is also the location of my cpp file. If you could take a look and tell me what im missing i would appreciate it!

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

int main() {
//file variables
string date;
int rainIn, minTempF, maxTempF;

//variables
int count = 0;
double totalRain = 0;
double averageMinimumTemp = 0;
double averageMaximumTemp = 0;
double overallMaxTemp = 0;
double overallMinTemp = 0;

ifstream inFile("2014WeatherData.txt");

if (!inFile) {
    cout << "Error: Input File Cannot Be Opened\n";
    exit(EXIT_FAILURE);
}

//read file records
while (inFile >> date >> rainIn >> maxTempF >> minTempF) {
    count++
}

cout << count << " Records read\n";

return 0;
}

im only in computer science 1, so im still learning any feedback will be very much appreciated!! also here is a few lines from the txt document im trying to read from

20140101    0.00    69.08   31.10
20140102    0.00    42.98   25.16
20140103    0.00    51.98   25.16

Upvotes: 0

Views: 81

Answers (1)

Tony Delroy
Tony Delroy

Reputation: 106068

The problem is that...

int rainIn, minTempF, maxTempF;

...creates integer variables, and you try to read floating point values into them. Change them to doubles. (You also need a semicolon after count++).

Upvotes: 1

Related Questions