| | |
help with an assignement please
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 4
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cctype> using std::fstream; using namespace std; int count[26]; int main(int argc, char *argv[]) { char ch; ifstream infile("c:\\a.txt") ; if ( !infile ) { cerr << "File could not be opened" << endl; exit( 1 ); } int i; for(i = 0; i <26; i++) count[ i ] = 0; while (! infile.eof()) { ch = cin.get(); if(isalpha(ch)) { ch = toupper(ch); count[ch-'A']++; } }; for(i = 0; i <26; i++) { cout << (char) ('A'+ i) << ": " << count[ i ] << '\n'; } return 0; }
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.
>> ch = cin.get();
you should use infine, not cin
And thanks for taking the time to read the board's rules to use code tags correctly. Not many first-time posters do that.
you should use infine, not cin
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Jul 2005
Posts: 1,692
Reputation:
Solved Threads: 267
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
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.
C++ Syntax (Toggle Plain Text)
while (! infile.eof()) { ch = cin.get();
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.
•
•
•
•
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.
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.
•
•
•
•
That would befeof( )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.
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)
#include <fstream> #include <iostream> #include <string> int main() { using namespace std; // Build a test file { ofstream os( "test.txt" ); os << "12345"; } // Test the test file :) ifstream is( "test.txt" ); int loopCount = 0; char ch; while ( !is.eof() ) { ++loopCount; is.get( ch ); cout << ch << '\n'; } cout << "Loop count: " << loopCount << '\n'; return 0; }
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
•
•
That would befeof( )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.
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*
•
•
•
•
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.
C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <string> int main() { using namespace std; // Build a test file { ofstream os( "test.txt" ); os << "12345"; } // Test the test file :) ifstream is( "test.txt" ); int loopCount = 0; char ch; while (1 ) { is.get( ch ); if( is.eof() ) break ; ++loopCount; cout << ch << '\n'; } cout << "Loop count: " << loopCount << '\n'; getchar( ) ; return 0; }
•
•
•
•
Originally Posted by Iamthwee
I'm surprised your little google groups didn't tell you that sos. Ha ha.
I don't accept change; I don't deserve to live.
•
•
•
•
But for small fixes I normally use:
C++ Syntax (Toggle Plain Text)
while ( 1 ) { is.get( ch ); if( is.eof() ) break ; ++loopCount; cout << ch << '\n'; }
C++ Syntax (Toggle Plain Text)
while ( is.get( ch ) ) { ++loopCount; cout << ch << '\n'; }
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
•
•
•
•
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:
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*
•
•
•
•
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.
An use of eof( ) would be:
c Syntax (Toggle Plain Text)
char buf[BUFSIZE]; do { in.read( buf, BUFSIZE ); std::streamsize n = in.gcount(); out.write( buf, n ); } while( in.good() ); if( in.bad() || !in.eof() ) { // fatal error occurred } in.close();
So stop considering a feature bad just because someone says so...
•
•
•
•
See you don't learn this at the google groups.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- (C++) Writing a Copy Constructor?!? (C++)
- Linkedlist Troubles...plz Help (Java)
- Please I Need U Again (C++)
- How to write a *WORKING* reservation system for public computers (C)
- Forum lurkers, introduce yourself ... !! (Community Introductions)
- "File operations in c++" (C++)
- beginner (C++)
- Counting the amount of letters in a phrase! (C++)
Other Threads in the C++ Forum
- Previous Thread: problem opening CSV file
- Next Thread: NOOB flashback
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline graph homeworkhelper iamthwee ifstream input int integer java lib linux list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






