Refine my Program / To find speed

0 votes
asked Nov 8, 2018 by Arivu Anukanth (220 points)
include <iostream>

#include <cmath>

#include <iomanip>

#include <string>

using namespace std;

class Time {

    

public:

void readTime(bool & errFlag);

int subtractTimes(Time t);

        

    private:

int minutes;

};

    void Time::readTime(bool & errFlag)

    {

        enum period { AM, PM } T_Period;

        int hr;

        int mn;

        char period;

errFlag = true;

if (!(cin >> hr))

return;

if (hr < 0 || hr > 12)

return;

cin >> period;

if (!(cin >> minutes))

return;

if (mn < 0 || mn > 59)

return;

cin >> period;

if (period == 'A' || period == 'a')

T_Period = AM;

else if (period == 'P' || period == 'p')

T_Period = PM;

else

return;

cin >> period;

if (period != 'M' && period != 'm')

return;

errFlag = false;

if (hr == 12)

minutes = mn;

else

minutes = hr * 60 + mn;

if (T_Period == PM)

minutes += 60 * 12;

}

int Time::subtractTimes(Time t)

{

return minutes - t.minutes;

}

int main()

{

int totalTimeTravelled(0);

double totalHourTravelled, totalSpeed;

double distance = -1;

bool valid = false;

char answer;

do {

bool errFlag;

Time startTime, stopTime;

cout << "\nEnter the start time (ex. 11:00am): ";

startTime.readTime(errFlag);

while (errFlag) {

cout << "Invalid formatted input, please try again: ";

startTime.readTime(errFlag);

}

cout << "Enter the stop time: ";

stopTime.readTime(errFlag);

while (errFlag) {

cout << "Invalid formatted input, please try again: ";

stopTime.readTime(errFlag);

}

totalTimeTravelled += stopTime.subtractTimes(startTime);

do

{

cout << "Enter the distance (mile): ";

cin >> distance;

if (cin.good())

{

valid = true;

}

else

{

cin.clear();

cout << "\t" << distance << " is not a valid distance. Try again! \n";

}

} while (!valid);

cout << "Would you like to calculate the speed? (y/n):";

cin >> answer;

totalHourTravelled = (double)totalTimeTravelled / 60;

totalSpeed = distance / totalHourTravelled;

} while (answer != 'y');

if (totalSpeed < 0)

{

cout << "Total speed of your trip shows " << setprecision(2) << fixed << totalSpeed << " mph. \n" << "However, your speed is mathematically incorrect! \n\n";

}

else

{

cout << "Total speed of your trip is " << setprecision(2) << fixed << totalSpeed << " mph. \n\n";

}

    system(pause);

return 0;

}

1 Answer

0 votes
answered Nov 9, 2018 by anonymous
Here, this is at least a little better:

#include <iostream>

#include <iomanip>

using namespace std;

class Time {

public:

void readTime();

int subtractTimes(Time t);

private:

struct mytime { int hr; int mn; } x;

};

void Time::readTime()

{

char period;

do

{

cout << "Enter an hour: ";

cin >> x.hr;

} while (x.hr < 0 || x.hr > 12);

do

{

cout << "Enter minutes: ";

cin >> x.mn;

} while (x.mn < 0 || x.mn > 59);

do

{

cout << "Enter a period: ";

cin >> period;

cout << endl;

} while (!(period == 'P' || period == 'p' || period == 'A' || period == 'a'));

if (period == 'P' || period == 'p')

x.hr = x.hr + 12;

x.mn += x.hr*60;

}

int Time::subtractTimes(Time t)

{

return x.mn - t.x.mn;

}

int main()

{

double totalSpeed = -1;

double distance = -1;

char answer;

Time startTime, stopTime;

do

{

cout << "\n-- Start time -- \n";

startTime.readTime();

cout << "-- Stop time -- \n";

stopTime.readTime();

do

{

cout << "\nPlease input the distance (miles): ";

cin >> distance;

} while (distance < 0);

totalSpeed = distance * 60 / stopTime.subtractTimes(startTime);

if (totalSpeed < 0) totalSpeed *= -1;

cout << "Total speed of your trip is " << setprecision(2) << fixed << totalSpeed << " mph. \n\n";

do

{

cout << "Would you like to continue? (y/n): ";

cin >> answer;

} while (!(answer == 'y' || answer == 'n'));

} while (answer == 'y');

cout << "Goodbye!" << endl;

return 0;

}
commented Nov 9, 2018 by kethan kumar (100 points)
y thi s much big
try it small;;
commented Nov 13, 2018 by Arivu Anukanth (220 points)
Need to use certain functions regardless, maintaining that how can someone make it shorter?
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...