Is this proper programming?

here is my simple calculator.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream> /// my includes
#include <string>
using namespace std;
int fnum,snum,answer; char operation; /// Variables

int main(){    ///start of code
	cout<<"Please type an operation... +,-,*,/."<<endl;    /// Operation input
	cin.get(operation);   
	cout<<"Please enter your first number"<<endl;     /// first number input
	cin>>fnum;
	cout<<"Please enter your second number"<<endl;   /// second number input
	cin>>snum;
	cout<<"processing..."<<"\n"<<endl;   /// random (filler)
	system("pause");
	cout<<"\n\n"<<endl;  // skips some lines so it looks cleaner in the consol
	switch(operation)
	{                                      /// Switch statement to use the operation at the top
	case'+':answer=fnum+snum; break; // addition
	case'-':answer=fnum-snum; break; // subtraction
	case'*':answer=fnum*snum; break; // multiplication
	case'/':answer=fnum/snum; break; // division
	}
	cout<<"##############"<<endl;
	cout<<"#   answer   #"<<endl;
	cout<<fnum<<operation<<snum<<"="<<answer<<endl;       /// output/answer with a little box around it
	cout<<"#            #"<<endl;
	cout<<"##############"<<endl;
	system("pause");
return 0;		
}			/// end of code 


Please leave suggestions on how i could make it better or fix things.
Apart from the lack of input validation (which I assume you don't care about for this small program), it looks fine. =)

Some suggestions:

1) Don't make variables global. It's a bad habit. Global variables are sloppy. fnum, snum, answer, and operation are all used only inside of main, so that is where they should be defined.

2) Don't be afraid of whitespace. You don't have to cram all the lines so close together. Sometimes putting a few blank lines here and there can make the code easier to read and follow.

3) Some people will tell you that system("pause") is evil. Apart from being non-portable it's a potential security risk. There's like a bazillion topics on this on these boards if you want to read about it.



EDIT:

4) You've got what I got "beginner comments".... your code comments aren't very useful because they're stating the obvious (ie: commenting "addition" on a line that is clearly adding two numbers). This isn't a bad thing... it just kind of exposes your inexperience ;)

It's hard to explain how to comment well. Usually I only try to clarify what I'm doing (apart from general function/class documentation) if the code isn't obvious or if it's part of a larger picture.
Last edited on
Remove

 
using namespace std;


and just type the std:: prefix before each call to cin and cout.
why remove using namespace std; it just makes things quicker?


@Disch thank you for the advice, one question, do you know how to program games(images for them and such)?
@CrazyKane If you want to program games try using this SDL tutorial to learn 2D programming. lazyfoo.net
@Niven I know about that site but right now im trying to learn how to programming in c++ then ill move onto SDL/OpenGL. also i was just asking Disch.
closed account (N36fSL3A)
Niven where did that come from? Lol.
@Disch thank you for the advice, one question, do you know how to program games(images for them and such)?


I clicked on this topic, saw that, and I didn't think that he actually directed the question solely at Disch so I thought I would give some advice. That's where it came from.
closed account (N36fSL3A)
Well that's pretty rude of CrazyKane then. He should be grateful for whatever help he gets.

What I'm trying to figure out is why he asked how to make them if he doesn't know C++ well.
i didnt put it in a mean why i just directeted it at Disch... sorry i guess, for what i did which im confused about.
@Disch thank you for the advice, one question, do you know how to program games(images for them and such)?


Yes.

And FWIW, I would avoid LazyFoo's tutorials, as he teaches SDL 1.x which is horrendously old and outdated. If you must learn SDL, I'd recommend SDL 2.x... though personally I prefer SFML.
cool i was just guess/wondering that you knew.
Topic archived. No new replies allowed.