reading different data type from file and compare

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

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
  int main()
{
	ifstream inputFile;
	string fileName;
	int month,day,stepCount;
	char slash;
	string line;
	int countLine=1;

	cout<<"Enter filename :";
	cin>>fileName;

	inputFile.open(fileName);
	if(!inputFile.fail())
	{
		while(inputFile>>month>>slash>>day>>stepCount)
		{

			if(month<=0 || month>=12)
			{
				cout<<"Unable to read month (or reached end-of-file) from line"<< countLine <<"of the input file"<<endl;
			}
			else
			{
			cout<<month<<slash<<day<<stepCount;
			}
		countLine++;
		}
		inputFile.close();
	}
	else
	{
		cout<<"error opening file !"<<endl;
	}


	return 0;
}

it gets each data type right but I cannot get data type char to month and compare like it data type int.
please help
My code so far ... it gets each data type right

How do you know? Your program doesn’t output anything.

macaogiay.dat:
a
2a
3/4a
1/1 10001
1/2 9002
1/4 4300


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
41
42
43
44
45
46
47
48
49
50
// 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:
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
#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())
    {
        std::cout << "I'm about to start reading from file...\n";
        while(inputFile>>month>>slash>>day>>stepCount)
        {
            std::cout << "month: " << month << "; slash: " << slash
                      << "; day: " << day << "; stepCount: " << stepCount << '\n';
            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
I'm about to start reading from file...

Your program doesn’t even start reading from file: the while loop fails at the first iteration.

If you don’t know how many data are there in each line, I’m afraid you need to read them by means of std::getline().
Topic archived. No new replies allowed.