Is my code any good?

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

Join Date: Aug 2007
Posts: 8
Reputation: laseredd is an unknown quantity at this point 
Solved Threads: 0
laseredd laseredd is offline Offline
Newbie Poster

Is my code any good?

 
1
  #1
Aug 17th, 2007
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.
  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Is my code any good?

 
0
  #2
Aug 18th, 2007
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 793
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Is my code any good?

 
0
  #3
Aug 18th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Is my code any good?

 
0
  #4
Aug 18th, 2007
You can add validation to your code by doing somthing like this
  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.
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