| | |
Line count not working....
Thread Solved
![]() |
hey,
This might be naive, but how come this bit of code isn't working???
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.
This might be naive, but how come this bit of code isn't working???
C++ Syntax (Toggle Plain Text)
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.
"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."
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."
You should check that the file actually opened.
Are you sure the data file is in the place where your program is looking for it?
C++ Syntax (Toggle Plain Text)
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?
"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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
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:
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?
C++ Syntax (Toggle Plain Text)
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?
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."
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."
•
•
•
•
C++ Syntax (Toggle Plain Text)
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;
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)
#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
Last edited by niek_e; Sep 4th, 2008 at 5:44 am.
•
•
•
•
Just out of curiosity: You do know that void main() doesn't exist right? I'm referring to your signature...
•
•
•
•
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?
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."
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."
"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."
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."
•
•
•
•
Why even bother reading the signature?(although you got to admit, some are pretty funny)
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?
![]() |
Similar Threads
- cin.getline not working in for loop or...? (C++)
- C Program where gets(), getchar(), are not working. (C)
- annoying error when closing working program (C)
- File operations (C)
- Elapse Time Since Program Start Working (C++)
- MySQL syntax error check for the right syntax to use near 'A (A) )' at line 1 (PHP)
- Cubic Numbers (C++)
Other Threads in the C++ Forum
- Previous Thread: about 'CryptDestroyKey' function!
- Next Thread: urgent help
| Thread Tools | Search this Thread |
api application array based binary bitmap c# c++ c/c++ char class classes code coding compile compression console conversion count cpm delete deploy deque desktop developer dialog directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer introductory java lib linkedlist linkednodes linker loop looping loops map math matrix memory multiple news node numbertoword output parameter pointer problem program programming project python random read recursion reference rpg security sorting string strings temperature template test text text-file tree url variable vector video whyisthiscodecausingsegmentationfault win32 windows winsock wordfrequency wxwidgets






