【问题标题】:Issue decrementing in a do while loop在 do while 循环中发出递减问题
【发布时间】:2015-01-21 18:06:14
【问题描述】:

我在使用 do while 循环进行递减时遇到问题。一切都被分解成单独的文件,以保持类和方法/函数分开(以及保持主要清晰)。

我遇到的问题是它递减一次,但是当它循环返回时,它会立即回到相同的值并递减到相同的结果。我知道这可能是我做的一些愚蠢的事情,但我还没有注意到!

具体来说,我遇到的错误是当我调用 useBrake 方法时。

Gondola.h

#ifndef _GONDOLA_CLASS
#define _GONDOLA_CLASS

class Gondola
{

private:
    double load; // load <= 500
    double speed; // Speed <= 20
    double condition; // 0-100

public:

    //Constructors
    Gondola();
    Gondola(double ld, double spd, double cnd);

    //Accessor methods (getters)
    double getLoad() const;
    double getSpeed() const;
    double getCondition() const;

    //Mutator Methods methods (setters)
    bool setLoad(double ld);
    bool setSpeed(double spd);
    bool setCondition(double cnd);

    //Other Methods
    double useBrake(int slowSpeed);

};

#endif

Gondola.cpp

//Accessor and mutator methods
#include <iostream>
#include "Gondola.h"

using namespace std;

//Constructors

Gondola::Gondola()
{
    load = 0;
    speed = 0;
    condition = 100;
}

Gondola::Gondola(double ld, double spd, double condit)
{
    if(!setLoad(ld))
    {
        load = 500;
    }

    if(!setSpeed(spd))
    {
        speed = 20;
    }

    if(!setCondition(condit))
    {
        condition = 100;
    }

}

//Accessor Methods

double Gondola::getLoad() const
{
    return load;
}

double Gondola::getSpeed() const
{
    return speed;
}

double Gondola::getCondition() const
{
    return condition;
}

//Mutator Methods

bool Gondola::setLoad(double ld)
{
    bool validLoad = (ld > 0) && (ld <= 500);
    if(validLoad)
    {
        load = ld;
    }

    return validLoad;
}

bool Gondola::setSpeed(double spd)
{
    bool validSpeed = (spd > 0) && (spd <= 20);
    if(validSpeed)
    {
        speed = spd;
    }

    return validSpeed;
}


bool Gondola::setCondition(double cnd)
{
    bool validCondition = (cnd > 0) && (cnd <= 100);
    if(validCondition)
    {
        condition = cnd;
    }

    return validCondition;
}

//Other Functions

double Gondola::useBrake(int slowSpeed)
{
    char userResponse = ' ';
    bool validInput = false;
    int newSpeed = 0;

    do
    {
        cout << "Do you want to use the brake?" << endl;
        cout << "Enter y for 'yes' or n for 'no'" << endl;
        cin >> userResponse;
        validInput = (userResponse == 'y') || (userResponse == 'n');

        if(!validInput)
        {
            cout << "Invalid response. Please try again!" << endl;
        }

        if(userResponse == 'y')
        {
            newSpeed = (slowSpeed - 5);
            speed = newSpeed;
        }
    }while(!validInput);

    return slowSpeed;
}

bool continueBraking()                                                   // Asks the user if they want to continue. If yes, the braking loop continues. If no, the program continues
{

    char userResponse = ' ';
    bool validInput = false;

    do
    {
        cout << endl;
        cout << "Do you wish to continue braking?" << endl;
        cout << "Enter y for 'yes' or n for 'no'" << endl;
        cin >> userResponse;
        validInput = (userResponse == 'y') || (userResponse == 'n');
        if (!validInput)
        {
            cout << "Invalid response. Please try again!" << endl;
        }
    } while (!validInput);
    return(userResponse == 'y');
}

bool askToContinue()                                                         // Asks the user if they want to continue. If yes, the loop restarts. If no, the program exits.
{

    char userResponse = ' ';
    bool validInput = false;

    do
    {
        cout << endl;
        cout << "Do you wish to continue?" << endl;
        cout << "Enter y for 'yes' or n for 'no'" << endl;
        cin >> userResponse;
        validInput = (userResponse == 'y') || (userResponse == 'n');
        if (!validInput)
        {
            cout << "Invalid response. Please try again!" << endl;
        }
    } while (!validInput);
    return(userResponse == 'y');
}

Main.cpp

/*

    Paul Christopher
    Skill 2.2
    Description: Program creates gondola object and then the user
                 enters in the load, speed, and condition. The program
                 also has methods to dump and use the brake to slow 
                 the speed of the gondola by 5 kph.

*/

//main program code

#include <iostream>
#include "Gondola.h"

using namespace std;


bool continueBraking();
bool askToContinue();


int main()
{

    //Variables
    double gondolaLoad = 0.0;
    double gondolaSpeed = 0.0;
    double gondolaCondition = 0.0;

    do
    {
        //Object Declaration
        Gondola gondola;

        //Change variables
        cout << "Enter a load size for this gondola (1-500): " << endl;
        cin >> gondolaLoad;
        gondola.setLoad(gondolaLoad);

        cout << "Enter a speed for this gondola (<=20): " << endl;
        cin >> gondolaSpeed;
        gondola.setSpeed(gondolaSpeed);

        cout << "Enter a condition for this gondola (1-100): " << endl;
        cin >> gondolaCondition;
        gondola.setCondition(gondolaCondition);

        //New Output
        cout << "Gondola's new load, speed, and condition: " << endl; 
        cout << gondola.getLoad() << endl;
        cout << gondola.getSpeed() << endl;
        cout << gondola.getCondition() << endl;

        do
        {
            gondola.useBrake(gondolaSpeed);
            cout << "Gondola's new speed: " << gondola.getSpeed() << endl;

        }while(continueBraking());

        cout << "Gondola's new speed: " << gondola.getSpeed() << endl;

    }while(askToContinue());

    system("PAUSE");
    return 0;

}

【问题讨论】:

  • 我看到三个 do-while 循环。哪个是您遇到的问题?
  • 你说的是newSpeed = (slowSpeed - 5);吗?每次循环都将其设置为相同的值,因为slowSpeed 永远不会改变。
  • 你有什么结果,你期望什么结果?给出您在加载速度和条件中输入的一组值。
  • 流的常见错误用法:在测试提取值之前没有测试成功提取(可能没有变化)
  • @Barmar 是的,我说的是那部分。您可以输入的最大速度是 20,所以当它递减时,它会减去 5(制动量)得到 15。不确定我是否必须让它循环分配,但这是我想添加到 '加倍努力”(另外它有助于我理解事物的运作方式)。

标签: c++ loops methods do-while decrement


【解决方案1】:

这些是你写的

double getLoad() const;
double getSpeed() const;
double getCondition() const; 

所以当你在 do-while 循环中调用这些时,一旦你之前通过,返回值就不会改变。

【讨论】:

  • 实际上通过删除 const 仍然没有工作。仍然只去15然后停止。我认为错误在于我使用制动方法的循环。我会玩弄那个
【解决方案2】:

你写的

return slowSpeed;

在 Gondola.cpp 中

double Gondola::useBrake(int slowSpeed)

我认为你应该返回速度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多