Line Count Programming Challenge

Pages: 12
Why would a professor force a student to solve such an assignment whereas the student doesn't even know (or have ever learned) what a "command line" is?


Professors usually give assignments that involve the material that was in the Lectures / Tutorials. There might be reasons why students had forgotten various things. But really it's basic knowledge in this case. Imagine someone doing 1st year calculus, and not knowing what trig functions were.
@TheIdeasMan
Correction.
"have ever learned" should be "has ever learned"
Guys. This is not an assignment. I simply found this problem while surfing the net and decided to solve it on my own. Unfortunately, I don't know where to start. I would like to write the code on my own with as little help as possible, but for this case I really don't know what should I read to be able to do it on my own. If you guys know what might help me - please share your thoughts. Thanks.
@aurimas13
Have you seen my example (see Page 1)?
@TheIdeasMan
> Guys. This is not an assignment. I simply found this problem while surfing the net and decided to solve it on my own.

This. Problem cleared.
@closed account

Yeah, I have seen it. It solves the assignment, but now I am trying to understand it. Thanks.
closed account (48T7M4Gy)
Let's cut to the chase:
1) Here is the question: http://www.cprogramming.com/challenges/count.html
2) Here is the solution: http://www.cprogramming.com/challenges/countsol.html

Any questions?
@kemort

Haven't seen that solution. Thanks.
closed account (48T7M4Gy)
Excellent. If you have any problems with it get back.

When you get count.exe you run it by typing count xyz.txt at the prompt xyz.txt is the name of the file you're applying the count to. You'll need to use quotes around the file name and it must be in the same directory as the .exe file
Last edited on
And let us know if you have any further questions :)
I have a couple questions when I want to run the code in terminal. When I set argc != 2 I get Error. Input should be of the form 'count filename.txt' , while when I set it to argc != 1 I get Segmenatation fault: 11 . What is happening? Why I cannot get my program to calculate the total lines of the file? Why argc != 2 throw an Error and not proceed with the code? Thanks.

The code is this:

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
  if (argc != 2) {cout << "Error. Input should be of the form 'count filename.txt' " << endl; return 1;}
  else {
    ifstream the_file(argv[1]);
    if(!the_file)
    {
      cout << "File " << argv[1] << " does not exist";
      return 0;
    }
    char c;
    int count = 0;
    while(the_file.get(c))
    {
      if(c == '\n')
      {
        count++;
      }
    }
    cout << "Total lines of file: " << count;
  }
  return 0;
}
Last edited on
If command line is really that tricky & problematic, use mine. Does my solution work well?

Anyway, in order to continue to work with command line the safer way, one should use some safer method. I recommend you use the Windows API function GetCommandLine() from the header file <windows.h>. And you can surely safely start from there.
@closed account

Your way succeeded, but I wish to understand why mine is not working properly..
How are you invoking the program on the command line?
I'm fairly impressed by much of the random (and bad) advice offered in this thread. OP still does not understand his problem.

Command-line arguments 
A quick intro to command line arguments:
http://www.cplusplus.com/articles/DEN36Up4/
http://www.cplusplus.com/forum/beginner/5404/#msg23839

argv[0] is always the name of your program (on all systems that I am aware of).
argv[1] is the first string given to your program -- which would be "filename.txt", given your example on the first page.

    count     filename.txt
    ↑         ↑
    argv[0]   argv[1]

Another example:

    count.exe count.cpp
    ↑         ↑
    argv[0]   argv[1]

With that, you can know how to open the file (argv[1]).

Counting lines 
The easiest way to count the number of lines is to use either std::getline() or ifstream::ignore() until the ifstream fails, counting the number of times through the loop.

??? 
closed account 5a8Ym39o6 wrote:
@TheIdeasMan
Correction.
"have ever learned" should be "has ever learned"

Er, no, TheIdeasMan's English is correct. But why are you seeking to correct him anyway? People constantly mis-type on the internet. You've done it yourself; no one felt it necessary to redress it, because it's a small, unimportant thing.

closed account (48T7M4Gy)
You'll need to use quotes around the file name and it must be in the same directory as the .exe file
No quotes required.
No, I haven't. I simply compile the code by g++ and then ask for the output in the terminal with ./a.out. That might be the problem?
closed account (48T7M4Gy)
I think your problem arises because you need to run the program via the Terminal, shell, console or similar. What system are you running it on because if it's a pc or Mac all you need to do is put the exe file and test text together in the same directory then, provided the program is called count.exe all you do is type count xyz.txt and press return.

closed account (48T7M4Gy)
http://www.cplusplus.com/forum/beginner/194740/

I can fully understand why you would want to abandon this thread, but how about closing it off with a green tick aurimas13 ? It helps to prevent wasting people's time.
Topic archived. No new replies allowed.
Pages: 12