C++ 总量持续上升

C++ total keeps going up

你好,这是我的第一个带有 do-while 循环的程序,我花了一些时间才弄下来。我需要让用户输入 2 个数字,并将第一个数字提高到第二个数字。我终于得到了询问是否 "they would like to raise another number by a power?" 的编码,当他们说是并输入 2 个新数字时,总数将输入的前 2 个数字的总数与第二组数字相加,依此类推。有人可以帮我解决这个问题吗?这是编码和图片来帮助大家!

#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);
}

答案不断增加的问题是 power 没有在 do-while 循环中被重置,所以最后一个值被带入下一个循环。您需要在循环的顶部重置它。

代码的另一个问题是退出条件永远不会发生。

试试这个:

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
}

当您到达第 } while (yesno != true); 行并循环回到 do { 时,变量 power 仍保留前一个 num^pow。您需要在 do {.

之后分配 power = 1
#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 '[=10=]' character, which would evaluate to false
                              // better:
       } while(yesno == 'y' || yesno == 'Y' );
}

完成。

没有杂乱:

#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' );
}