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.

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!
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.
Ok. Thank you for your answer.
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
1
2
3
	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...

1
2
3
4
5
6
7
8
9
10
11
12
13
void increaseSpeed(Car cars[], int n)
{
	{
	for(int i = 0; i < n; i++)
	{
		cars[n].setIncreaseSpeed(i);
		cout<<cars[i].getModel()<<"  kör i "<<cars[i].getSpeed();
		cout<<endl<<endl;
		
	}
	system("pause");
	}
}
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void increaseSpeed(Car cars[], int n)
{
	int speedTotal=0;

	while(cars[n].getSpeed()<speedTotal);
	{
	for(int i = 0; i < n; i++)
	{
		cars[i].setIncreaseSpeed();
		cout<<cars[i].getModel()<<"  kör i "<<cars[i].getSpeed();
		cin>>speedTotal;
		cout<<endl<<endl;
	}
	n++;
	system("pause");
	}
}


Cpp-file
1
2
3
4
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.