943,781 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1466
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 3rd, 2008
1

Line count not working....

Expand Post »
hey,

This might be naive, but how come this bit of code isn't working???

C++ Syntax (Toggle Plain Text)
  1. int lc=0;
  2.  
  3. read.open(filename_user.c_str(), ios::in);
  4. while(getline(read, linecount, '\n'))
  5. {
  6. ++lc;
  7. }

Have looked through the internet, and this code seems to be fine, so what is it that makes it not work??

the output is always 0, no matter what file I try to open.
Similar Threads
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Sep 3rd, 2008
0

Re: Line count not working....

You should check that the file actually opened.

C++ Syntax (Toggle Plain Text)
  1. read.open(filename_user.c_str(), ios::in);
  2. if( ! read )
  3. {
  4. //error handler here
  5. }
  6. else
  7. while(getline(read, linecount, '\n'))
  8. {
  9. ++lc;
  10. }

Are you sure the data file is in the place where your program is looking for it?
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 4th, 2008
0

Re: Line count not working....

Actually, it does check whether the file is open and it does also show the contents of the file...The only thing that doesn't work is the line count I made. But I see what you mean: Below is the whole code:

C++ Syntax (Toggle Plain Text)
  1.  
  2. unsigned int lc=0;
  3. reDo:
  4. cout << "\n\n\n\n\tUser, please Enter the filename (with directory) below:\n\n\t\t::==:: ";
  5. getline(cin, filename_user);
  6.  
  7. ifstream read(filename_user.c_str());
  8.  
  9. if(read.fail())
  10. {
  11. cout << "\n\n\t";
  12. error_slashes();
  13. cerr << "\n\n\t Sorry, but this program was unable to locate/read\n\n\t\t\tthe given file!\n\n\t";
  14. error_slashes();
  15.  
  16. cin.get();
  17. cout << "\n\n\t\tDo you wish to open a new file? (y/n[quit]) ";
  18. cin >> choice;
  19.  
  20. if(choice=='y')
  21. {
  22. goto reDo;
  23. }
  24. else
  25. {
  26. exit(1);
  27. }
  28. }
  29. read.open(filename_user.c_str(), ios::in);
  30. while(getline(read, linecount, '\n'))
  31. {
  32. ++lc;
  33. }
  34. read.clear();//clear memory
  35. read.close();
  36. read.open(filename_user.c_str(), ios::in);
  37. while(getline(read, lines, '\n'))
  38. {
  39. cout << lines << "\n\n\n";
  40. }
  41. cout << "\n\n\t\t" << lc;

In the code, it checks for the existence of the file before opening...and it does output the file contents...Only thing not working is the line count.

Is the method correct?
Last edited by amrith92; Sep 4th, 2008 at 4:36 am.
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Sep 4th, 2008
0

Re: Line count not working....

Why don't you just create a simple linecount program to see where you're going wrong?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 4th, 2008
1

Re: Line count not working....

Click to Expand / Collapse  Quote originally posted by amrith92 ...
C++ Syntax (Toggle Plain Text)
  1. read.open(filename_user.c_str(), ios::in);
  2. while(getline(read, linecount, '\n'))
  3. {
  4. ++lc;
  5. }
  6. read.clear();//clear memory
  7. read.close();
  8. read.open(filename_user.c_str(), ios::in);
  9. while(getline(read, lines, '\n'))
  10. {
  11. cout << lines << "\n\n\n";
  12. }
  13. cout << "\n\n\t\t" << lc;
If this code were to work at all, it would display the cout line as many times as there are newlines in the file. I suppose that wasn't your intent?

There are a few other things wrong with your program, but I guess this isn't all of it?

Anyway, here's a small example for linecounting.
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string buff;
  10. int count = 0;
  11. ifstream input("c:\\input.txt"); //or something
  12. if (!input.is_open()) return 1; // error handling goes here
  13. while (getline(input, buff, '\n')) count++;
  14. cout << "file has " << count << " lines";
  15. return 0;
  16. }

Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...

Also: Using goto is considered bad coding-practice. 99.9% of the cases (including yours) the better answer would be to use a loop. See attachment

ps. You could also have a look at indenting code
Attached Thumbnails
Click image for larger version

Name:	goto.png
Views:	18
Size:	25.6 KB
ID:	7279  
Last edited by Nick Evan; Sep 4th, 2008 at 5:44 am.
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Sep 4th, 2008
0

Re: Line count not working....

Quote ...
Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...
Good... Even signature is checked!
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008
Sep 4th, 2008
0

Re: Line count not working....

Click to Expand / Collapse  Quote originally posted by niek_e ...
Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...
Click to Expand / Collapse  Quote originally posted by raul15791 ...
Good... Even signature is checked!
Yes, I am aware that void main doesn't exist as a standard, although this does work in older c++ compilers, like the ones I used to use...


Click to Expand / Collapse  Quote originally posted by niek_e ...
If this code were to work at all, it would display the cout line as many times as there are newlines in the file. I suppose that wasn't your intent?
There are a few other things wrong with your program, but I guess this isn't all of it?
Well, the code does do this, and the only reason this exists is to check that the program can actually render the file properly...and it does, it keeps printing the line as many times as it encounter '\n' delimiter.
And you're correct, This is just a part of a bigger program. I'll try your code out and will try and modify my own...

Thanks for all the replies...

[EDIT]Thanks for your code and advice, 'niek_e', it helped such a lot! The code's finally working!!
Last edited by amrith92; Sep 4th, 2008 at 6:44 am. Reason: tested code
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Sep 4th, 2008
0

Re: Line count not working....

Why even bother reading the signature?(although you got to admit, some are pretty funny)
Reputation Points: 20
Solved Threads: 8
Junior Poster in Training
Dannyo329 is offline Offline
79 posts
since Apr 2008
Sep 4th, 2008
0

Re: Line count not working....

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
Why even bother reading the signature?(although you got to admit, some are pretty funny)
I quite agree to that myself....
Reputation Points: 130
Solved Threads: 22
Junior Poster
amrith92 is offline Offline
187 posts
since Jul 2008
Sep 4th, 2008
0

Re: Line count not working....

Click to Expand / Collapse  Quote originally posted by Dannyo329 ...
Why even bother reading the signature?(although you got to admit, some are pretty funny)
Click to Expand / Collapse  Quote originally posted by amrith92 ...
I quite agree to that myself....
I'm trying to help people with C++. void main() isn't standard C++, so every time I encouter it, I mention it to the poster. That way, when/if the poster gets a job in the programming-field, he/she won't get laughed out of the building by using void main() .

What difference does it make if I see it in the post or in the signature?
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: about 'CryptDestroyKey' function!
Next Thread in C++ Forum Timeline: urgent help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC