code seems to stop executing

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

code seems to stop executing

 
0
  #1
Aug 23rd, 2005
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

  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
  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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: code seems to stop executing

 
0
  #2
Aug 23rd, 2005
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: code seems to stop executing

 
0
  #3
Aug 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: code seems to stop executing

 
0
  #4
Aug 23rd, 2005
Don't forget to read all of my post. Your code has a subtle bug that needs to be fixed.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: code seems to stop executing

 
0
  #5
Aug 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: code seems to stop executing

 
0
  #6
Aug 23rd, 2005
could you give me an example of how i mite overcome this problem, eof() is the only thing i have used while using files.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: code seems to stop executing

 
0
  #7
Aug 23rd, 2005
"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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: code seems to stop executing

 
0
  #8
Aug 23rd, 2005
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
  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 ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,342
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: code seems to stop executing

 
0
  #9
Aug 23rd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: code seems to stop executing

 
0
  #10
Aug 24th, 2005
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.
  1. while (!file.eof())
  2. {
  3. nextcharacter = file.get();
  4. }
I have tried to incorporate the input as the loop by putting:
  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:
  1. nextcharacter = file.get();
  2. while(nextcharacter)
  3. {
  4. }
but that didnt work either, do you have any suggestions ?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC