I'm working on a program from a book exercise that prints the lines of a text document. It works fine, except when it gets to the part where it prompts the user for more lines or to exit. It doesn't matter what you enter for the number of lines, it prints 24 lines (the default amount). I have a variable that stores the users entered lines and it's in the condition of the for loop that prints the lines, but it doesn't listen to what the variable is. The only reason I can think is maybe the variable isn't being changed correctly. If so, why is that?
#include <iostream>
#include <fstream>
usingnamespace std;
#define COL_WIDTH 80
int main() {
int c = 24;
int i;
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 <= c && ! file_in.eof(); i++) {
file_in.getline(input_line, COL_WIDTH);
cout << input_line << endl;
}
if (file_in.eof())
break;
cout << "How may more lines? (Press 'Q' and ENTER to quit)";
cin.getline(input_line, COL_WIDTH);
if (c == 'Q' || c == 'q')
break;
}
c = atoi(input_line);
system("PAUSE");
return 0;
}
Also, why aren't you using C++ strings? They're a lot simpler to use than C strings
I know, and the answer to that is I'm learning C++ right now. I know C strings are harder, but it's better to learn it now than to run into a problem later down the road when I need to use them for some reason and have to learn it then. Also, this is an exercise from a book. Where should I put line 31 then? That's why it's just kind of out there, I don't know where it should go. Before the if that checks c for Q? And, if I were to use a comparison using input_line it would just be
if (input_line[0] == "Q" || input_line[0]== "q")
Is that right? I've only been learning C++ for a weak so you have to bear with me. xD Thanks for help Albartross