943,947 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3254
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 4th, 2007
1

help with an assignement please

Expand Post »
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cctype>
  4. using std::fstream;
  5.  
  6. using namespace std;
  7.  
  8. int count[26];
  9. int main(int argc, char *argv[])
  10. {
  11. char ch;
  12.  
  13. ifstream infile("c:\\a.txt") ;
  14. if ( !infile )
  15. {
  16. cerr << "File could not be opened" << endl;
  17. exit( 1 );
  18. }
  19. int i;
  20. for(i = 0; i <26; i++) count[ i ] = 0;
  21.  
  22. while (! infile.eof())
  23. {
  24. ch = cin.get();
  25. if(isalpha(ch))
  26. {
  27. ch = toupper(ch);
  28. count[ch-'A']++;
  29. }
  30. };
  31. for(i = 0; i <26; i++) {
  32. cout << (char) ('A'+ i) << ": " << count[ i ] << '\n';
  33. }
  34.  
  35. return 0;
  36. }
what i wanted to do with this code was input a stream of alphabetical characters and output the amount of each alphabetical letter. for ex. if the text said aaabbccccc.....yyzzzz i want to output
a:3
b:2
c:5 etc etc

but even this compiles alrigght and file was opened all i get is a blank screen. i'm really stumped and since i'm such a newbie i really don't know what im doing wrong. ang suggestions or comments will be useful.
Similar Threads
Reputation Points: 21
Solved Threads: 0
Newbie Poster
loslos is offline Offline
4 posts
since Feb 2007
Feb 4th, 2007
0

Re: help with an assignement please

>> ch = cin.get();
you should use infine, not cin
C++ Syntax (Toggle Plain Text)
  1. ch = infile.get();

And thanks for taking the time to read the board's rules to use code tags correctly. Not many first-time posters do that.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Feb 4th, 2007
0

Re: help with an assignement please

Off topic, but in the spirit of trying not to learn bad habits, and trying to avoid bugs that can be difficult to find when trying to determine why you don't get the results you expect on trial runs with known input as you test your code, the following
C++ Syntax (Toggle Plain Text)
  1. while (! infile.eof())
  2. {
  3. ch = cin.get();
could be changed to

while(infile >> ch)

You should avoid using the return value of eof() as the controlling condition for a loop. There are two reasons I know of to want to do this.

First, using the return value of eof() to terminate the loop is likely to cause a double counting of the last char in the file. If that char is a newline, then the double count will go unnoticed in your current code. But if the file terminates with an alphabetical char, it may, or may not, be counted twice. Inmy experience, trying to figure out why the code works appropriately sometimes and not others is probably the hardest bug to find.

Second, using the return value of the input statement to control the loop allows the loop to terminate appropriately, whether it terminates becuase it read the entire file, or whether it terminates because of some other cause. The loop might not terminate appropriately if the control condition is only whether the EOF marker was found but the linput stream fails for some other reason.
Last edited by Lerner; Feb 4th, 2007 at 11:12 am.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by Lerner ...
You should avoid using the return value of eof() as the controlling condition for a loop. There are two reasons I know of to want to do this.

First, using the return value of eof() to terminate the loop is likely to cause a double counting of the last char in the file. If that char is a newline, then the double count will go unnoticed in your current code. But if the file terminates with an alphabetical char, it may, or may not, be counted twice. Inmy experience, trying to figure out why the code works appropriately sometimes and not others is probably the hardest bug to find.
That would be feof( ) you are talking about which returns zero only *after* the end of file is reached and not *when* the end of file is reached. eof( ) is actually good and returns a boolean value when the end of file is reached.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
That would be feof( ) you are talking about which returns zero only *after* the end of file is reached and not *when* the end of file is reached. eof( ) is actually good and returns a boolean value when the end of file is reached.
I don't get it. eof() returns true if the eofbit is set, but the eofbit doesn't get set until you hit end of file while reading. Unless you test for end of file after the read, the order is wrong and you'll loop one too many times. Here's an example that shows the problem.
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5.  
  6. int main()
  7. {
  8. using namespace std;
  9.  
  10. // Build a test file
  11. {
  12. ofstream os( "test.txt" );
  13.  
  14. os << "12345";
  15. }
  16.  
  17. // Test the test file :)
  18. ifstream is( "test.txt" );
  19. int loopCount = 0;
  20. char ch;
  21.  
  22. while ( !is.eof() ) {
  23. ++loopCount;
  24. is.get( ch );
  25. cout << ch << '\n';
  26. }
  27.  
  28. cout << "Loop count: " << loopCount << '\n';
  29.  
  30. return 0;
  31. }
eof() and feof() have the same problem because they work the same way.
Reputation Points: 84
Solved Threads: 15
Posting Whiz in Training
Ravalon is offline Offline
209 posts
since Dec 2006
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
That would be feof( ) you are talking about which returns zero only *after* the end of file is reached and not *when* the end of file is reached. eof( ) is actually good and returns a boolean value when the end of file is reached.
No I believe eof has similar problems when the file contains extra newlines.

Like lerner and ravalon said best to avoid it altogether, especially in c++. I'm surprised your little google groups didn't tell you that sos. Ha ha.
Last edited by iamthwee; Feb 4th, 2007 at 1:22 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by Ravalon ...
I don't get it. eof() returns true if the eofbit is set, but the eofbit doesn't get set until you hit end of file while reading. Unless you test for end of file after the read, the order is wrong and you'll loop one too many times.
My bad, I should have been more explicit. Yes, I agree, feof( ) and eof( ) are not that great when compared to checking the stream state while reading files. But for small fixes I normally use:
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. // Build a test file
  10. {
  11. ofstream os( "test.txt" );
  12.  
  13. os << "12345";
  14. }
  15.  
  16. // Test the test file :)
  17. ifstream is( "test.txt" );
  18. int loopCount = 0;
  19. char ch;
  20.  
  21. while (1 ) {
  22. is.get( ch );
  23. if( is.eof() ) break ;
  24. ++loopCount;
  25. cout << ch << '\n';
  26. }
  27.  
  28. cout << "Loop count: " << loopCount << '\n';
  29. getchar( ) ;
  30. return 0;
  31. }
