I need help with a C++ car program. The instructions are: Write a simple program
ID: 3671266 • Letter: I
Question
I need help with a C++ car program.
The instructions are:
Write a simple program that contains the following features:
1. function main calls functions class Car containing the public method API; API calls the private methods: simulator_input, run_simulator.
2. Use a local variable myCar defined as a struct data_car declared in the method API. This struct contains: vehicle_name, terrain, speed, tank_level.
3. Method simulator_input: Ask how TOUGH the terrain is (1:easy, 2:tough, 3:really hard). Do it in a method named simulator_input. Select the average speed to drive(a:slow = minus 3%, b:medium = minus 6%, c:fast = minus 8%). Select tank level for the fuel when you start to drive (100.0; 80.00; or 50.00)
4. Use pointers to access the input values provided by the user. Print on every loop iteration the fuel level.
5. Call the method run_simulator. Inside it, put your parameters to run inside a loop that keeps running for as long as your vehicle has fuel. Every time the loop foes through, the rule applies to the tank level: tank_level = tank_level - (TOUGH * average_speed)
The code below works but basically I just want a version without exception handling and if else statements instead of switch.
#include<string>
#include<iostream>
using namespace std;
class Car {
public:
Car();
~Car();
void API();
struct data_car {
string vehicle_name = "";
int terrain = 0;
double speed = 0.0;
double tank_level = 0.0;
}myCar;
private:
void simulator_ch(data_car*);
void run_simulator(data_car*);
};
Car::Car()
{
API();
}
Car::~Car()
{
}
void Car::API()
{
simulator_ch(&myCar);
run_simulator(&myCar);
}
void Car::simulator_ch(data_car* myCar)
{
cout << "How TOUGH is the terrain?? (1:easy, 2:tough, 3:really hard)";
while (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
try {
cin >> myCar->terrain;
}
catch (exception e)
{
cout << "Invalid terrain!" << endl;
}
if (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
cout << "Please enter a valid terrain toughness";
}
}
cout << "Select the average speed to drive (1: slow, 2: medium, 3: fast)";
int ch;
while (ch != 1 && ch != 2 && ch != 3) {
try {
cin >> ch;
}
catch (exception e) {
cout << "Invalid speed!" << endl;
}
if (ch != 1 && ch != 2 && ch != 3) {
cout << "That's not an allowed speed setting, try another! ";
}
}
switch (ch) {
case 1:
myCar->speed = 0.3;
break;
case 2:
myCar->speed = 0.6;
break;
case 3:
myCar->speed = 0.8;
break;
}
cout << "Select tank level for the fuel when you start to drive (1: 100%, 2. 80%, 3. 50%) ";
int i;
while (i != 1 && i != 2 && i != 3) {
try {
cin >> i;
}
catch (exception e) {
cout << "Invalid fuel amount" << endl;
}
if (i != 1 && i != 2 && i != 3) {
cout << "Please enter a valid fuel amount";
}
}
switch (i) {
case 1:
myCar->tank_level = 100.0;
break;
case 2:
myCar->tank_level = 80.0;
break;
case 3:
myCar->tank_level = 50.0;
break;
}
}
void Car::run_simulator(data_car* myCar)
{
while (myCar->tank_level > 0.1) {
cout << "New tank level: " << myCar->tank_level << endl;
myCar->tank_level = myCar->tank_level - (myCar->terrain*myCar->speed );
}
}
int main() {
Car car;
return 0;
}
Explanation / Answer
#include<string>
#include<iostream>
using namespace std;
class Car {
public:
Car();
~Car();
void API();
struct data_car {
string vehicle_name = "";
int terrain = 0;
double speed = 0.0;
double tank_level = 0.0;
}myCar;
private:
void simulator_ch(data_car*);
void run_simulator(data_car*);
};
Car::Car()
{
API();
}
Car::~Car()
{
}
void Car::API()
{
simulator_ch(&myCar);
run_simulator(&myCar);
}
void Car::simulator_ch(data_car* myCar)
{
cout << "How TOUGH is the terrain?? (1:easy, 2:tough, 3:really hard)";
while (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
cin >> myCar->terrain;
if (myCar->terrain != 1 && myCar->terrain != 2 && myCar->terrain != 3) {
cout << "Please enter a valid terrain toughness";
}
}
cout << "Select the average speed to drive (1: slow, 2: medium, 3: fast)";
int ch;
while (ch != 1 && ch != 2 && ch != 3) {
cin >> ch;
if (ch != 1 && ch != 2 && ch != 3) {
cout << "That's not an allowed speed setting, try another! ";
}
}
if(ch == 1) {
myCar->speed = 0.3;
}
else if(ch == 2) {
myCar->speed = 0.6;
}
else if(ch == 3) {
myCar->speed = 0.8;
}
cout << "Select tank level for the fuel when you start to drive (1: 100%, 2. 80%, 3. 50%) ";
int i;
while (i != 1 && i != 2 && i != 3) {
cin >> i;
if (i != 1 && i != 2 && i != 3) {
cout << "Please enter a valid fuel amount";
}
}
if(i == 1) {
myCar->tank_level = 100.0;
}
else if(i == 2) {
myCar->tank_level = 80.0;
}
else if(i == 3) {
myCar->tank_level = 50.0;
}
}
void Car::run_simulator(data_car* myCar)
{
while (myCar->tank_level > 0.1) {
cout << "New tank level: " << myCar->tank_level << endl;
myCar->tank_level = myCar->tank_level - (myCar->terrain*myCar->speed );
}
}
int main() {
Car car;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.