I am trying to make a program where I ask the user to input data about an animal and then it works out how likely they are to take over the world. How do you make a C++ Program do any sort of calculation: Multiply, Divide, Add, Subtract?

Printable View

- 11-08-2005bumfluffCalculations
I am trying to make a program where I ask the user to input data about an animal and then it works out how likely they are to take over the world. How do you make a C++ Program do any sort of calculation: Multiply, Divide, Add, Subtract?

- 11-08-2005SlyMaelstrom
All mathematical operators are standard in C and C++, just use them in your code.

Code:`int a = 5;`

int b = 2;

int c = a + b; // This will equal 7

// Standard operators are:

// for addition: +

// for subtraction: -

// for multiplication: *

// for division: /

// for modulus: %

- 11-08-2005bumfluff
I take it you would use * for multiply and / for divide.

Oh and can you use more than one if statement in a row? - 11-08-2005SlyMaelstrom
You mean nested if statements? Like this:

Code:`if (conditions) {`

if (conditions) {

}

}

- 11-09-2005bumfluff
I know this is a very dum question but how do you show theanswer. I want to have it all in the sam line, The answer is.....

- 11-09-2005SlyMaelstrom
I'm having trouble with your questions. They're extremely vague.

Ok, so you want to show the answer to an equation in output and you want it on the same line as text. A cout statement can accept any number of variables and constants, you just seperate each with "put to" operators and end the statement (like all statements) with a semicolon.

Code:`int x = 5;`

int y = 6;

int z = x + y;

cout << x << " + " << y << " = " << z << endl;

*/ or if you prefer */

cout << "The answer is " << z << endl;

- 11-09-2005bumfluff
that is exactly what i was needing. sorry i am very vague.

- 11-09-2005bumfluff
One of the problems was that I was posting using my PSP.