| | |
Is my code any good?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 8
Reputation:
Solved Threads: 0
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.
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)
/* * My first program that wasn't from a tutorial. * (C) 2007 Laseredd. */ #include <iostream> #include <string> #include <sstream> using namespace std; class Calc { int num1, num2; public: void set_num(int, int); int add(); }; void Calc::set_num(int a, int b) { num1 = a; num2 = b; } int Calc::add() { return (num1 + num2); } int main() { Calc calc; string valid; int numb1, numb2; cout << "What numbers to you want to add? " << endl; cout << "Number 1: "; getline(cin, valid); stringstream(valid) >> numb1; cout << "Number 2: "; getline(cin, valid); stringstream(valid) >> numb2; calc.set_num(numb1, numb2); cout << calc.add(); return 0; }
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 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.
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
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
You can add validation to your code by doing somthing like this
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <algorithm> #include <sstream> using namespace std; struct nondigit { public: bool operator() (char ch) { return !isdigit(ch); } }; int main() { int num; string str; bool flag=false; cout<< "Enter number: "<< endl; cin>>str; //make sure that num contains digits exclusively if(find_if(str.begin(),str.end(), nondigit())==str.end()) flag=true; // we have a valid string else cout<<"Invalid Input"; if(flag==true) { stringstream temp(str); //insert string to temp temp>>num; //extract int value and write it to num cout<<num; } return 0; }
Last edited by SpS; Aug 18th, 2007 at 7:56 am.
![]() |
Similar Threads
- How to make windows form from c++ code (GUI) (C++)
- Country code conversion (PHP)
- What's wrong with this code? (PHP)
- Free ASP.NET Application with code to practice (ASP.NET)
- after a very unproductive day! - code bugs (C++)
- c++ code help (C++)
- My Sample ASM Code (Assembly)
- Some Basic Code Hopefully (Help Needed) (HTML and CSS)
Other Threads in the C++ Forum
- Previous Thread: Problem with strtok() for data structure creation
- Next Thread: simple project to help me understand pointers.
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






