C++ Help with displaying code from file

N

norwich

Hi,

I'm trying to display customer details on screen from a file using
functions.

The details are written to the file first then I display them on screen
after I search for the file.

My problem is this:

I'm trying to display the details individually using this function for
void display();

All the other code is written I just need to get this function to run.

===============================================

void display()
{

char filename[15];
char in_char,ans;
int line;

do
{
clrscr();
cout<<"\nTo Display Contractor Details on the Screen";
cout<<"\nType the file name you want to see";
cout<<"\n\nEG. F:YOUR_FILE_NAME.txt :";
cin>>filename;

infile.open(filename, ios::in);


if(!infile)
{
clrscr();
cout<<"\n\n****That file does not exist****\n";
cout<<"Type F:\ and then up to eight letters for the file name,"
<<" then a dot and then three characters for the file type";
cout<<"\n\nEXAMPLE F:\THISFILE.TXT";
cout<<"\n\nDo you want to QUIT (Y/N)";
cin>>ans;
ans=toupper(ans);
}

}while((!infile)&&(ans!='Y'));

while(infile.get(in_char))


if (line>20)
{

cout<<(in_char);
cout<<"\n\nThat is the screen full...\n"
<<"\npress any key to clear it and continue...";
getch();
clrscr();

}


infile.close();

return;
}

===============================================

When I run this code it displays each character one at a time and not
the details.

The details are about 10 lines long and I need the program to say
"screen full" so that when I click continue the next set of details
comes up...

I been looking and trying everything for 3 hours now and I must be
missing something... can anyone point me in the right direction.

Thanks for your time.
 
J

John Harrison

Hi,

I'm trying to display customer details on screen from a file using
functions.

The details are written to the file first then I display them on screen
after I search for the file.

My problem is this:

I'm trying to display the details individually using this function for
void display();

All the other code is written I just need to get this function to run.

===============================================

void display()
{

char filename[15];
char in_char,ans;
int line;

do
{
clrscr();
cout<<"\nTo Display Contractor Details on the Screen";
cout<<"\nType the file name you want to see";
cout<<"\n\nEG. F:YOUR_FILE_NAME.txt :";
cin>>filename;

infile.open(filename, ios::in);


if(!infile)
{
clrscr();
cout<<"\n\n****That file does not exist****\n";
cout<<"Type F:\ and then up to eight letters for the file name,"
<<" then a dot and then three characters for the file type";
cout<<"\n\nEXAMPLE F:\THISFILE.TXT";
cout<<"\n\nDo you want to QUIT (Y/N)";
cin>>ans;
ans=toupper(ans);
}

}while((!infile)&&(ans!='Y'));

while(infile.get(in_char))


if (line>20)
{

cout<<(in_char);
cout<<"\n\nThat is the screen full...\n"
<<"\npress any key to clear it and continue...";
getch();
clrscr();

}


infile.close();

return;
}

===============================================

When I run this code it displays each character one at a time and not
the details.

The details are about 10 lines long and I need the program to say
"screen full" so that when I click continue the next set of details
comes up...

I been looking and trying everything for 3 hours now and I must be
missing something... can anyone point me in the right direction.

Thanks for your time.

Look at your variable line. Where do you give it a value? Answer,
nowhere. How does you problem say you need to read the file? Answer,
line by line. How do you read the file? Answer, character by character.

I don't know what you mean when you say that you must be missing
something, the problem is that your code bears no relation to the
problem you are trying to solve.

Here's a better way

int line_count = 0;
while (read a line from file)
{
display line;
++line_count;
if (line_count%20 == 0)
display 'press any key to continue';
}

Something like that anyway, you can fill in the gaps.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top