Hey everyone, I'm new to posting in these forums so sorry if I didn't post my code correctly? I think i did it...hopefully.
Anyways,
I've been working on this all day but I can't figure it out. (i think i will be having problems with the rest of my assignments in this chapter =( ...sigh)
What I need to do:
- Ask user what file to open
- Display contents of file
----display 24 lines, user will strike any key to continue with the next 24 lines
So my code compiles, but it shows me character by character
Please help me with how to display every 24 lines!!
I can't for the life of me figure it out and i've seriously tried all day.
I've read my book and still can't figure it out.
Hints given to me, but i still don't know where to put it / what to put/ how to do it:
- Use an if statement to check if the character is '\n'. If so, that is a new line and can add 1 to the line counter.
- To pause after 24 lines, you can use an integer constant for # of lines per page and use cin.getline to get enter key.
example code for this:
char buff[160];
cout << "\n"
<< "(more) ";
cin.getline( buff, sizeof buff );
So here's what I have right now =/
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int SIZE = 51; // Array size for file name
char fileName[SIZE]; // Hold file name
char ch; // Hold a character
fstream file; // File stream
// Get file name from user
cout << " Enter the file name you wish to open and read: ";
cin.getline(fileName, SIZE);
// Open the file
file.open(fileName, ios::in);
if (!file)
{
cout << fileName << " could not be opened.\n";
return 0;
}
// Get each character from file and display
char buff[160];
file.get(ch);
while ( !file.eof() )
{
cout << ch;
file.get(ch);
cout << "\n"
<< "(more) ";
cin.getline( buff, sizeof buff );
}
// Close the file.
file.close();
return 0;
}