we were given a text file with string date; string city; double temperature; string weather; but the .txt file formats this into one continuous line for 4 different instances.
I know how to read data from a file, if the data in the .txt file goes line by line, but this file goes as a long string essentially.
this is what I did to just try and read the file data and see if I could do that correctly.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main ()
{
ifstream input("weather.txt");
string date;
string cITY; //compare to input city string
double temp;
string wEATHER;//compare to input weather string
while (!input.eof())//while not end of line
{
input>>date>>cITY>>temp>>wEATHER;
if (input.eof()) break;
cout<<date<<" "<<cITY<<" "<<temp<<" "<<wEATHER;
input.close();
}
return 0;
}
but the file looks like this
2/28/2019 Boston 27 Snow 2/28/2019 Miami 72 Sun 3/1/2019 London 62 Rain
3/2/2019 Boston 34 Clouds
and I got this output
-bash-4.2$ g++ -std=c++11 readtest.cpp -o pr
-bash-4.2$ ./pr
2/28/2019 Boston 27 Snow-bash-4.2$
shouldn't the !input.eof() work, even though? because the input>>date>>cITY>>temp>>wEATHER; defines the data categories?
Also if I want to calculate the probability of a weather condition happening, should I store all city and weather data into a multi-dimensional array, and then calculate probabilities by indexing into the arrays? ie. have an array for each city.
Aucun commentaire:
Enregistrer un commentaire