# Thread: brand new to c++ and having a problem wit some experimental coding

1. ## brand new to c++ and having a problem wit some experimental coding

im trying to make a program which lets you input 2 numbers and then select what to do to them (add subract or multiply). and as i have been reading the beginers tutorials on this site have been changing how i am doing it. currently i have it set up with 3 prototypes and some if rules to make each action work. it it however only registering the first if rule for some reason and no matter what numbers i input it always adds the 2 numbers. if any1 is willing to look at my coding coudl you pls post here so i can send it to you, or should i just put my code on a post?

thanks

2. ill post the code here to see if any1 can quickly see anything.
just to warn you i am literally brand new to c++ (as in started today) and so any advice on how to inprove my methods would be appreciated.

Code:
```#include <iostream>

using namespace std;

int sub (int num1,int num2);
int mult(int num1,int num2);

int main()
{
int num1, num2, num3, num4;

cin>> num1;

cout<<"and another number: ";
cin>>num2;

cin>>num3;
cin.ignore();

if ( num3 = 1) {
num4=num1+num2;
}

else if ( num3 = 2) {
num4=num1-num2;
}

else if ( num3 = 3 ) {
num4=num1*num2;
}

num4 = num1+num2;

cin.get();
}

{
return num1+num2;
}

int sub (int num1, int num2)
{
return num1-num2;
};

int mult (int num1, int num2)
{
return num1*num2;
}```

3. if(something = nothing)

What you are doing there is not checking if something equals nothing, you are making something equal nothing.

One equals sign "=" put what is on the right into the left.

Two equals signs "==" means check if what is on the left is equal to what is on the right.

if(something == nothing)

4. use == instead of = to test for equality. = is the C assignment operator, and == tests for equality and returns true if equal and false if not. otherwise the rest of your program looks ok.

edit: looks like someone beat me to it.

5. :P thanks. ill try to remember that one. it seems to work fine now.

6. just 1 more thing, would it be simpler to do this with switches? (i just got to tutorial 5 :P)

7. > just 1 more thing, would it be simpler to do this with switches?
Try it and see.
There's always another way, feel free to experiment with different solution ideas to the same problem.

8. in your case a switch is un-necessary IMO. you are not testing alot of expressions, meaning the else if is probably a better bet. although yes, once you go testing lots of expressions it is better to use a switch to make things clearer (several nested ifs can get cryptic and hard to follow) and more compact.

will also tell you for future use that the test:

if(!something)

is the equivalent of:

if(something == 0)

not important which one you use (no real difference in efficiency), but I'm telling you this just incase you see the ! conditional in use and wonder what it is. just like this:

something == 0 ? something++ : something--;

is the equivalent of:

if(something == 0)
something++;
else
something--;

just some C stuff that the tutorial doesn't tell you about if else.

9. hmm, i should probably know this but what do the ++, --, ? and : do ?

10. increment and decrement opperators: add one to number (++) minus one from number (--); they can be used before or after a variable name, like this:

something++;

or this

++something;

there position is only relevent when used in a loop's operation (i.e. the third statement in the for loop), and specifiy whether you want to increment/decrement before the loop starts or at the end of a loop. standalone as lines of code, it doesnt matter which position.

11. Ok,

Code:
`a++;`
Returns the value of a, then adds one.

So this will type in "yesyes"

Code:
```a=2;
if(a++==2)
cout << "yes";
if(a==3)
cout << "yes";```
However, if you apply the ++'s to the end you get only "yes"

Code:
```a=2;
if(++a==2)
cout << "yes";
if(a==3)
cout << "yes";```
Because:
Code:
`++a;`
returns a's value + 1.

Same idea with a-- and --a.
== Test for equity,
= set to value.

They're a bunch of other operators like <<, >>, and other things, but the most commonly used are ++', --', ==', and =.

Have fun programming .

12. > if(a++=2)
wow
Increment and assignment....

13. You could give your numbers more descriptive names. Like, method instead of num3, result instead of num4, ...

14. lol i just do that cos there is no way for me to get confused. i cant confuse num1 with num2 in a small program like this but im sure i will have to start making better names if i make any larger programs.

15. Originally Posted by Richie T
there position is only relevent when used in a loop's operation (i.e. the third statement in the for loop), and specifiy whether you want to increment/decrement before the loop starts or at the end of a loop. standalone as lines of code, it doesnt matter which position.
Using pre-increment (++x) vs post-increment (x++) doesn't affect behaviour of a loop as such.
Code:
```for (int i = 0; i < some_max; ++i)
{
// whatever
}```
achieves the same as
Code:
```for (int i = 0; i < some_max; i++)
{
// whatever
}```
with modern compilers. Technically there is a subtle difference (the second form can be slower due to need to hold a temporary) but modern compilers are able to recognise the fact there is no net difference, and optimise the difference out.

Where pre-increment and post-increment are different is if the results of the expression are stored or used. So, for example;
Code:
```#include <iostream>

void something(int x)
{
std::cout << x << ',';
}

int main()
{
int value = 5;
something(++value);
std::cout << value << ',';

value = 5;    // reset
something(value++);
std::cout << value << '\n';
}```
will print the values 6,6,5,6 where the first and third values printed are the results of the different increment operators, and the second and third values output are the actual value in main().