DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Line count not working.... (http://www.daniweb.com/forums/thread143828.html)

amrith92 Sep 3rd, 2008 3:35 pm
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.

vmanes Sep 3rd, 2008 5:39 pm
Re: Line count not working....
 
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?

amrith92 Sep 4th, 2008 4:33 am
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:


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?

iamthwee Sep 4th, 2008 5:28 am
Re: Line count not working....
 
Why don't you just create a simple linecount program to see where you're going wrong?

niek_e Sep 4th, 2008 5:43 am
Re: Line count not working....
 
1 Attachment(s)
Quote:

Originally Posted by amrith92 (Post 684102)
    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: 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

raul15791 Sep 4th, 2008 6:10 am
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! :twisted:

amrith92 Sep 4th, 2008 6:27 am
Re: Line count not working....
 
Quote:

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

Quote:

Originally Posted by raul15791 (Post 684154)
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 I used to use...


Quote:

Originally Posted by niek_e (Post 684142)
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!!

Dannyo329 Sep 4th, 2008 6:27 am
Re: Line count not working....
 
Why even bother reading the signature?(although you got to admit, some are pretty funny)

amrith92 Sep 4th, 2008 6:35 am
Re: Line count not working....
 
Quote:

Originally Posted by Dannyo329 (Post 684171)
Why even bother reading the signature?(although you got to admit, some are pretty funny)

I quite agree to that myself....

niek_e Sep 4th, 2008 6:39 am
Re: Line count not working....
 
Quote:

Originally Posted by Dannyo329 (Post 684171)
Why even bother reading the signature?(although you got to admit, some are pretty funny)

Quote:

Originally Posted by amrith92 (Post 684179)
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?


All times are GMT -4. The time now is 9:38 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC