View Single Post
Join Date: Oct 2006
Posts: 2,875
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: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Cenosillicaphobiac

Re: Line count not working....

 
0
  #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