i have some issue with reading different data type from the file
data type format is it month/day step count for each line
sample
a
2a
3/4a
1/1 10001
1/2 9002
1/4 4300
your program should enable to detect which line is valid data, which is not.
sample output will be like this
-Unable to read month (or reached end-of-file) from line 1 of the input file
-Did not find expected slash from line 2 of the input file
-Unable to read activity from line 3 of the input file
// i have some issue with reading different data type from the file
// data type format is it month/day step count for each line
// sample
// a
// 2a
// 3/4a
// 1/1 10001
// 1/2 9002
// 1/4 4300
// your program should enable to detect which line is valid data, which is not.
// sample output will be like this
// -Unable to read month (or reached end-of-file) from line 1 of the input file
// -Did not find expected slash from line 2 of the input file
// -Unable to read activity from line 3 of the input file
// My code so far gets each data type right but I cannot get data type char
// to month and compare like it data type int.
#include <fstream>
#include <iostream>
#include <string>
int main()
{
std::ifstream inputFile;
std::string fileName;
int month,day,stepCount;
char slash;
std::string line;
int countLine=1;
std::cout<<"Enter filename: ";
std::cin>>fileName;
inputFile.open(fileName);
if(!inputFile.fail())
{
while(inputFile>>month>>slash>>day>>stepCount)
{
if(month<=0 || month>=12)
{
std::cout<<"Unable to read month (or reached end-of-file) from line"
<< countLine <<"of the input file"<<std::endl;
}
else { std::cout<<month<<slash<<day<<stepCount; }
countLine++;
}
inputFile.close();
}
else { std::cout<<"error opening file !"<<std::endl; }
return 0;
}
Output:
Enter filename: macaogiay.dat
If you added a couple of output instructions you’d discover the (sorry?) truth: