I am trying to read in a date from a data file that looks osmething like this
10/ 2 3 / 20 0 4
I want my variables to be month = 10, day = 23, and year = 2004
Right now, it is only reading the last two numbers..so month and day are fine but year I am getting 04. Can anybody see what I'm doing wrong?
I haven't included my main function but it just opens my data file.
Code:
void process_text(istream& in_file, ofstream& out_stream)
{
char next;
int new_next;
int last_digit = 0;
int month;
int day;
int year;
in_file.get(next);
while(next != '/')
{
if(isdigit(next))
{
new_next = convert_digit(next);
month = total_number(new_next, last_digit);
last_digit = convert_digit(next);
in_file.get(next);
}
else
in_file.get(next);
}
last_digit = 0;
in_file.get(next);
while(next != '/')
{
if(isdigit(next))
{
new_next = convert_digit(next);
day = total_number(new_next, last_digit);
last_digit = convert_digit(next);
in_file.get(next);
}
else
in_file.get(next);
}
last_digit = 0;
in_file.get(next);
while(next != '/' && next != '\n')
{
if(isdigit(next))
{
new_next = convert_digit(next);
year = total_number(new_next, last_digit);
last_digit = convert_digit(next);
in_file.get(next);
}
else
in_file.get(next);
}
cout << "the month is " << month << endl << endl;
cout << "the day is " << day << endl << endl;
cout << "the year is " << year << endl << endl;
}
int convert_digit(char& number)
{
int new_number = static_cast<int>(number) - 48;
return(new_number);
}
int total_number(int tot_num, int last_digit_par)
{
return((last_digit_par * 10) + tot_num);
}