Quote originally posted by Iamthwee ...
I'm surprised your little google groups didn't tell you that sos. Ha ha.
*sigh* Btw when are you turning 14 ?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
But for small fixes I normally use:
C++ Syntax (Toggle Plain Text)
  1. while ( 1 ) {
  2. is.get( ch );
  3. if( is.eof() ) break ;
  4. ++loopCount;
  5. cout << ch << '\n';
  6. }
Why that instead of this? Seems like more for less.
C++ Syntax (Toggle Plain Text)
  1. while ( is.get( ch ) ) {
  2. ++loopCount;
  3. cout << ch << '\n';
  4. }
This will break the loop on errors too.
Last edited by Dave Sinkula; Feb 4th, 2007 at 3:43 pm.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Feb 4th, 2007
0

Re: help with an assignement please

Quote ...
Yes, I agree, feof( ) and eof( ) are not that great when compared to checking the stream state while reading files. But for small fixes I normally use:
As always, in these circumstances go for the solution that works every time. Sure you can build functionality in eof() so that it works correctly.

Hell you can even use gets() safely if you go outta your way to implement the requisite error checks.

But that's extra work for very little benefit.

See you don't learn this at the google groups.:lol:
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 4th, 2007
0

Re: help with an assignement please

Click to Expand / Collapse  Quote originally posted by iamthwee ...
As always, in these circumstances go for the solution that works every time. Sure you can build functionality in eof() so that it works correctly.

Hell you can even use gets() safely if you go outta your way to implement the requisite error checks.

But that's extra work for very little benefit.
The code snippet / my post was meant to illustrate that using eof( ) is not completely wrong or taboo if used the right way. Just ignoring a part of language just because using it in the wrong way ensues chaos is foolishness.

An use of eof( ) would be:
  1. char buf[BUFSIZE];
  2. do {
  3. in.read( buf, BUFSIZE );
  4. std::streamsize n = in.gcount();
  5. out.write( buf, n );
  6. } while( in.good() );
  7. if( in.bad() || !in.eof() ) {
  8. // fatal error occurred
  9. }
  10. in.close();
Not bad, is it...

So stop considering a feature bad just because someone says so...
Quote ...
See you don't learn this at the google groups.
I guess you were badly flamed at google groups for obvious reasons, thats why you keep mentioning it.....
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006

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: problem opening CSV file
Next Thread in C++ Forum Timeline: NOOB flashback





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


Follow us on Twitter


© 2011 DaniWeb® LLC