943,917 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1260
  • C++ RSS
Apr 29th, 2009
0

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

Expand Post »
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.

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 29th, 2009
2

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

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).
Reputation Points: 392
Solved Threads: 108
Posting Shark
Alex Edwards is offline Offline
971 posts
since Jun 2008
Apr 29th, 2009
0

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

Thanks for the reply.
Reputation Points: 673
Solved Threads: 125
Practically a Posting Shark
Sky Diploma is offline Offline
818 posts
since Mar 2008
Apr 29th, 2009
1

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

I would define a constant char for the '?' comparer. If you want to change the compare criteria, you only have to change it once.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lqdo is offline Offline
24 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: pointer to TREE structuring      
Next Thread in C++ Forum Timeline: how to sort Gtk::Treeview?





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


Follow us on Twitter


© 2011 DaniWeb® LLC