Just started learning c++ thoughts on my first program?

I'm wondering if anyone can tell me if this is okay for the very first program I've coded, besides a simple "hello world" thing lol.
Its a very simple calculator. so no hate :p
If there is anything I could have done better Id like to hear what I can do better!
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
47
48
49
50
51
52
53
54
55
  #include "stdafx.h"
#include <iostream>

using namespace std;

class calculator {
	public: 
		void getUserInfo() {
			int x;
			int y;
			int answer;
			int op;
			
			cout << "Please enter a number: ";
			cin >> x;

			cout << "enter another number: ";
			cin >> y;

			cout << "Chose a operator(1 = +, 2 = -, 3 = *, 4 = /)";
			cin >> op;

			if (op == 1) {
				op = x + y;
			}

			if (op == 2) {
				op = x - y;
			}

			if (op == 3) {
				op = x * y;
			}
			if (op == 4) {
				op = x / y;
			}
			//Changing the variable 'answer' to op so answer turns into 'x +- y'
			answer = op;

			cout << "Your answer is: " << answer << endl;		
		}

};


int main()
{
	calculator calc;

	calc.getUserInfo();

    return 0;
}

Last edited on
What happens on line 35 when y is 0 ?
line 25 - what happens if a number less than 1 or greater than 4 is entered or a letter is entered ?
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>

namespace calculator {

    void run() {

        int x;
        int y;

        std::cout << "Please enter a number: ";
        std::cin >> x;

        std::cout << "enter another number: ";
        std::cin >> y;

        char op ;
        std::cout << "Chose an operator (+-*/): ";
        std::cin >> op;

        int answer = 0 ;

        if( op == '+' ) answer = x+y ;
        else if( op == '-' ) answer = x-y ;
        else if( op == '*' ) answer = x*y ;
        else if( op == '/' ) {

            if( y != 0 ) std::cout << x << " / " << y << " == " << double(x) / y << '\n' ;
            else std::cout << "division by zero!\n" ;

            return ;
        }

        else {

            std::cout << "invalid operator '" << op << "'\n" ;
            return ;
        }

        std::cout << x << ' ' << op << ' ' << y << " == "  << answer << '\n' ;
    }
}

int main() {

	calculator::run();
}
Hello baller56,

Welcome to the forum and thank you for using "code" tags.

Looking over your program the class is not really needed tha way you have it written. Here is a suggestion that is a more accepted way:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//  This is usually put in its own .h file.
class Calculator
{
public:
	Calculator(); // <--- ctor (constructor).
	~Calculator();  // <--- dtor (destructor).
	void getUserInfo();

private:
	int x;
	int y;
};

// This is usually put in its own .cpp file.
Calculator::Calculator()
{
	//  Initalizes the variables when the ctor (constructor) creates the class
	x = 0;
	y = 0;
}

Calculator::~Calculator()
{
}

void Calculator::getUserInfo()
{
	int answer{ 0 };  //<--- Always initialize your variables.
	int op{ 0 };

	std::cout << "Please enter a number: ";
	std::cin >> x;  // <--- Uses the class variable x.

	std::cout << "enter another number: ";
	std::cin >> y; //<--- Uses the class variable y.

	std::cout << "Chose a operator(1 = +, 2 = -, 3 = *, 4 = /)";
	std::cin >> op;

	// <--- Should validate op here before continuing. Per alonso12 suggestion.

	if (op == 1)
	{
		answer = x + y;  // <--- "answer" is a better choice here. What happens when "op" would end up with 2, 3 or 4?
	}

	if (op == 2)
	{
		answer = x - y;
	}

	if (op == 3)
	{
		answer = x * y;
	}

	// <--- Watch for divide by 0 errors. Per Thomas1965 suggestion.
	if (op == 4)
	{
		// Code to check for "y" being > 0.
		answer = x / y;
	}
	//Changing the variable 'answer' to op so answer turns into 'x +- y'
	//answer = op;  // <--- Not needed now.

	std::cout << "Your answer is: " << answer << std::endl;
}

int main()
{
	Calculator calc;

	calc.getUserInfo();

	return 0;
}


As you have written the class it is mostly useless and the class is not really need because the "getUserInfo" function could be a stand alone regular function.

In the "getUserInfo" function I would have used a "switch" in place of the "if" statements. Also be sure to initialize you variables when you define them, it is just good programming practice.

Line 4 "using namespace std;" is not a good idea. It can be used in a simple program like this, but in the future it will cause problems. This link will give you some understanding:

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Also you could do a search here for some good information.

"main" will work as is. You are likely to find with VS that the console window will close to soon when the program is finished. Check this for some ideas:

http://www.cplusplus.com/forum/beginner/1988/

If you have any questions about VS send me a PM and I will help all I can.

Hope this helps,

Andy
Thank you all for the great feedback!
What happens on line 35 when y is 0 ?

I'm going to make a fix to this, I have something in mind that might work.

line 25 - what happens if a number less than 1 or greater than 4 is entered or a letter is entered ?

I'm going to fix this as well now, thank you for the feedback! I appreciate it!

As you have written the class it is mostly useless and the class is not really need because the "getUserInfo" function could be a stand alone regular function.

In the "getUserInfo" function I would have used a "switch" in place of the "if" statements. Also be sure to initialize you variables when you define them, it is just good programming practice.

Line 4 "using namespace std;" is not a good idea. It can be used in a simple program like this, but in the future it will cause problems. This link will give you some understanding:

http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/

Also you could do a search here for some good information.

"main" will work as is. You are likely to find with VS that the console window will close to soon when the program is finished. Check this for some ideas:

http://www.cplusplus.com/forum/beginner/1988/

If you have any questions about VS send me a PM and I will help all I can.

Hope this helps,

Andy


I started learning c++ like a week ago, I'm like very new to this, never coded in any other language.

I put a class for the function because I just started learning about classes and objects it was intended for a little practice lol.

You put something in your code called a constructor and deconstructor, im not quite sure what that is yet. But I look forward to learning it! I appreciate the feedback! Thank you alot for taking the time to read my code.


Im going to work on the program now and make it better with all this feedback.
Topic archived. No new replies allowed.