File I/O Not Opening Input File

[Note: This was from a homework assignment, but I believe the code is all correct aside from this problem. A helpful hint or someone pointing out that I was mistaken in that assumption would really be appreciated).

This is a 24-hour to 12-hour conversion assignment emphasizing the use of file input (strings, EOF, etc.). All the separate parts/functions are correct; the only way I could see this not working is that the program is refusing to open the input file I had made for it. I do not know why that is happening and would appreciate any pointers. Here is my code below:

-
#include <iostream>
#include <fstream>
using namespace std;
int readtime(ifstream& infile, int& hours, int& mins);
char convert(int& hours);
void showtime (ofstream& outfile, int hours, int mins, char AorP, int orighours);
void main(void)

{
ofstream outfile;
ifstream infile;
int hours, mins;
char AorP;
char filename[100];
char ans;
int eof;
int hours2;
ans='y';

cout <<"Daisy Bilenkin"<<endl;
cout <<"CSCI-116-01"<<endl;
cout <<"Program 5"<<endl;
cout <<"October 23rd, 2012"<<endl;
cout <<endl;
cout <<endl;



while ((ans=='Y')||(ans=='y'))
{
eof = 1;

cout<<"What file would you like to read in?"<<endl;
cin>>filename;

while (eof == 1)

{
infile.open(filename);
if (infile.fail())
{
cout<< "FAIL."<< endl;
eof=0;
}

eof=readtime(infile, hours, mins);

hours2 = hours;

AorP = convert (hours);

outfile.open ("report.txt");

showtime (outfile, hours, mins, AorP, hours2);
}

infile.close();
outfile.close();

cout<<"Would you like to input another file?"<<endl;
cin>>ans;
}
return;
}

int readtime(ifstream& infile, int& hours, int& mins)

{
char colon;

infile>>hours;
infile>>colon;
infile>>mins;

if (infile.eof())
return 0;
else
return 1;
}

char convert (int &hours)

{
if (hours > 12)
{
hours = hours - 12;
return 'P';
}
else if (hours == 12)
return 'P';

else if (hours == 0)
{
hours = 12;
return 'A';
}

else
return 'A';

}

void showtime (ofstream& outfile, int hours, int mins, char AorP, int orighours)

{
cout<<"Original time inputted: "<<orighours<<":";
outfile<<"Original time inputted: "<<orighours<<":";
if(mins > 9)
{
cout<<mins<<AorP<<"M"<<endl;
outfile<<mins<<AorP<<"M"<<endl;
}
else
{
cout<<'0'<<mins<<AorP<<"M"<<endl;
outfile<<'0'<<mins<<AorP<<"M"<<endl;
}

cout<<"Converted 12-hour time: "<<hours<<":";
outfile<<"Converted 12-hour time: "<<hours<<":";
if(mins > 9)
{
cout<<mins<<AorP<<"M"<<endl;
outfile<<mins<<AorP<<"M"<<endl;
}
else
{
cout<<'0'<<mins<<AorP<<"M"<<endl;
outfile<<'0'<<mins<<AorP<<"M"<<endl;
}

return;
}

-
Topic archived. No new replies allowed.