在do while循环中减少问题

时间:2015-01-21 18:06:14

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

我正在使用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;

}

2 个答案:

答案 0 :(得分:0)

你写了这些

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

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

答案 1 :(得分:0)

你写了

return slowSpeed;

在Gondola.cpp

double Gondola::useBrake(int slowSpeed)

我认为你应该返回速度。