Проблема уменьшается в цикле do while

У меня проблема с использованием цикла 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;

}

-4

Решение

ты написал это

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

поэтому, когда вы вызываете их в цикле do-while, возвращаемые значения не изменятся, как только вы пройдете раньше.

0

Другие решения

вы написали

return slowSpeed;

в Gondola.cpp

double Gondola::useBrake(int slowSpeed)

Вы должны вернуть скорость вместо этого, я думаю.

0

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector