954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Line count not working....

hey,

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

int lc=0;

read.open(filename_user.c_str(), ios::in);
while(getline(read, linecount, '\n'))
{
                     ++lc;
                     }


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.

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

You should check that the file actually opened.

read.open(filename_user.c_str(), ios::in);
if( ! read )
{
  //error handler here
}
else
     while(getline(read, linecount, '\n'))
     {
          ++lc;
     }


Are you sure the data file is in the place where your program is looking for it?

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

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:

unsigned int lc=0;
     reDo:
     cout << "\n\n\n\n\tUser, please Enter the filename (with directory) below:\n\n\t\t::==:: ";
     getline(cin, filename_user); 
     
     ifstream read(filename_user.c_str());
     
     if(read.fail())
     {
                    cout << "\n\n\t";
                    error_slashes();
                    cerr << "\n\n\t   Sorry, but this program was unable to locate/read\n\n\t\t\tthe given file!\n\n\t";
                    error_slashes();
                    
                    cin.get();
                    cout << "\n\n\t\tDo you wish to open a new file? (y/n[quit]) ";
                    cin >> choice;
                    
                    if(choice=='y')
                    {
                                   goto reDo;
                                   }
                                   else
                                   {
                                       exit(1);
                                       }
                    }
     read.open(filename_user.c_str(), ios::in);
     while(getline(read, linecount, '\n'))
     {
                         ++lc;
                         }
                         read.clear();//clear memory
                         read.close();
                         read.open(filename_user.c_str(), ios::in);
                         while(getline(read, lines, '\n'))
                         {
                                             cout << lines << "\n\n\n";
                                             }
                         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?

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

Why don't you just create a simple linecount program to see where you're going wrong?

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 
read.open(filename_user.c_str(), ios::in);
    while(getline(read, linecount, '\n'))
    {
        ++lc;
    }
    read.clear();//clear memory
    read.close();
    read.open(filename_user.c_str(), ios::in);
    while(getline(read, lines, '\n'))
    {
        cout << lines << "\n\n\n";
    }
    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.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string buff;
    int count = 0;
    ifstream input("c:\\input.txt"); //or something
    if (!input.is_open()) return 1; // error handling goes here
    while (getline(input, buff, '\n')) count++;
    cout << "file has " << count << " lines";
    return 0;
}


Just out of curiosity: Youdo 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

Attachments goto.png 25.65KB
Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
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! :twisted:

raul15791
Junior Poster
102 posts since Jun 2008
Reputation Points: 37
Solved Threads: 7
 
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! :twisted:

Yes, I am aware that void main doesn't exist as a standard, although this does work in older c++ compilers, like the ones Iused to use...

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

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

Why even bother reading the signature?(although you got to admit, some are pretty funny)

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

I quite agree to that myself....

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 
Why even bother reading the signature?(although you got to admit, some are pretty funny)
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?

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Hmm, just a thought .. how about changing one line of the signature to:

cout << "Hi there!" << "I'm a non-standard-compliant C++ coder!";


;)

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

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?

And you're correct as well, but I never use void main() now-a-days, although I have some really old compilers on my computer that accept void main() , so I used it when I was learning basic c++. And the only reason its like that in my signature is because you're allowed only 5 lines, and a return statement will make 6...

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

Hmm, just a thought .. how about changing one line of the signature to:

cout << "Hi there!" << "I'm a non-standard-compliant C++ coder!";

Haha :Dand a return statement will make 6...
I'll give you the benefit of the doubt ;)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 

Haha :D

I'll give you the benefit of the doubt ;)

lol...okay!

amrith92
Junior Poster
188 posts since Jul 2008
Reputation Points: 130
Solved Threads: 23
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You