### Problem with calling function in my class

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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````#pragma once #ifndef CAR_H #define CAR_H #include #include #include #include #include #include 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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#pragma once #include "Car.h" #include #include #include #include #include #include #include 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)<regNr=r; } void Car::print(int p) { } ``````

The main-file:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687`` ``````#pragma once #include "Car.h" #include #include #include #include #include #include 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>name; cars[i].setModel(name); cout<>regNr; cars[i].setRegNr(regNr); cout<>speed; cars[nrOfCars].setSpeed(speed); cout<

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.

Thank you!
Do one thing, and do it right.
`increaseSpeed()' should only traverse the array and increase every car speed. That's it.
It should not print anything,

`while(counter<cars[n].getSpeed())` not sure what you want with that. `cars[n]' is out of bounds.

Also, you don't use the parameter in `Car::setIncreaseSpeed()'
and `Car::setDecreaseSpeed()' makes no sense.
But if i want to print out as the example... which car model that accelerates, i thought i had to put the cout<< in main file were the function is. Should i have that in the cpp file in setIncreaseSpeed()?

I thought that the while loop would help me with keeping track of the car objects.
> But if i want to print out as the example... which car model that accelerates, i thought i had to put the
> cout<< in main file were the function is. Should i have that in the cpp file in setIncreaseSpeed()?
¿what?
If you want to print the cars make a `print()' function that traverse the array and prints the cars.

By the way
 ``123`` `````` for(int i = 0; i < n; i++) { cars[n].setIncreaseSpeed(i);``````
you are modifying always the same car. The one that does not exist, as `cars[n]' is out of bounds
Last edited on
aa okey. so I should use my print function with that text and call it everytime I increase the speed?

And I suspected that i did something wrong with that, thats why i tried the while loop. But i cant figure out how to solve it so that it iterate threw every car...

 ``12345678910111213`` ``````void increaseSpeed(Car cars[], int n) { { for(int i = 0; i < n; i++) { cars[n].setIncreaseSpeed(i); cout<
Now i have put it in a while loop to loop thrue the cars.
But it still crashes, ive tried alot of different stuff but i cant get it right. Someone that can help me? Please!

Main-file
 ``1234567891011121314151617`` ``````void increaseSpeed(Car cars[], int n) { int speedTotal=0; while(cars[n].getSpeed()>speedTotal; cout<

Cpp-file
 ``1234`` ``````void Car::setIncreaseSpeed() { this->speed += rand() % 10 +1; }``````

How can i move on to get it right? I want it to keep going until someone reached the speed target and then move on from that function.
Thank you!
At risk of repeating what ne555 said - `cars[n]` is out of bounds. Even before you start incrementing n at line 14 of your posted snippet.

Edit: And I don't understand your loop-within-a-loop. n is the total number of cars in your vector. Why are you incrementing it? Is your while loop actually supposed to be iterating over each item in the `cars `array? If so, why do you have a for loop inside it which also iterates over each member of that array?
Last edited on
Thank you MikeyBoy...I think I got it right now. Its good to repeat :)

So now I only have one problem, I wanna return the position of the car int he array that has won the race. It will return from int raceDone function in main-file. But I cant get it work because i dont now what do with the do-while loop so i break out when i get the right value. If no one has come up to the target speed it will return -1. Heres the code in pastebin.
Hope someone can help, really want it to be right

http://pastebin.com/ciSiFrT4
Topic archived. No new replies allowed.