Counting the number of characters and lines in a given text file

So far this is my code. It needs to take from a text time (textfile1), read it, and output how many characters and lines it has. But right now all it does it take the textfile1.txt and spit it back out at me. What did I do wrong and where do I go from here?

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
 #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {

  int number_of_lines = 0, file1=1;
    int number_of_letters;
    string lines;

    ifstream myfile;
      myfile.open ("testfile1.txt");


while (myfile.good ())
{
  getline(myfile, lines);
    cout << lines << endl;

  if (lines==" ")
  {
    cout << "Number of lines:" << file1 << number_of_lines << endl;
    number_of_lines=0;
    file1++;
  }
  else
  {
    number_of_lines;
  }
}


            return 0;
}
You read one line at a time. Every time you read a line, the number_of_lines should increase by one (read: you must increase it by one).

Every line that you have read has some number of characters. You must add them to the running sum of number_of_letters.

Both number_of_letters and number_of_lines must be 0 at start. Make it so.

Both number_of_letters and number_of_lines should contain the correct answers after reading of the file has completed.
Topic archived. No new replies allowed.