0

Hello this is my first program with a do-while loop and its taken me a little while to get it down. I need to have the user enter 2 numbers, and raise the first number to the second number. I have finally got the coding to ask if "they would like to raise another number by a power?" and when they say yes and enter 2 new numbers the total adds the total from the first 2 numbers entered with the second set of numbers and so on. Can someone help me out with this problem? Here is the coding and a picture to help y'all out!

#include <iostream>
using namespace std;

int main()
{

    int num;
    int pow;
    int p;
    int power = 1;
    char yesno = 'y' || 'Y';

    do
    {
        cout << "Enter a number: ";
        cin >> num; "\n";
        cout << "Enter the power to raise: ";
        cin >> pow; "\n";

        for (p = 1; p <= pow; p++)
        {
            power = power * num;
        }

        cout << "The total is: " << power << endl;
        cout << "\n\n";

        cout << "Would you like to raise another number by a power? [Y/N]";
        cin >> yesno;
    } while (yesno != true);
}

Swordfish
  • 12,971
  • 3
  • 21
  • 43

3 Answers3

0

The problem of the ever-increasing answer is that power is not being reset inside the do-while loop, so the last value is being carried forward into the next loop. You need reset it at the top of the loop.

Another problem with the code is that the exit condition would never occur.

Try this instead:

int main()
{

    int num;
    int pow;
    int p;
    int power;
    char yesno;

    do
    {
        power = 1;   // <<<<<< reset power here

        cout << "Enter a number: ";
        cin >> num; "\n";
        cout << "Enter the power to raise: ";
        cin >> pow; "\n";

        for (p = 1; p <= pow; p++)
        {
            power = power * num;
        }

        cout << "The total is: " << power << endl;
        cout << "\n\n";

        cout << "Would you like to raise another number by a power? [Y/N]";
        cin >> yesno;
    } while (yesno == 'y' || yesno == 'Y');  // <<<<< test for 'yes' response
}
Ian
  • 1,221
  • 1
  • 18
  • 30
  • 1
    Addendum: `for (p = 1; p <= pow; p++)` could also be `for (p = 0; p < pow; p++)`, and since C++ is [Origin Zero](https://en.wikipedia.org/wiki/Zero-based_numbering), this notation will look less "weird" to your fellow programmers. – user4581301 Sep 28 '18 at 22:48
  • Thank you so much! I knew something was off about it but couldn't tell what it was. I appreciate it! – Keenen Owens Sep 28 '18 at 23:02
0

When you reach line } while (yesno != true); and loop back to do {, the variable power still holds the previous num^pow. You will need to assign power = 1 after do {.

Nicholas Pipitone
  • 4,002
  • 4
  • 24
  • 39
0
#include <iostream>
// you also need
#include <cmath>  // for pow()

using namespace std;

int main()
{
    // int num;  Declare variables where they're used. As locally as possible.
    // int pow;
    // int p;
    // int power = 1;
    // char yesno = 'y' || 'Y';  I don't know what you were trying to do here
                              // the expression 'y' || 'Y' will always be true
                              // and evaluate to some value different from null
                              // wich will be assigne to yesno. But with no con-
    char yesno;               // sequences since it later gets overwritten by
    do                        // cin >> yesno; There is no need to initialize
    {                         // this variable.
        cout << "Enter a number: ";
        int num;
        cin >> num; "\n";  // the statement "\n"; has no effect.

        cout << "Enter the power to raise: ";
        int pow;
        cin >> pow; "\n";  // again. no effect.

        // for (p = 1; p <= pow; p++)  as user4581301 has pointed out in the
                                    // comments it is more ... natural in C
                                    // to loop from 0 to < max:
        int power = 1;  // now its time to declare and define power ;)
        for(int p = 0; p < pow; ++p)  // notice that you can declare variables
        {                             // in the init-statement of a for-loop
            // power = power * num; shorter:
            power *= num;
        }

        cout << "The total is: " << power << /* endl; + 2 x '\n' gives: */ << "\n\n\n";
        // cout << "\n\n";

        cout << "Would you like to raise another number by a power? [Y/N]";
        cin >> yesno;
    // } while (yesno != true);  that condition will most likely always be true
                              // since the user would have a hard time to input
                              // a '\0' character, which would evaluate to false
                              // better:
       } while(yesno == 'y' || yesno == 'Y' );
}

done.

Without clutter:

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    char yesno;
    do {
        cout << "Enter a number: ";
        int num;
        cin >> num;

        cout << "Enter the power to raise: ";
        int pow;
        cin >> pow;

        int power = 1;
        for(int p = 0; p < pow; ++p)
            power *= num;

        cout << "The total is: " << power << "\n\n\n";
        cout << "Would you like to raise another number by a power? [Y/N]";
        cin >> yesno;
    } while(yesno == 'y' || yesno == 'Y' );
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43