944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2352
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 23rd, 2005
0

code seems to stop executing

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  1. /*This program is to find out statistics of a specified file*/
  2.  
  3. #include<fstream.h>
  4. #include<iostream.h>
  5. #include<stdlib.h>
  6. #include<conio.h>
  7.  
  8. int characternumber,arraylength,nextcharacter,
  9. numberofcharacters,totalnumberofcharacters,numberoflines;
  10. char filename[100];
  11. char linebuffer[100];
  12. char characters[100]="abcdefghijklmnopqrstuvwxyz0123456789[];'#,./<>?:@~{}!\"£$%^&*()_+=-\\`¬|";
  13.  
  14.  
  15. int main ()
  16. {
  17. cout<<"Enter the filename: "; //get user input
  18. cin.getline(filename,100);
  19.  
  20. ifstream file1(filename); //check to see if file exists
  21. if (!file1.is_open()) //if file doesnt exist clear screen and restart
  22. {
  23. cout<<endl<<endl<<"file does not exist\n\n";
  24. system("PAUSE");
  25. system("CLS");
  26. main();
  27. }
  28. file1.close();
  29.  
  30. 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
  31. characternumber = 0; //reinitialize the variables
  32. numberofcharacters = 0;
  33. totalnumberofcharacters = 0;
  34. numberoflines = 0;
  35.  
  36. /*this section finds out how many of each character is in the file*/
  37.  
  38. while (characternumber <= arraylength) //main loop do this for each character
  39. {
  40. ifstream file(filename);
  41. while (!file.eof()) //2nd loop, goes through file 1ce for each character
  42. {
  43. nextcharacter = file.get();
  44. if (nextcharacter == characters[characternumber])
  45. {
  46. numberofcharacters++;
  47. }
  48. else
  49. {
  50. if (characternumber == 0) //makes sure it calculates total # of chars 1nce
  51. {
  52. totalnumberofcharacters++;
  53. }
  54. }
  55. }
  56. cout<<characters[characternumber]<<" = "<<numberofcharacters<<"\t"; //output results in columns
  57. numberofcharacters = 0; //re-initialize this variable for next character
  58. characternumber++; //get next character
  59. file.close(); //close file as it is re-opened at beginning of main loop
  60. }
  61.  
  62. cout<<endl<<endl<<"Total number of Characters: "<<totalnumberofcharacters; //output total number of chars only 1ce
  63.  
  64. //number of lines...code stops excecuting here !!!***********************
  65. ifstream file(filename); //open file
  66. while (!file.eof()) //loop till eof() is reached
  67. {
  68. cin.getline(linebuffer,100); //count how many times cin.getline()
  69. numberoflines++; //needs to be used before eof()
  70. }
  71. file.close();
  72. cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result
  73.  
  74. /*extra features are needed like
  75.   number of lines: how many times do ya need to do cin.getline() before eof(),
  76.   filesize: need to look this up
  77.   wordcount: if character == space or newline and character after that is not == a space or newline then tht is 1 word
  78.   *****need lots of extra features******
  79.   */
  80.  
  81. system(filename); //open file for comparing - testing
  82. getch(); //keep on screen
  83.  
  84. /*
  85.   take user input on a file they want to check
  86.   use arraylength as product of loop with incremental characternumber that starts at 0 while (characternumber <= arraylength)
  87.   take 1st char from array and then use that to compare
  88.   use a loop to get next char from file and compare
  89.   if the character matchs the 1 brought form the array increment the numberofcharacters
  90.   when eof if reached break from loop, output number of chars and add 1 to characternumber and go through the loop again
  91.   do this till all characters in the array have been checked and outputted
  92.  
  93.   next section to count the total number of chars and estimated file size
  94.   then get actual file size
  95.   */
  96. }
i dont know whether i am right but i think the problem lies within this section of code
C++ Syntax (Toggle Plain Text)
  1. //number of lines...code stops excecuting here !!!***********************
  2. ifstream file(filename); //open file
  3. while (!file.eof()) //loop till eof() is reached
  4. {
  5. cin.getline(linebuffer,100); //count how many times cin.getline()
  6. numberoflines++; //needs to be used before eof()
  7. }
  8. file.close();
  9. cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output result
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
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Aug 23rd, 2005
0

Re: code seems to stop executing

>cin.getline(linebuffer,100);
This should be
file.getline(linebuffer,100);
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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 23rd, 2005
0

Re: code seems to stop executing

ok thanks for your help. Before looking back on here i spotted that error in the code where i needed to change cin to file lol, thanks for your help anyway
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Aug 23rd, 2005
0

Re: code seems to stop executing

Don't forget to read all of my post. Your code has a subtle bug that needs to be fixed.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Aug 23rd, 2005
0

Re: code seems to stop executing

im not exactly sure what you mean with using eof() as the loop condition, it works anyway, but thanks a lot for the tip i will read up on it
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Aug 23rd, 2005
0

Re: code seems to stop executing

could you give me an example of how i mite overcome this problem, eof() is the only thing i have used while using files.
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 23rd, 2005
0

Re: code seems to stop executing

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
C++ Syntax (Toggle Plain Text)
  1. //number of lines...code stops excecuting here !!!***********************
  2. ifstream file(filename); //open file
  3. while (!file.eof()) //loop till eof() is reached
  4. {
  5. cin.getline(linebuffer,100); //count how many times cin.getline()
  6. numberoflines++; //needs to be used before eof()
  7. }
  8. file.close();
  9. cout<<endl<<endl<<"Number of lines: "<<numberoflines; //output
could you show me how i could alter this code to overcome the problem ?
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005
Aug 23rd, 2005
0

Re: code seems to stop executing

A new tip for you and those who will follow.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 24th, 2005
0

Re: code seems to stop executing

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.
C++ Syntax (Toggle Plain Text)
  1. while (!file.eof())
  2. {
  3. nextcharacter = file.get();
  4. }
I have tried to incorporate the input as the loop by putting:
C++ Syntax (Toggle Plain Text)
  1. while(nextcharacter = file.get())
  2. {
  3. }
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:
C++ Syntax (Toggle Plain Text)
  1. nextcharacter = file.get();
  2. while(nextcharacter)
  3. {
  4. }
but that didnt work either, do you have any suggestions ?
Reputation Points: 23
Solved Threads: 5
Posting Whiz in Training
bops is offline Offline
214 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Pointers
Next Thread in C++ Forum Timeline: New C/C++ Source Code Search Website





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC