Q:

How to read a file line by line without using the string class?

So in my c++ class, we're not allowed to use the string class right now. To substitute strings, we're using character arrays.
The assignment requires that i read from a file that contains a sentence on each line. The first line of the file is an integer that tells how many lines are in the file. 
My first problem is that reading in the integer into a variable and using that variable in an array causes an error saying that the variable must be constant.
How can I get past that? I need a 2D array to count how many characters each sentence has. I want to initialize my array as char FileSentences[numberOfLines][81]. It's been decided that the sentence character cap will be 80 characters long, so the width of each row has to be 81 to account for the \0.
My second problem comes from how I'm reading in the integer. Since the first line in the file is an integer, I'm reading it like:
int numberOfLines;
ifstream fin;
fin.open("TestTextFile.txt");
fin >> numberOfLines;

This works and it sets the variable numberOfLines to the correct value. However, when I call fin.getline, the next thing it will read in is a blank. If I call fin.getline again, it is the first sentence of the file. Is there another way to read in the integer to prevent that or should I just set a blank sentence in memory to hold the first fin.getline value and then proceed to reading sentences into my array?

A:

For the first problem, use a std::vector.
// Define a typedef for a line.
typedef char Line[81];

// Read the number of lines.
fin >> numberOfLines;

// Define a vector for the lines.
std::vector<Line> lines(numberOfLines);

For the second problem, use ifstream::ignore() to ignore the rest of the line.
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

Make sure to add
#include <limits>

to use std::numeric_limits.

