By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,903 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,903 IT Pros & Developers. It's quick & easy.

C++ Help with displaying code from file

P: n/a
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.

Oct 25 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
no*****@gmail.com wrote:
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
Oct 25 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.