# Thread: Help out a complete beginner

1. ## Help out a complete beginner

I started reading the using C++ tutorials a couple days ago, and have had no experience programming in any language before that. I'm attempting to write a very simple program that lets me choose between multiplication and division, then do that function to two numbers. what I have so far is this:
Code:
#include <iostream>

using namespace std;

int mult ( int x, int y );

int function; //the variable "function" is what chooses between multiply and divide
int main()
{
cout<<"enter 1 for multiplication, 2 for division.";
cin>>function; //my trouble is that it uses multiply regardless of what number you enter for "function"
if (function = 1){
int x;
int y;

cout<<"Please input two numbers to be multiplied: ";
cin>> x >> y;
cin.ignore();
cout<<"The product of your two numbers is "<< (x * y) <<"\n";
cin.get();
}
else if (function = 2) {
int a;
int b;
cout<<"Please input two numbers to be divided: ";
cin>> a >> b;
cin.ignore();
cout<<"the two numbers divided is " << (a / b) <<"\n";
cin.get();
}
}
can you guys help me out? it works for multiplying, but I can't get it to go to the second string for "if (function = 2). Any other code-writing tips would be helpful as well. thank you very much!

2. to check for equality use two equal signs == one equal sign means you are assigning the variable a value.
integers are whole numbers. When you divide, you will need to use a variable of type double or float if you want to store the decimal fraction.

don't forget to check for errors like divide by zero

3. Use == for comparing.

4. Aside from the integer division thing that you are facing...
Code:
if (function = 1){
The code above assigns 1 to function, and then since 1 evaluates to true, the expression is true and so control passes to the code block that follows. What you probably meant to write is:
Code:
if (function == 1){
Also, you should declare function within the main() function.

5. Other than what everyone else said you also didn't use this line of code. it isn't really necessary to change but you can if you want
Originally Posted by Jmcrofts
int mult ( int x, int y );

6. Thanks a lot everyone for your help, it seems to be working fine now.

Originally Posted by tbca
Other than what everyone else said you also didn't use this line of code. it isn't really necessary to change but you can if you want
What does that code do? I saw it in the tutorials, but never really understood what it was for.

7. It is the prototype for a function named mult that takes two arguments, each an int, and returns an int. The tutorial on functions should say more about it.

8. it's needed if you write your function after code that uses it. For single file projects, you don't really need it, but it comes in handy when you're working on larger projects.