简单的 C++ while 循环

Simple C++ while loop

我在解决我的 c++ 书中的这个问题时遇到了问题,而且没有很好的例子。

Write a code that lets users enter a number. The number should be multiplied by 2 and printed until the number exceeds 50. Use a while loop.

我想我应该 post 我的尝试,虽然我知道它还远未准备好:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";

    multiply = number * 2;

    while (number <= 50)
    {

        cin >> number;
        cout << multiply;
    }
}

需要添加或删除什么?也许有人有 link 类似的源代码?

随意更改变量名称或任何内容。请在否决之前要求更改或更多信息。谢谢。

这应该有效。根据你的问题,输入时数字需要加倍,所以你只需要在循环内移动你的乘法,这样就有一个数字到 x2。否则,到那时 'number' 没有价值。

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int number, multiply=1;

    cout << "Enter a number: ";
    cin >> number;
    multiply = number * 2;

    while (multiply<= 50)
    {

        cout << multiply;
        multiply*=2;
    }

return;
}
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int number;

    cout << "Enter a number: ";

   cin >> number;

    while (number <= 50)
    {
        cout << number
         number *= 2;
    }
}