【问题标题】:C++ total keeps going upC++ 总数持续上升
【发布时间】:2018-09-28 22:35:36
【问题描述】:

您好,这是我的第一个带有 do-while 循环的程序,我花了一点时间才把它搞定。我需要让用户输入 2 个数字,并将第一个数字提高到第二个数字。我终于得到了编码来询问“他们是否想通过幂来提高另一个数字?”当他们说是并输入 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);
}

【问题讨论】:

  • 你需要在 do-loop 中重置power
  • 无关:请与您的Rubber Duck讨论char yesno = 'y' || 'Y';
  • @user4581301 另外,想想将chartrue 进行比较意味着什么。 chars 是真的吗?
  • 几年前我会说除了 nul 之外的任何东西。可悲的是,在当今世界,true 甚至可能不是true

标签: c++ integer do-while cumulative-sum


【解决方案1】:

不断增加的答案的问题是 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
}

【讨论】:

  • 附录:for (p = 1; p &lt;= pow; p++) 也可以是 for (p = 0; p &lt; pow; p++),因为 C++ 是 Origin Zero,所以这种表示法对你的程序员同行来说不会那么“奇怪”。
  • 非常感谢!我知道它有些不对劲,但不知道是什么。我很感激!
【解决方案2】:

当您到达} while (yesno != true); 行并循环回到do { 时,变量power 仍保留之前的num^pow。您需要在do { 之后分配power = 1

【讨论】:

    【解决方案3】:
    #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' );
    }
    

    完成。

    没有杂乱:

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

    【讨论】:

      猜你喜欢
      • 2013-04-24
      • 2018-11-12
      • 2021-10-14
      • 1970-01-01
      • 2022-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      相关资源
      最近更新 更多