New in C++ asking for suggestions

Hello everyone,

I just started learning this C++ language. (I dont have any previous programming experience) Most forums the I've search says this language is unfriendly for beginners in programming, I havent tried learning other language yet thus I cant tell if its true. Anyways, My first program (posted below) is a simple calculator in cmd.
I know that a good program is somewhat simple and neat, so I am welcoming any suggestions and comments for the improvement of this program.

Thank you

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;

double iNumber, iNumber2;
void a();
void b();
void c();
void d();


void main()
{
	home:
	cout << " CONSOLE CALCULATOR\n\n";
	char iSign; iSign = '+' , '-' , '*' , '/';
	cin >> iNumber;
	cin >> iSign;  
	cin >> iNumber2;
	if (iSign == '+' ) { a();}
	if (iSign == '-' ) { b();}
	if (iSign == '*' ) { c();}
	if (iSign == '/' ) { d();}
	
	goto home;

}
	
void a() 
{ 
	cout << "\n" << iNumber + iNumber << "\n\n";
}

void b() 
{ 
	cout << "\n" << iNumber - iNumber2  << "\n\n";
}

void c() 
{ 
	cout << "\n" << iNumber * iNumber2  << "\n\n";
}

void d() 
{ 
	cout << "\n" << iNumber / iNumber2  << "\n\n";
}
I'd avoid goto and instead put the body of main in a while loop with a boolean check each time (e.g. you press '.' as sign and it exits).

Secondly I'd name the functions according to what they do. I'd also pass the parameters instead of making them global.

And I'd add the % calculation (modulus). And not use using namespace std;, but instead write std::
use else:
1
2
3
4
5
	if (iSign == '+' ) { a();}
	else if (iSign == '-' ) { b();}
	else if (iSign == '*' ) { c();}
	else if (iSign == '/' ) { d();}
	else cout << "Invalid sign" << "\n";


line 15: This iSign = '+' , '-' , '*' , '/'; has no effect, remove it
Thank you guys for your quick replies..

I will start modifying my program according to your suggestions.

closed account (N85iE3v7)
Oxxo,

main does not return a void. In the past C functions defaulted to int. so programmers could write main(void) but main void returned int by default just like any other C function with a type return not expressed.

This is not a real criticism, it is just that you should use a new compiler that accepts modern C++.

If you are using goto, it is a good idea for error handling like:

1
2
3
4
5
6
7
8
9
10
11
int function()
{  
     // some error here
     goto error;

    // ...

  error:
     // error handling code here

}



In this way you won't write code that it is hard to mantain. Once I had to fix a 1500 lines function with so many gotos and the only way to solve it was to add a new goto. I know it is ugly, but sometimes it is what you have to do.
@zlogdan: why not use exceptions? I thought they were specifically made for handling errors (with stack trace, break on all exceptions in VS AFAIK).
closed account (N85iE3v7)
@Bourgond, completely agree about using try / catch statements, they are far better!
Topic archived. No new replies allowed.