C++ Question Mark counter-- Any Improvements in Code?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

C++ Question Mark counter-- Any Improvements in Code?

 
0
  #1
Apr 29th, 2009
Hey all,
I have decided that in order to learn i will need to write code. So i am working on all the questions on my book. And as I am teaching myself i end up with no-one to scrutinize my code.

  1.  
  2. #include <iostream>
  3.  
  4. /************Declarations of Functions*********/
  5. void func1();
  6. void func2();
  7. //=============================================Tue 28 Apr 2009 18:37:48 IST//
  8. /* Global Test Variables */
  9.  
  10. int const max_length=18;
  11. int quest_count=0 ;
  12. char input_line[max_length]="???sky???";
  13.  
  14.  
  15. int main()
  16. {
  17.  
  18. func1();// Character string with While loop
  19. std::cout<<"No Of Question Marks = " << quest_count<< "\n";
  20. quest_count=0;
  21. func2();//Pointer Based Passing.
  22. std::cout<<"No Of Question Marks(Pointer Based) = " << quest_count<< "\n";
  23. }
  24.  
  25. void func1()
  26. {
  27. int i=0;
  28. while(i<max_length)
  29. {
  30. if(input_line[i]=='?')
  31. quest_count++;
  32. i++;
  33. }
  34. }
  35.  
  36. void func2()
  37. {
  38.  
  39. char *ptr=input_line;
  40. while(*ptr!=0)
  41. {
  42. if(*ptr=='?')
  43. {
  44. quest_count++;
  45. }
  46. *ptr++;
  47. }
  48.  
  49. }

The above program counts the number of "?" marks in a string passed to it.

I would like to recieve comments on the code and any improvements that can be made.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 973
Reputation: Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough Alex Edwards is a jewel in the rough 
Solved Threads: 107
Alex Edwards's Avatar
Alex Edwards Alex Edwards is offline Offline
Posting Shark

Re: C++ Question Mark counter-- Any Improvements in Code?

 
2
  #2
Apr 29th, 2009
I think it would be more useful if one could pass the string into a method and have it return the number of question-marks found (as an unsigned int, or in extraordinary cases an unsigned long int).
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: C++ Question Mark counter-- Any Improvements in Code?

 
0
  #3
Apr 29th, 2009
Thanks for the reply.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 24
Reputation: lqdo is an unknown quantity at this point 
Solved Threads: 0
lqdo's Avatar
lqdo lqdo is offline Offline
Newbie Poster

Re: C++ Question Mark counter-- Any Improvements in Code?

 
1
  #4
Apr 29th, 2009
I would define a constant char for the '?' comparer. If you want to change the compare criteria, you only have to change it once.
C++ it!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC