| | |
code seems to stop executing
Thread Solved
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
hi again people, im working on a simple program to look into a text file and output details about it. It seems to work fine for the first part but at the end of a while loop the code seems to stop working, i have had a little play about with it moving the code below the error into different places and i have found that there seems to be a problem with that little piece, i will explain as i go along
i dont know whether i am right but i think the problem lies within this section of code
I have tried to comment it as much as i can, but a lot of teh comments on there are to help me as i am going along. Any ideas why i am having this problem ?
I am using winXP and Dev-Cpp compiler
C++ Syntax (Toggle Plain Text)
/*This program is to find out statistics of a specified file*/ #include<fstream.h> #include<iostream.h> #include<stdlib.h> #include<conio.h> int characternumber,arraylength,nextcharacter, numberofcharacters,totalnumberofcharacters,numberoflines; char filename[100]; char linebuffer[100]; char characters[100]="abcdefghijklmnopqrstuvwxyz0123456789[];'#,./<>?:@~{}!\"£$%^&*()_+=-\\`¬|"; int main () { cout<<"Enter the filename: "; //get user input cin.getline(filename,100); ifstream file1(filename); //check to see if file exists if (!file1.is_open()) //if file doesnt exist clear screen and restart { cout<<endl<<endl<<"file does not exist\n\n"; system("PAUSE"); system("CLS"); main(); } file1.close(); arraylength = (strlen(characters)-1); //used in main loop to find # of characters**needs to be length - 1 or else it shows the number of NULL chars too characternumber = 0; //reinitialize the variables numberofcharacters = 0; totalnumberofcharacters = 0; numberoflines = 0; /*this section finds out how many of each character is in the file*/ while (characternumber <= arraylength) //main loop do this for each character { ifstream file(filename); while (!file.eof()) //2nd loop, goes through file 1ce for each character { nextcharacter = file.get(); if (nextcharacter == characters[characternumber]) { numberofcharacters++; } else { if (characternumber == 0) //makes sure it calculates total # of chars 1nce { totalnumberofcharacters++; } } } cout<<characters[characternumber]<<" = "<<numberofcharacters<<"\t"; //output results in columns numberofcharacters = 0; //re-initialize this variable for next character characternumber++; //get next character file.close(); //close file as it is re-opened at beginning of main loop } cout<<endl<<endl<<"Total number of Characters: "<<totalnumberofcharacters; //output total number of chars only 1ce //number of lines...code stops excecuting here !!!*********************** ifstream file(filename); //open file while (!file.eof()) //loop till eof() is reached { cin.getline(linebuffer,100); //count how many times cin.getline() numberoflines++; //needs to be used before eof() } file.close(); cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result /*extra features are needed like number of lines: how many times do ya need to do cin.getline() before eof(), filesize: need to look this up wordcount: if character == space or newline and character after that is not == a space or newline then tht is 1 word *****need lots of extra features****** */ system(filename); //open file for comparing - testing getch(); //keep on screen /* take user input on a file they want to check use arraylength as product of loop with incremental characternumber that starts at 0 while (characternumber <= arraylength) take 1st char from array and then use that to compare use a loop to get next char from file and compare if the character matchs the 1 brought form the array increment the numberofcharacters when eof if reached break from loop, output number of chars and add 1 to characternumber and go through the loop again do this till all characters in the array have been checked and outputted next section to count the total number of chars and estimated file size then get actual file size */ }
C++ Syntax (Toggle Plain Text)
//number of lines...code stops excecuting here !!!*********************** ifstream file(filename); //open file while (!file.eof()) //loop till eof() is reached { cin.getline(linebuffer,100); //count how many times cin.getline() numberoflines++; //needs to be used before eof() } file.close(); cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result
I am using winXP and Dev-Cpp compiler
>cin.getline(linebuffer,100);
This should be
The program is stopping dead in its tracks because the loop is keyed on whether the file is empty or not, and you never read from the file, just standard input.
Also, using eof() as a loop condition is wrong because the eofbit is only set for the stream after you try and fail to read. This will usually cause the loop to iterate one more time than you expect.
This should be
file.getline(linebuffer,100);Also, using eof() as a loop condition is wrong because the eofbit is only set for the stream after you try and fail to read. This will usually cause the loop to iterate one more time than you expect.
I'm here to prove you wrong.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
thanks for those links, i can know see the problem, but i cant seem to find a way of incorporating this into my own code
could you show me how i could alter this code to overcome the problem ?
C++ Syntax (Toggle Plain Text)
//number of lines...code stops excecuting here !!!*********************** ifstream file(filename); //open file while (!file.eof()) //loop till eof() is reached { cin.getline(linebuffer,100); //count how many times cin.getline() numberoflines++; //needs to be used before eof() } file.close(); cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output
A new tip for you and those who will follow.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
thankyou for that tip, i have managed to get this to work in all places but 1, where i used file.eof() as the loop and when i used nextcharacter = file.get() to read and work with 1 character individually.
I have tried to incorporate the input as the loop by putting:
The above didnt work, i thought it was because the compiler may have took the = as comparing the 2 instead of assigning file.get() to nextcharacter, so I then tried this:
but that didnt work either, do you have any suggestions ?
C++ Syntax (Toggle Plain Text)
while (!file.eof()) { nextcharacter = file.get(); }
C++ Syntax (Toggle Plain Text)
while(nextcharacter = file.get()) { }
C++ Syntax (Toggle Plain Text)
nextcharacter = file.get(); while(nextcharacter) { }
![]() |
Other Threads in the C++ Forum
- Previous Thread: Pointers
- Next Thread: New C/C++ Source Code Search Website
| Thread Tools | Search this Thread |
anyfile api array based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download draw dynamic dynamiccharacterarray email encryption error file forms fstream function functions game gdi+ givemetehcodez gnu graph gui homeworkhelp homeworkhelper http iamthwee ibm ifstream input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output packing parameter pointer problem program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video visualc++ visualizationtoolkit win32 windows winsock word wordfrequency wxwidgets






