Help with arithmetic program?

Completely new to CS and I was given my first assignment yesterday. I was able to write the code to do addition (Integers only) that code is

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

int main() {
	int x;
    int y;
    int z;
    
    cin>> x;
    cin>> y;
    z=x+y;
    cout<<z;
    
	return 0;
}

As you can see, its very very simple. I could simply edit the code to make it do multiplication,subtraction,division easily. However, is there anyway I could make the functions possible in the single code? BTW, we are using ideone.com to code the program and compile and run. The input box is on the website itself.

The homework hint says
1
2
3
Given the inputs 10 and 5, your program should output 4 values: 15, 5, 50, 2. Assuming your 4 output 
values are in 4 different variables z1, z2, z3 and z4, a good way to output them to the console is 
cout << z1 << “,” << z2 << “,” << z3 << “,” << z4 << endl;


Im not really sure what that means. Im assuming that I should include

1
2
3
4
int z1;
int z2;
int z3;
int z4;
Last edited on
Replace line 8 in your program with those four lines you assumed you should include, and replace line 12 with the four math functions you need to perform. THen replace line 13 with the example line given to you.

When you get it working, post it here and I'll show you a trick. If you can't get it working then obviously I'll keep helping ;)
Hey LB. Thanks for replying. I understood your help up until on the solution for line 13.

Here is my code again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main() {
	int x;
    int y;
    int z1;
    int z2;
    int z3;
    int z4;
    
    cin>> x;
    cin>> y;
    z1=x+y; 
    z2=x-y; 
    z3=x*y; 
    z4=x/y;
   cout<<z1;
   
	return 0;
}


If I change the "1" then each arithmetic function gives the correct answer. For the line
1
2
 4 different variables z1, z2, z3 and z4, a good way to output them to the console is 
cout << z1 << “,” << z2 << “,” << z3 << “,” << z4 << endl;
Am I supposed to put something in the "," parts? Otherwise, when I put that statement in what was line 13, it got a compiling error.
Last edited on
Ah, the quotes used “ and ” need to be replaced with the normal "
Okay I think this should be right

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

int main() {
	int x;
    int y;
    int z1;
    int z2;
    int z3;
    int z4;
    
    cin>> x;
    cin>> y;
    z1=x+y; 
    z2=x-y; 
    z3=x*y; 
    z4=x/y;
   cout << z1 <<","  << z2 << "," << z3 << "," << z4 << endl;
   
	return 0;
}


What was the trick for this?
Here is the way I would write it:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>

int main()
{
    int x, y;

    std::cin >> x >> y; //trick 1

    //trick 2:
    std::cout << x+y << "," << x-y << ","
              << x*y << "," << x/y << std::endl;
    //notice there are no z, z1, z2, z3, z4 variables
}
If you want to know what to do if the user enters letters instead of numbers or of they enter 0 for y (making division fail) I can show that too.
Last edited on
I think what you are trying to say is that by not using the z variable, you can still display the value of the operation by making the code display what x+y is etc. However, what does the std:: part mean? Like I said, I am just getting my feet wet in CS. Im sure no one else in my class hasn't even started the code.
Yeah, all I'm saying is that you don't necessarily need intermediate variables. Usually they are only used for separating long equations into more logical steps.

As for the std::, I have to write that because I did not include "using namespace std;". When you are more comfortable in C++ you can search for why "using namespace std;" is bad. It's OK for beginning but ideally you should get out of the habit soon.
Ok. Thanks for the help!
Topic archived. No new replies allowed.