Help with File Reading loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Help with File Reading loop

 
0
  #1
Feb 28th, 2005
Okay, I'm trying to do my homework, but my code isn't doing what I want it to. What I'm trying to do is have the user enter a file name and then the program will output the number of words in the file. It works fine the first time through the loop, but when prompted again, the file never opens correctly. Here is my code:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int numberOfWords;
  10. string fileName;
  11. string wordString;
  12. ifstream infile;
  13.  
  14. cout << "File to process (type quit to exit): ";
  15. cin >> fileName;
  16.  
  17. while(fileName != "quit")
  18. {
  19. infile.open(fileName.c_str());
  20.  
  21. assert(infile);
  22.  
  23. infile >> wordString;
  24.  
  25. for(numberOfWords = 0; infile; numberOfWords++)
  26. {
  27. infile >> wordString;
  28. }
  29.  
  30. infile.close();
  31.  
  32. cout << fileName << " has " << numberOfWords
  33. << " words." << endl << endl;
  34.  
  35. cout << "File to process (type quit to exit): ";
  36. cin >> fileName;
  37. }
  38. }
Last edited by alc6379; Mar 1st, 2005 at 11:22 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #2
Feb 28th, 2005
I have six different files that all work the first time through the loop. I've tried re-ordering the loop stuff, but nothing seems to work. I then tried infile.close() but that didn't do anything. Please don't write the code for me... maybe a suggestion on what to do would be best. I really like this class. This is the first time I've run into something I cannot fix myself.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #3
Feb 28th, 2005
I tried inFile.clear(), too but that also did nothing to help my problem.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 199
Reputation: Tight_Coder_Ex is an unknown quantity at this point 
Solved Threads: 14
Tight_Coder_Ex's Avatar
Tight_Coder_Ex Tight_Coder_Ex is offline Offline
Junior Poster

Re: Help with File Reading loop

 
0
  #4
Feb 28th, 2005
DISREGUARD, You are using iostream and I'm not sure how that works with strings.

You can't compare strings the way you do.
  1. while (strcmp (filename, "quit") != 0)

I think that should solve your problem.
Last edited by Tight_Coder_Ex; Feb 28th, 2005 at 9:57 pm. Reason: Failed to notice you are using iostream
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #5
Feb 28th, 2005
what do you mean by "You can't compare strings the way you do."? I enter the loop just fine. I also got a couple errors when I tried replacing my code with yours. BTW, I haven't learned about comparing strings yet... I gues I have to figure out a different way.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
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: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help with File Reading loop

 
0
  #6
Feb 28th, 2005
>You can't compare strings the way you do.
Sure you can. The string class overloads the relational operators. Though <string> needs to be included.

>the file never opens correctly
Try this:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. int numberOfWords;
  11. string fileName;
  12. string wordString;
  13.  
  14. cout << "File to process (type quit to exit): ";
  15. cin >> fileName;
  16.  
  17. while(fileName != "quit")
  18. {
  19. ifstream infile(fileName.c_str());
  20.  
  21. assert(infile);
  22.  
  23. infile >> wordString;
  24.  
  25. for(numberOfWords = 0; infile; numberOfWords++)
  26. {
  27. infile >> wordString;
  28. }
  29.  
  30. cout << fileName << " has " << numberOfWords
  31. << " words." << endl << endl;
  32.  
  33. cout << "File to process (type quit to exit): ";
  34. cin >> fileName;
  35. }
  36. }
>assert(infile);
No. assert is designed as a debugging tool, not as a run-time error handling mechanism. Failure to open a file is not considered a programming error, it's an input error in this case. So you should be using a conditional statement:
  1. if (!in) {
  2. cerr<<"Failure to open file"<<endl;
  3. // Terminate here or handle the error
  4. }
I know it's subtle, but a program error and a programming error are two completely different things and need to be dealt with in different ways.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #7
Feb 28th, 2005
I just read (http://www.eg.bucknell.edu/~cs204/Fa...++-strings.pdf) that you can use the comparisson opperators to compare strings and it returns a boolean value.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #8
Feb 28th, 2005
I treid putting #include <string> but it didn't change anything. The assert thing comes from my teacher. He wanted us to use that instead of an if statement. Don't ask me why. I think an if statement is more clear during the run time phase of a program, though an assert statement may look cleaner in the actual code. Any more suggestions?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,802
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: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Help with File Reading loop

 
0
  #9
Feb 28th, 2005
>He wanted us to use that instead of an if statement.
Tell him I said he's a bad teacher.

>Any more suggestions?
...Well, if you bothered to read the code I gave you, you'll find that I changed more than just adding "#include <string>". :rolleyes: Try running the code.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 15
Reputation: maizen is an unknown quantity at this point 
Solved Threads: 0
maizen maizen is offline Offline
Newbie Poster

Re: Help with File Reading loop

 
0
  #10
Feb 28th, 2005
yeah, sorry about that... I'm stressing out about this program. I'm running it right now...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC