Hi,
I have come up with my own project, and I am stuck.
Im making a class called car, which describes a car. A car has in this system a model (ex Volvo), a registration number (ex AAA123) and speed (ex 75
km / h).
It should be possible to create a carobject by entering the model and
registration. Otherwise, a carobject will be able to:
• return the model
• return the registration
• returning their speed
• increase their speed by accelerating, the speed increase is a random
integer between 1 and 10
• reduce their speed by braking, deceleration is a
random integer between 1 and 5
• indicate whether the vehicle is stationary, if it has no speed
• return a string corresponding to the job that is displayed for each car in
examples run below
And i also made a program which is a simple variant of a race where the cars represented as a car object (of type Car).
Example:
How many cars will be in the contest? 3
Car1 - model? Volvo
Car1- registration? AAA111
Car2- model? Nissan
Car2- registration? BBB222
Car3- model? Saab
Car3- registration? CCC333
What is the target speed? 20
The competition begin!!
Press any key to continue ....
Volvo AAA111 runs in 4 km / h
Nissan BBB222 driving at 3 km / h
Saab CCC333 running at 7 km / h
Press any key to continue ....
Volvo AAA111 driving at 11 km / h
Nissan BBB222 driving at 6 km / h
Saab CCC333 driving at 10 km / h
Press any key to continue ....
Volvo AAA111 driving at 15 km / h
Nissan BBB222 driving at 13 km / h
Saab CCC333 driving at 12 km / h
Press any key to continue ....
Volvo AAA111 driving at 19 km / h
Nissan BBB222 driving at 21 km / h
Saab CCC333 driving at 15 km / h
Press any key to continue ....
and the contest is over. First up the target speed of 20 km / h, with Nissan
Registration BBB222.
***
That was the example and when the competition is completed, the cars slow down so that all cars is stationary at the end.
In my solution im using a field with carobject. The user specifies
number of cars to be included.
I have theese function protoypes in my main-file:
increaseSpeed (...) handles all n cars increase their speed (gas)
stopAllCars (...) handles all n cars reduce their speed
(brakes) until all are stationary
race done (...): determines if a car came up in the speed
set speed and if so, returns the position of the
array for this car, and otherwise returns -1.
Show cars (...) handles all the n cars presents itself
by printing
I have to have the Class in a header and a cpp file.
This is the Header:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
#pragma once
#ifndef CAR_H
#define CAR_H
#include <string>
#include <time.h>
#include <ctime>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
class Car
{
private:
string model;
string regNr;
int speed;
public:
Car();
Car(string, string, int);
~Car();
string getModel();
string getRegNr();
int getSpeed();
//int getIncreaseSpeed();
//int getDecreaseSpeed();
void print(int);
void setModel(string);
void setRegNr(string);
void setSpeed(int);
void setIncreaseSpeed(int);
void setDecreaseSpeed(int);
};
#endif
|
This is the cpp-file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#pragma once
#include "Car.h"
#include <string>
#include <time.h>
#include <ctime>
#include <sstream>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
using namespace std;
Car::Car()
{
this->model="Default";
this->regNr="XAX000";
this->speed=0000;
}
Car::Car(string model_, string regNr_, int speed_)
{
this->model=model_;
this->regNr=regNr_;
this->speed=speed_;
}
Car::~Car()
{
}
string Car::getModel()
{
return this->model;
}
string Car::getRegNr()
{
return this->regNr;
}
int Car::getSpeed()
{
return this->speed;
}
void Car::setModel(string m)
{
this->model=m;
}
void Car::setSpeed(int speed_)
{
this->speed=speed_;
}
void Car::setIncreaseSpeed(int n)
{
this ->speed += rand() % 10 +1;
}
void Car::setDecreaseSpeed(int newSpeed1)
{
//speed=rand()%1+1;
//this->speed=newSpeed1;
srand((unsigned)time(0));
this->speed=newSpeed1;
for(int i=1; i<5; i--)
{
cout<<1-(rand()%5)<<endl;
}
}
void Car::setRegNr(string r)
{
this->regNr=r;
}
void Car::print(int p)
{
}
|
The main-file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
#pragma once
#include "Car.h"
#include <time.h>
#include <ctime>
#include <sstream>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
using namespace std;
void increaseSpeed(Car cars[], int s);
void stopAllCars(Car cars[], int n);
int raceDone(Car cars[], int n, int speed);
void showCars(Car cars[], int n);
int main()
{
srand((unsigned)time(0));
Car *cars = NULL;
int nrOfCars = 0;
int speed = 0;
string regNr;
string name;
cout<<"How many cars are gonna be in the competition? ";
cin>>nrOfCars;
cars = new Car[nrOfCars];
for(int i=0; i<nrOfCars; i++)
{
cout<<"Car "<<i+1<<" - model? ";
cin>>name;
cars[i].setModel(name);
cout<<endl<<endl;
cout<<"Bil "<<i+1<<" - registrationnumber? ";
cin>>regNr;
cars[i].setRegNr(regNr);
cout<<endl<<endl;
}
cout<<"Which is the target speed? "<<endl;
cin>>speed;
cars[nrOfCars].setSpeed(speed);
cout<<endl<<endl;
cout<<"The race can start!!!!"<<endl<<endl;
system("pause");
increaseSpeed(cars, nrOfCars);
//showCars(cars, nrOfCars);
//stopAllCars(cars, rNR);
// raceDone(cars, rNr, speed);
//showCars(cars, rNr);
delete [] cars;
return 0;
}
void increaseSpeed(Car cars[], int n)
{
int counter=0;
while(counter<cars[n].getSpeed())
{
for(int i = 0; i < n; i++)
{
cars[n].setIncreaseSpeed(i);
cout<<cars[i].getModel()<<" driving in "<<cars[i].getSpeed();
cout<<endl<<endl;
counter++;;
}
system("pause");
}
}
void showCars(Car cars[], int n)
{
cars[n].print(n);
}
|
As you can see i have not called every function from the main-file.
Im stuck in increaseSpeed right now and tried solving this for days.
Can someone please help me with how i should proceed??
Thank you!