I made a basic calculator, see how it is and talk your opinion please :)

See my code and talk your opinion about how like it.
I'm new, and i have made some codes to pratice, like this 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
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int main()
{
	int a, b;
	string oper;
	cout << "Digit the operator as you want" << "(Available: sum, minus, multi, div)." << endl;
	cin >> oper;
	if (oper == "sum") {
		cout << "Now digit the numbers you want to calculate" << endl;
		cin >> a >> b;
		double sum = a + b;
		cout << "Results: " << sum << endl;
	}
	else if (oper == "minus") {
		cout << "Now digit the numbers you want to calculate" << endl;
		cin >> a >> b;
		double minus = a - b;
		cout << "Results: " << minus << endl;
	}
	else if (oper == "multi") {
		cout << "Now digit the numbers you want to calculate" << endl;
		cin >> a >> b;
		double multi = a * b;
		cout << "Results: " << multi << endl;
	}
	else if (oper == "div") {
		cout << "Now digit the numbers you want to calculate" << endl;
		cin >> a >> b;
		int div = a / b;
		int rest = a % b;
		cout << "Results: " << div << " with rest " << rest << endl;
	}
	else {
		cout << "This is not available in this program, sorry :/" << endl;
		cout << "Try again";
	}
	cout << "Thanks for use the basic caculator :0" << endl;
	return 0;
}
Hello Pkmost,

Welcome to the forum.

line 4 best not to use.

Try to avoid using using namespace std; in your programs it may seem easy now, but WILL get you in trouble some day.

It is better to learn to qualify what is in the standard name space with "std::" and then to learn what is in the standard name space now while it is easy.

What you are most likely to use for now is "std::cout", "std::cin" and "std::endl". About a week or so of typing this and you will not even notice that you are doing it.

Line 8: It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with an do not need initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

Line 10. When prompting the user for this kind of input you should use "+, -, * or /" to avoid any typing errors.

Starting at line 12 the if/else if statements are OK, but I would use a switch if you have learned that.

The other thing I fine that makes the code is to put thee opening { on the next line so it lines up in the same column as the closing }.

Example:
1
2
3
4
5
6
7
if (oper == "sum")
{
	std::cout << "Now digit the numbers you want to calculate" << std::endl;
	std::cin >> a >> b;
	double sum = a + b;
	std::cout << "Results: " << sum << std::endl;
}


You will find this useful when the closing brace is past the last line of the screen.

In the if statement for division after entering a value for "b" you need to check that "b" is greater than zero or you will receive a run time error.

In general what I like to do, personal preference, is:
std::cout << "Now digit the numbers you want to calculate (A B): ";. Ending the line with (: ";) allows the "std::cin" to follow on the same line after a space. I think this looks better. Showing the user what you expect for entering the numbers is less confusing.

I noticed that in the if/else if statement with exception of division you define a variable as a double. Why? There is no advantage to storing the result in a double unless you want to use floating point numbers for your calculations. Also I would not define the variables inside the if/else if statements, but at the beginning of main with the other variables. and do not forget to initialize your variables.

At the end of the program just before the return statement I usually add this code:
1
2
3
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires heder file <limits>.
std::cout << "\n\n\n\n Press Enter to continue";
std::cin.get();

to keep the console window open before the program ends. Since you are using "cin >>" you will need to clear the input buffer before you reach the "cin.get". Otherwise the "cin.get" will extract the "\n" that is left in the input buffer.

Once the program is working these last lines can be removed if you want. Or I have wrapped these line in a "#ifndef RELEASE" and the first line of the file would be "#define RELEASE" which I would comment out until I was finished.

A few well placed blank lines makes the code easier to read.

Hope that helps,

Andy

Hello Andy,

Thank you so very much, i appreciate your tips and understand my mistakes. I'll try all of this tips, this look so more easiest and useful to code. This help a lot, thanks again Andy, i'll pratice more and more XDD.

Bruno

Topic archived. No new replies allowed.