Hello, I made my first program, I know its newbie and lame...
Anyway, I have a problem.
I used the techniques I learned so far.
This should calculate powers:
//
// FirstTry - the first try of shalito to create a program.
// a program that calculates power.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int nNumberofArgs, char* pszArgs[])
{
//enter a number and a power to calculate the result
cout << "This program is powering numbers.\n" << endl;
int nNumber;
int nPower;
cout << "Enter a number:";
cin >> nNumber;
cout << "Enter a Power number:";
cin >> nPower;
// declare nPower and nNumber to use the calculation formula
int nPower2;
int nNumber2;
// if the number entered is 1, it allways will be 1
if (nNumber == 1)
{
cout << "1 after powering it by "
<< nPower << ", equals: 1."
<< endl;
}
// if the Power number is 1 it will allways will be the same number
if (nPower == 1)
{
cout << nNumber
<< " after powering it by 1, equals: "
<< nNumber << "." << endl;
}
//if the number is 0, it will allways be 0.
if (nNumber == 0)
{
cout << "0 after powering it by " << nPower
<< ", equals: 0." << endl;
}
//if the power number is 0, it will allways be 1.
if (nPower == 0)
{
cout << nNumber << " after powering it by 0, equals: 1." << endl;
}
if (nPower < 0)
{
nPower2 = -1;
nNumber2 = nNumber;
while (nPower2 > nPower)
{
nNumber *= nNumber2;
nPower2--;
}
cout << nNumber2 << " after powering it by "
<< nPower << ", equlals: " << "1/"
<<nNumber << "." << endl;
}
// if not a speacial power or number is entered user this formula to calculate.
else
{
nPower2 = 1;
nNumber2 = nNumber;
while (nPower2 < nPower)
{
nNumber *= nNumber2;
nPower2++;
}
cout << nNumber2 << " after powering it by "
<< nPower << ", equlals: " << nNumber
<< "." << endl;
}
// wait until user is ready before terminating program
// to allow the user to see the program results}
system("PAUSE");
return 0;
}
The problem is, when when two 'if's are true. and it uses both's braces { }
how can i avoid that?
Like if one if is true, Thats it it will use it's braces and stop
I don't understand your question. Are you trying to keep your program going? Like say I enter 6^2 and it compleates the process and say's push any key, then exits? You don't want it to do this? I"m not sure if I can help you but I would still like to know the question please. Thank you :)
There are a couple ways you could accomplish this. Here's one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
if(nNumber == 1 && nPower == 1) // If both nNumber and nPower are equal to 1...
{
cout << "1 after powering it by 1 equals: 1" << endl; // Output this
}
elseif(nNumber == 1) // Otherwise, if nNumber is equal to 1...
{
cout << "1 after powering it by "
<< nPower << ", equals: 1."
<< endl;
}
elseif (nPower == 1) // And if nNumber wasn't equal to 1, but nPower IS equal to 1...
{
cout << nNumber
<< " after powering it by 1, equals: "
<< nNumber << "." << endl;
}
The first if statement is checking both nNumber and nPower to see if their values match. Then, it steps to the next part of the if statement, which is elseif(nNumber == 1). Else If means that if the first part of the if statement wasn't true, then the program should check for this. Then, if THAT statement isn't true, go on to the next elseif and check to see if that's true.
Using elseif makes it so the system doesn't look at that code if the first if statement is true. Otherwise, when using plain if statements, the system will go through each one separately.