Switch logic error

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

Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Switch logic error

 
0
  #1
Feb 21st, 2006
Hey guys I am just wondering why the following code gives the wrong number of characters from a file when the character count is the default? Thanks.

  1.  
  2. void main()
  3. {
  4. int blank_count = 0;
  5. int char_count = 0;
  6. int sentence_count = 0;
  7. char ch;
  8.  
  9. ifstream iFile("c:\test.txt");
  10.  
  11. if (! iFile)
  12. {
  13. cout << "Error opening input file" << endl;
  14.  
  15. }
  16.  
  17. while (iFile.get(ch))
  18. {
  19. switch (ch) {
  20. case ' ':
  21. blank_count++;
  22. break;
  23. case '.':
  24. sentence_count++;
  25. break;
  26. default:
  27. char_count++;
  28. break;
  29. }
  30. }
  31.  
  32. cout << "There are " << blank_count << " blanks" << endl;
  33. cout << "There are " << char_count << " characters" << endl;
  34. cout << "There are " << sentence_count << " sentences" << endl;
  35. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,433
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: 1471
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Switch logic error

 
0
  #2
Feb 21st, 2006
why do you think it is incorrect? Is the char count supposed to include spaces and periods? How about end-of-line terminating characters?

Why not consider question mark ? and exclaimation point! as end-of-sentence terminators/
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 91
Reputation: Jon182 is an unknown quantity at this point 
Solved Threads: 0
Jon182 Jon182 is offline Offline
Junior Poster in Training

Re: Switch logic error

 
0
  #3
Feb 21st, 2006
Thanks for your reply Ancient Dragon, if you get a moment can you please see my other post. Thanks again
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