如果有资格使用所有代码,如何使用(while)循环以及将其放置在何处

时间:2018-12-02 00:40:27

标签: c++ while-loop

在c ++上,除非按-999,否则我将尝试制作一个将反复接受用户输入的程序。另外,如果输入不能被5或20整除,那么我要求它输出“必须输入5或20可以整除”。我希望继续执行直到他们输入-999为止,但是我不知道将while循环放在哪里,或者如果未输入则放入什么内容。我还不知道将“完成时输入-999离开”放在哪里,同时使它始终(而不只是开始)都符合条件。谢谢!!!

#include <iostream>
using namespace std;

int main(void)
{
    int amountEntered;
    cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << endl;
    cout << "when finished, enter -999 to leave" << endl;
    if (amountEntered == -999)
    {
        cout << "Thank you for doing business." << endl;
    }

    cin >> amountEntered;

    if (amountEntered % 20 == 0)
    {
        cout << amountEntered / 20 << endl;
    }
    else
    {
        if (amountEntered % 5 == 0)
        {
            cout << amountEntered / 5 << endl;
        }
        else
        {
            cout << "You must enter multiples of twenty or five only!" << endl;
        }
    }
    {
        while (amountEntered != -999);
        while (amountEntered % 5 == 0);
    else
    {
        if (amountEntered % 5 != 0)
        {
            cout << "You must enter multiples of twenty or five only!" << endl;
        }
    }

    while (amountEntered % 20 == 0);
    }
    if (amountEntered % 20 != 0);
    {
        cout << "You must enter a number divisible by 20 or 5!" << endl;
    }

    if (amountEntered = -999)
    {
        cout << "Thank you for doing business." << endl;
    }
}

3 个答案:

答案 0 :(得分:2)

下面是一些伪代码来说明:

while true
    get input
        if input is -999 (or other conditions)
            break out of loop
        else
            // rest of code goes here

因此,基本上,将整个内容包装在while true循环中,然后在满足某些条件时使用条件逻辑跳出循环。

答案 1 :(得分:1)

  

在c ++上,除非按-999,否则我将尝试制作一个将反复接受用户输入的程序。   另外,如果输入不能被5或20整除,那么我要求它输出“必须输入5或20可以整除”。

#include <limits>    // std::numeric_limits<>
#include <iostream>

int main()
{
    for (;;) {  // forever
        int value;
        while (std::cout << "Thou must enter a value divisible by 5 or 20. When finished enter -999 to leave.\n",
               !(std::cin >> value) || value != -999 && value % 5 != 0 && value % 20 != 0)
        //     ^^ extraction failed or value does not conform to requirements
        {
            std::cerr << "Input error :(\nYou must enter a number divisible by 5 or 20.\n";
            std::cin.clear();  // clear the flags that might have been set by a 
                               // failed input operation.
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            // ^^ discards up to the maximum value of std::streamsize characters
            //    until a newline character ('\n') is encountered. If we don't do that
            //    the next input operation will choke on the same erroneous input.
        }

        if (value == -999)
            break;

        // do sth with value
    }
}

答案 2 :(得分:0)

  

我希望这继续进行直到他们输入-999为止,但是我不知道将while循环放在哪里或者如果不输入则放入什么。我也不知道将“完成后输入-999离开”放在哪里,同时又使它在所有时间(不仅是开始时间)都符合条件。

也许您可能想将问题分解。首先,您将知道此循环的“结束条件”将是用户键入-999。因此,您希望循环看起来像

while userinput != -999
    // do something here
end while loop

有了这一点,我们所需要做的就是捕获用户输入。一个在开始,一个在while循环结束之前。

get userinput
while userinput != -999
    // do something here
    get userinput
end while loop

有几种方法可以解决此问题,这取决于您要如何设计代码。这是我的处理方法:

#include <iostream>

void Request(int& amount)
{
    std::cout << "Please enter the amount of money you would like to dispense (must be in 20's or 5's)" << std::endl;
    std::cout << "when finished, enter -999 to leave" << std::endl;
    std::cout << ">";
    std::cin >> amount;
}

int main(void)
{
    int amount = 0;

    for (Request(amount); amount != -999; Request(amount))
    {
        // Since 20 is a multiple of 5, just check if its divisble by 5 will do 
        if (amount % 5)
        {
            std::cout << "You must enter multiples of twenty or five only!" << std::endl;
            continue;
        }

        // Otherwise print out (or do stuff here)
        std::cout << amount % 5 << std::endl;
    }

    std::cout << "Thank you for doing business." << std::endl;
}