Line count not working....

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 135
Reputation: amrith92 is on a distinguished road 
Solved Threads: 13
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Line count not working....

 
1
  #1
Sep 3rd, 2008
hey,

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

  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,675
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Line count not working....

 
0
  #2
Sep 3rd, 2008
You should check that the file actually opened.

  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?
"We Americans got so tired of being thought of as dumb by the rest of the world that we went to the polls last November and removed all doubt."
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 135
Reputation: amrith92 is on a distinguished road 
Solved Threads: 13
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Line count not working....

 
0
  #3
Sep 4th, 2008
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:

  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.
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Line count not working....

 
0
  #4
Sep 4th, 2008
Why don't you just create a simple linecount program to see where you're going wrong?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,833
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Line count not working....

 
1
  #5
Sep 4th, 2008
Originally Posted by amrith92 View Post
  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.
  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
Last edited by niek_e; Sep 4th, 2008 at 5:44 am.
Attached Thumbnails
goto.png  
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Line count not working....

 
0
  #6
Sep 4th, 2008
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 135
Reputation: amrith92 is on a distinguished road 
Solved Threads: 13
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Line count not working....

 
0
  #7
Sep 4th, 2008
Originally Posted by niek_e View Post
Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...
Originally Posted by raul15791 View Post
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...


Originally Posted by niek_e View Post
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
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 59
Reputation: Dannyo329 is an unknown quantity at this point 
Solved Threads: 6
Dannyo329's Avatar
Dannyo329 Dannyo329 is offline Offline
Junior Poster in Training

Re: Line count not working....

 
0
  #8
Sep 4th, 2008
Why even bother reading the signature?(although you got to admit, some are pretty funny)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 135
Reputation: amrith92 is on a distinguished road 
Solved Threads: 13
amrith92's Avatar
amrith92 amrith92 is offline Offline
Junior Poster

Re: Line count not working....

 
0
  #9
Sep 4th, 2008
Originally Posted by Dannyo329 View Post
Why even bother reading the signature?(although you got to admit, some are pretty funny)
I quite agree to that myself....
"C++ : Where friends have access to your private members."
C++: You accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical assistance is impossible since you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,833
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is online now Online
Roasting Maven

Re: Line count not working....

 
0
  #10
Sep 4th, 2008
Originally Posted by Dannyo329 View Post
Why even bother reading the signature?(although you got to admit, some are pretty funny)
Originally Posted by amrith92 View Post
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?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC