I believe that I have my code ready to go without any errors. The problem I'm encountering is that when I'm entering the data file that is to be linked to this program, it isn't being found. The data file that is to be linked to this program is weatherdata.txt. Am I missing a piece of code which lets the compiler know that this is the file I am to be using to determine average wind speed and temperature for a 30 day period? Thank you for helping out.
#include <iostream>
#include <fstream>
#include <string>
#include<iomanip>
#include<stdlib.h>
usingnamespace std;
int main ()
{
char filename[20];
ifstream inFile;
float speed,temperature,avgspeed=0,avgtemp=0;
int count,getcount;
cout << "Enter the file name\n;";
cin >> filename;
inFile.open(filename);//open the file
if (inFile.fail())//error handling
{
cout<<"Input filename cannot be found\n";
exit(1);
}
inFile>>count>>speed>>temperature;//file details
while(inFile.good())
{
//calculate on the go
avgspeed=avgspeed+speed;
avgtemp=avgtemp+temperature;
getcount=count;
cout<<count<<" "<<speed<<" "<<temperature<<endl;
inFile>>count>>speed>>temperature;
}
inFile.close();//close the file after reading
avgspeed=avgspeed/getcount;
avgtemp=avgtemp/getcount;
cout << fixed; //set the precision after decimal to 2 values
cout <<setprecision(2);
cout<<"The average of the wind speed in the file is "<<avgspeed<<endl;
cout<<"The average of the temperatures in the file is "<<avgtemp<<endl;
return 0;
}
you should have the file locally on your computer: download it, place it in a directory you like .
e.g if you download it to Documents: when entering filename you should enter the path details.
where would I include the code for the path?
1 2 3 4
///when this runs, enter the path to your file
///"C:\\Users\\your username\\Documents\\weatherdata.txt" ///your path
cout << "Enter the file name\n;";
cin >> filename;
that simple.
EDIT:
also note your char filename[20]; might not be enough for such a long file name, just change file name to string or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
///option 1:
std::string filename{};
std::cout << "Enter the file name\n;";
std::cin>>filename;
///option 2:
///if you already know the path; e.g file in documents folder, you can:
std::string path("C:\\Users\\your username\\Documents\\");
std::string filename{};
std::cout << "Enter the file name\n;";
std::cin>>filename;///just enter weatherdata.txt only
//then
inFile.open(path + filename);