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
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?
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: %
I take it you would use * for multiply and / for divide.
Oh and can you use more than one if statement in a row?
You mean nested if statements? Like this:
Yes you can do this.Code:if (conditions) {
if (conditions) {
}
}
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.....
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.
If this isn't what you're asking, explain your question better.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;
that is exactly what i was needing. sorry i am very vague.
One of the problems was that I was posting using my PSP.