I'm having a problem.

Here is the example code from one of the books I'm learning from:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

#define COL_WIDTH 80

int main() {
    int c;   // input character
    int i;   // loop counter
    char filename[MAX_PATH + 1];
    char input_line[COL_WIDTH + 1];
    cout << "Enter a file name and press ENTER: ";
    cin.getline(filename, MAX_PATH);
    ifstream file_in(filename);
    if (! file_in) {
        cout << filename << " could not be opened.";
        cout << endl;
        system("PAUSE");
        return -1;
    }
    while (true) {
        for (i = 1; i <= 24 && ! file_in.eof(); i++) {
            file_in.getline(input_line, COL_WIDTH);
            cout << input_line << endl;
        }
        if (file_in.eof())
            break;
        cout << "More? (Press 'Q' and ENTER to quit)";
        cin.getline(input_line, COL_WIDTH);
        c = input_line[0];
        if (c == 'Q' || c == 'q')
            break;
        }
    system("PAUSE");
    return 0;
}


It displays a text file, 24 lines at a time.

The exercise I was given involves changing the "More? (Press 'Q' and ENTER to quit)" prompt to also accept a number and if that number is above zero read that many lines instead of 24.

I am quite uncertain how to do this. What I think needs doing is to add another if statement after one that checks for 'Q', but this time check to see if the string is a number and if it is change the variable readNlines. I would replace the value 24 with the int variable readNlines.

The problem is I don't know how to check if input_line is a number or not.
WOW, I think this book may be badly designed. I assumed it would be possible to just use what the book has given me so far but there is a lot in that function you posted that I don't understand. I think it might be best to just skip this particular exercise.

Thanks for the help though, it's always appreciated.
Topic archived. No new replies allowed.