943,596 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 970
  • C++ RSS
Aug 17th, 2007
1

Is my code any good?

Expand Post »
Hello,
I recently finished a tutorial on C++ and I have written a small program. Could you please take a look at my code and give me feedback on it?

It's a program that will ask you for two values and then add them together.

Please be nice, since it's my first "non-tutorial" program and I just turned 13.
C++ Syntax (Toggle Plain Text)
  1. /*
  2.  * My first program that wasn't from a tutorial.
  3.  * (C) 2007 Laseredd.
  4.  */
  5.  
  6. #include <iostream>
  7. #include <string>
  8. #include <sstream>
  9. using namespace std;
  10.  
  11. class Calc
  12. {
  13. int num1, num2;
  14. public:
  15. void set_num(int, int);
  16. int add();
  17. };
  18.  
  19. void Calc::set_num(int a, int b)
  20. {
  21. num1 = a;
  22. num2 = b;
  23. }
  24.  
  25. int Calc::add()
  26. {
  27. return (num1 + num2);
  28. }
  29.  
  30. int main()
  31. {
  32. Calc calc;
  33. string valid;
  34. int numb1, numb2;
  35.  
  36. cout << "What numbers to you want to add? " << endl;
  37.  
  38. cout << "Number 1: ";
  39. getline(cin, valid);
  40. stringstream(valid) >> numb1;
  41.  
  42. cout << "Number 2: ";
  43. getline(cin, valid);
  44. stringstream(valid) >> numb2;
  45.  
  46. calc.set_num(numb1, numb2);
  47. cout << calc.add();
  48.  
  49. return 0;
  50. }
Similar Threads
Reputation Points: 19
Solved Threads: 0
Newbie Poster
laseredd is offline Offline
8 posts
since Aug 2007
Aug 18th, 2007
0

Re: Is my code any good?

How to judge this depends on what your intentions were when writing it...

As a means of adding 2 numbers it's overly complicated, as a way of learning how simple classes can be written it's not bad.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Aug 18th, 2007
0

Re: Is my code any good?

Hi Laseredd,

That short program looks good! I think you have shown understanding of some very important C++ principles here, classes, methods, I/O.
As an extension, how do you think you would go about accepting either integers or floats rather than just integers? Can you think of how your Calc class might be written to accept two strings and concatenate them together (add the second onto the end of the first)?

darkagn
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 18th, 2007
0

Re: Is my code any good?

You can add validation to your code by doing somthing like this
c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. struct nondigit
  8. {
  9. public:
  10. bool operator() (char ch)
  11. {
  12. return !isdigit(ch);
  13. }
  14. };
  15.  
  16.  
  17. int main()
  18. {
  19. int num;
  20. string str;
  21. bool flag=false;
  22. cout<< "Enter number: "<< endl;
  23. cin>>str;
  24.  
  25. //make sure that num contains digits exclusively
  26. if(find_if(str.begin(),str.end(), nondigit())==str.end())
  27. flag=true; // we have a valid string
  28. else
  29. cout<<"Invalid Input";
  30.  
  31. if(flag==true)
  32. {
  33. stringstream temp(str); //insert string to temp
  34. temp>>num; //extract int value and write it to num
  35. cout<<num;
  36. }
  37.  
  38. return 0;
  39. }
Last edited by SpS; Aug 18th, 2007 at 7:56 am.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

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: Problem with strtok() for data structure creation
Next Thread in C++ Forum Timeline: simple project to help me understand pointers.





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


Follow us on Twitter


© 2011 DaniWeb® LLC