help with an assignement please

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

Join Date: Feb 2007
Posts: 4
Reputation: loslos is an unknown quantity at this point 
Solved Threads: 0
loslos loslos is offline Offline
Newbie Poster

help with an assignement please

 
1
  #1
Feb 4th, 2007
  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,475
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: help with an assignement please

 
0
  #2
Feb 4th, 2007
>> ch = cin.get();
you should use infine, not cin
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,692
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 267
Lerner Lerner is offline Offline
Posting Virtuoso

Re: help with an assignement please

 
0
  #3
Feb 4th, 2007
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
  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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,627
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with an assignement please

 
0
  #4
Feb 4th, 2007
Originally Posted by Lerner View Post
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: help with an assignement please

 
0
  #5
Feb 4th, 2007
Originally Posted by ~s.o.s~ View Post
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.
  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.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with an assignement please

 
0
  #6
Feb 4th, 2007
Originally Posted by ~s.o.s~ View Post
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,627
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with an assignement please

 
0
  #7
Feb 4th, 2007
Originally Posted by Ravalon View Post
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:
  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. }
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 ?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,389
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: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: help with an assignement please

 
0
  #8
Feb 4th, 2007
Originally Posted by ~s.o.s~ View Post
But for small fixes I normally use:
  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.
  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.
"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: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: help with an assignement please

 
0
  #9
Feb 4th, 2007
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:
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,627
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: help with an assignement please

 
0
  #10
Feb 4th, 2007
Originally Posted by iamthwee View Post
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...
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.....
I don't accept change; I don't deserve to live.
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



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

©2003 - 2009 DaniWeb® LLC