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.
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.
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
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
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.
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.
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.
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.
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.