I need Efficiency suggestions

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
//       FirstTry - the first try of shalito to create a program.
//       a program that calculates power.
//
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace 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 :)
else

¿how's that related to efficiency?
Sorry.
The problem is that
'if (nPower == 0)'
and
'if (nNumber == 0)'
can be entered in the same time
which leads to display both if's:
1
2
 cout << "0 after powering it by " << nPower
             << ", equals: 0." << endl;

and

cout << nNumber << " after powering it by 0, equals: 1." << endl;

and i need a tip or a technique to avoid that


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
}
else if(nNumber == 1)           // Otherwise, if nNumber is equal to 1...    
{
    cout << "1 after powering it by "
             << nPower  << ", equals: 1."
             << endl;
}
else if (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 else if(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 else if and check to see if that's true.

Using else if 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.

Hope that helps.
Wow! Thanks! It worked... i just added 'else if' to all other statements but the first one.
Its perfect! Thank you very very much!
Topic archived. No new replies allowed.