Not sure why this won't work

Hi all! New to programming. Decided to take it up as a hobby. After going through half a C++ tutorial I decided to try making a four basic operation, two value calculator. I can't seem to get it to run though. It says num isn't defined but I'm trying to use num as an user input option. Thoughts?

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
 #include <iostream>

using namespace std;

int main()
{

    cout << "Input Desired Function\n\nMultiply=1\nDivide=2\nAddition=3\nSubtraction=4\n" << endl;//Informing User about the Options
    int (num=1,2,3,4);
    cin>>num;
    //Multiply
    if (num=1){
            int y;
            int z;
            int sum;
        cout<<"Enter First Value\n";
        cin>>y;
        cout<<"Enter Second Value\n";
        cin>>z;
        sum=y*z;
        cout<<sum;

    }
    //Divide
     if (num=2){
           int y;
            int z;
            int sum;
        cout<<"Enter First Value\n";
        cin>>y;
        cout<<"Enter Second Value\n";
        cin>>z;
        sum=y/z;
        cout<<sum;

    }
    //Addition
    if (num=3){
        int y;
            int z;
            int sum;
        cout<<"Enter First Value\n";
        cin>>y;
        cout<<"Enter Second Value\n";
        cin>>z;
        sum=y+z;
        cout<<sum;

    }
    //Subtraction
    if (num=4){
         int y;
            int z;
            int sum;
        cout<<"Enter First Value\n";
        cin>>y;
        cout<<"Enter Second Value\n";
        cin>>z;
        sum=y-z;
        cout<<sum;

    }
    return 0;
A compiler says about yaur line 9:
 In function 'int main()':
9:10: error: 'num' was not declared in this scope
9:21: error: expression list treated as compound expression in functional cast [-fpermissive] 


What do you try to achieve with:
int (num=1,2,3,4);

It is not the variable declaration syntax. int num = 0; is.
Hello dracon867,

Welcome to the forum.

This is not the way to define a variable: int (num=1,2,3,4);. The () are causing a problem here and the comma operator is not working the way that you think. Even it this worked "num" would be equal to four, but just removing the () still caused compile errors for me. A more proper way to define a numeric variable is: int num{}; where the empty {}s will initialize the variable to zero, 0.0 for doubles and '\0' for a "char".

It is always a good practice and programming to initialize your variables when they are defined. From the C++11 standards on the {}s or uniform initializer are the easiest way to initialize a variable. Should you need a value other than zero just put it between the {}.

Since you are new I offer a suggestion. Be consistent in the way you write your code.

An example is you write:
1
2
int main()
{


But later with your if statements you write:
 
if (num=1){

This makes it hard to match the opening and closing brace when you go back through your code. When the {}s are in the same column and you have the proper indenting it is much easier to match the {}s or find one that is missing.

This may be just my personal preference, but I like to put the variable definitions at the beginning of a function, so I know where to find them. I might follow this something like opening a file before I start with the code of the function.

I have to test the program before I can see what it is doing. Just looking at the program I see some redundancy that should be fixed.

Hope that helps,

Andy
Hello dracon867,

When I ran the program the first thing I found is your if statements are setting "num" equal to 1, 2 ,3 and 4 as the program progresses. The '=' is setting "num" equal to whereas what you want is '==' to compare the two values.

Other than some small stuff it appears to work.

Hope that helps,

Andy
Hi Handy Andy and keskiverto!

Thanks for the warm welcome and the advice. I managed to fix it! This has been a fun hobby so far. I'll definitely try to be more consistent with my code.

dracon867
Hello dracon867,

Your program works with the changes that were pointed out.

Keeping with what you started with I rearranged the code this way. See what you think:
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
#include <iostream>

//using namespace std;  // <--- Best not to use.

int main()
{
	int num{};  // <--- Best to initialize all your variables. Some day it will be a problem.
	int y{};
	int z{};
	int sum{};

	std::cout << "Input Desired Function\n\n Multiply = 1\n Divide = 2\n Addition = 3\n Subtraction = 4" << std::endl;//Informing User about the Options <--- Added some spaces.
	std::cout << " Enter choice: ";  // <---Added this line, personal quirk, notice the ': ' and no endl.
	std::cin >> num;
	
	// <--- These lines only need to be done once. Not in each if statement.
	std::cout << "\n Enter First Value: ";
	std::cin >> y;
	std::cout << " Enter Second Value: ";
	std::cin >> z;

	//Multiply
	if (num == 1)
	{
		sum = y*z;
		std::cout << "\n " << y << " * " << z << " = " << sum << std::endl;  // <--- Same format for the others.
	}

	//Divide
	if (num == 2)
	{
		// <--- Check here that 'z' is > 0. Divide by zero is a run time error.
		sum = y / z;
		std::cout << sum;
	}

	//Addition
	if (num == 3)
	{
		sum = y + z;
		std::cout << sum;
	}

	//Subtraction
	if (num == 4)
	{
		sum = y - z;
		std::cout << sum;
	}

	return 0;
}


Read my comments in the code. They start with "// <---".

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.

This is worth reading:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#user-content-sf6-use-using-namespace-directives-for-transition-for-foundation-libraries-such-as-std-or-within-a-local-scope-only

https://stackoverflow.com/questions/4043930/is-using-namespace-like-bad

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

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

Hope that helps,

Andy

Edit:
Last edited on
looks like a good place to use switch case statements rather than if.
Topic archived. No new replies allowed.