简体   繁体   English

C ++将while循环转换为do-while循环

[英]C++ Converting a while loop to a do-while loop

I see a ton a questions for converting for loops to while and do while loops, but I cant seem to find anything on converting while loops to do while loops in C++. 我看到很多关于将for循环转换为while和do while循环的问题,但是我似乎在C ++中找不到将while循环转换为while循环的任何内容。 It still needs to maintain the same function as the original code as well. 它还仍然需要保持与原始代码相同的功能。

Here is the original code: 这是原始代码:

int number, product = 1, count = 0; 

cout << "Enter a whole number to be included in the product" 
<< endl << "or enter 0 to end the input: "; 
cin >> number; 

while (number != 0) 
{
product = product * number; 
count++; 
cout << "Enter a whole number to be included in the product" 
<< endl << "or enter 0 to end the input: "; 
cin >> number; 
} 

if (count > 0) 
{ 
cout << endl << "The product is " << product << "." << endl; 
} 

By moving it around, I was able to end up here, but I keep ending up with errors. 通过四处移动,我终于可以在这里结束了,但是我一直以错误告终。 When I run the program, it prompts me to enter a while number, like expected. 当我运行程序时,它会提示我输入一个空号,如预期的那样。 If I enter a valid number, it loops and asks me the same question. 如果输入有效数字,它会循环并问我同样的问题。 Then when I enter 0, it kicks out as usual, but it displays that the product is 0 no matter the numbers entered before. 然后,当我输入0时,它会像往常一样被踢出,但无论之前输入的数字如何,它都会显示乘积为0。

Here is my attempt at adjusting it into a do while loop: 这是我尝试将其调整为do while循环的尝试:

int main()
{   

int number, product = 1, count = 0; 

do
{
    cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number; 
    product = product * number; 
    count++; 
}

while (number != 0);
{
    if (count > 0) 
    cout << endl << "The product is " << product << "." << endl; 
}
}

In the do .. while loop when you insert 0, first you multiply product by 0, than it exists. do .. while循环中,当您插入0时,首先将乘积乘以0,然后乘积不存在。 Therefore the product is always 0. 因此,乘积始终为0。

Move the product before: 移动产品之前:

int count = -1, number = 1, product = 1; 
do
{
    count++;
    product = product * number; // you can use product *= number;
    cout << "Enter a whole number to be included in the product" << endl << "or enter 0 to end the input: "; 
    cin >> number;
}    
while (number != 0);

if (count > 0) 
    cout << endl << "The product is " << product << "." << endl;

Note: my code does not use additional if and still preserve the same functionality. 注意: if并且仍然保留相同的功能,我的代码将不使用其他代码。

test number!=0 before assigning product = product*nuumber 测试编号!= 0,然后再分配产品=产品*数字

otherwise, when user enters 0, you multiply by 0 and exit the loop 否则,当用户输入0时,您乘以0并退出循环

The problem is that in the do-while version, when the user inputs 0 this line: 问题是在do-while版本中,当用户输入0时,此行:

product = product * number;

is executed, whereas in the while version not. 被执行,而在while版本中则不执行。

Because of this the product will always be 0. 因此,乘积将始终为0。

If the number is 0 just don't multiply product . 如果数字为0,则不要product

Your problem was the number input on exit is 0, which makes your whole product equal to 0. Try this logic: 您的问题是退出时输入的number为0,这会使您的整个乘积等于0。请尝试以下逻辑:

#include <iostream>

int main() {

    int number = 0, product = 1, count = 0;

    do {
        std::cout << "Enter a whole number to be included in the product" << std::endl << "or enter 0 to end the input: ";
        std::cin >> number;
        if (number > 0) {
            product = product * number;
            count++;
        }
    } while (number != 0);
    {
        if (count > 0)
            std::cout << std::endl << "The product is " << product << "." << std::endl;
    }
}

If you just want to solve the problem that you are getting a 0 as your answer, then the problem is due to the following lines 如果您只想解决答案为0的问题,则该问题归因于以下几行

cin >> number; 
product = product * number; 

you take the number which is 0 and multiply it with the product and obviously the result will be 0. 您将数字取0并将其乘以乘积,显然结果将为0。

you can fix it by putting a break statement. 您可以通过放置break语句来解决它。

cin >> number;
if(number == 0)
break;
product = product * number;

However, in general I am not sure what you are achieving by trying to solve this problem ie converting a while to a do-while. 但是,总的来说,我不确定您通过尝试解决此问题即将一会儿转换为一会儿会实现什么目标。

int number, product = 1, count = 0; 

cout << "Enter a whole number to be included in the product" 
     << endl << "or enter 0 to end the input: "; 
cin >> number; // you enter a non-0 number here

while (number != 0) // you now loop until you hit 0 ...
{
    product = product * number; 
    count++; 
    cout << "Enter a whole number to be included in the product" 
         << endl << "or enter 0 to end the input: "; 
    cin >> number; // you overwrite the non-0 number you had previously input
} 

if (count > 0) 
{ 
    cout << endl << "The product is " << product << "." << endl; 
}

Without knowing the full context of your program, I'm guessing here, but you can probably rewrite it as: 在不知道程序完整上下文的情况下,我在这里猜测,但是您可能可以将其重写为:

int number = 0, product = 1, count = 0; 

do
{
    cout << "Enter a whole number to be included in the product" 
         << endl << "or enter 0 to end the input: ";
    cin >> number;
    if (number != 0)
    {
        count++;
        product *= number;
    } 
} while (number != 0); // exit the loop when you have a non-zero entry

if (count > 0) 
{ 
    cout << endl << "The product is " << product << "." << endl; 
}

Or ... 要么 ...

int number = 0, product = 1; 

do
{
    cout << "Enter a whole number to be included in the product" 
         << endl << "or enter 0 to end the input: ";
    cin >> number;
    if (number != 0)
    {
        product *= number;
    } 
} while (number != 0); // exit the loop when you have a non-zero entry

cout << endl << "The product is " << product << "." << endl; 

You can avoid the 2nd condition since you are excluding 0 from potential inputs, and simply always output a product. 您可以避免第二种情况,因为您从潜在输入中排除了0,并且仅总是输出乘积。

You are increasing count regardless of the input you get. 无论您获得什么输入,您都在增加count First check if the input was zero (your abort input). 首先检查输入是否为零(您的中止输入)。

Change 更改

count++

to

if (number)
    count++;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM