Im new to C++

Im trying to write a program that lists two Airplanes speed and distance for every second leading up until the first plane crosses a 10km distance. Am I even close?

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
////////////////////////////////////////////
////////////////////////////////////////////
float ShowData(float time, float s1, float d1, float s2, float d2);
class Airplane{
private:
float Accel;
float distance;
float speed;
float t;
float distanceConversion = 1/1000;
float speedConversion = 1/3600;
public:
Airplane(){
float s1 = getSpeed();
float s2 = getSpeed();
float d1 = getDistance();
float d2 = getDistance();
cout<< setw(10)<< s1<< setw(10)<< d1<< setw(10)<< s2<< setw(10)<< d2<< endl;
}

void setTime(float);
void setAccel(float);
void setSpeed(float);
void setDistance(float);
float getTime();
float getAccel();
float getSpeed();
float getDistance();
float ShowData(float &t, float, float, float, float);
void Run();
};
////////////////////////////////////////////
////////////////////////////////////////////


void Airplane::setTime(float time){
for((time= 0); t=0; time++)

t = time;
}
void Airplane::setAccel(float accel){
accel = rand()%25 + 1;
Accel = accel;
};

void Airplane::setSpeed(float Speed){
speed = Speed;}

void Airplane::setDistance(float Distance){
distance = Distance;}

float Airplane::getAccel(){
return Accel;}

float Airplane::getTime(){
return t;}

float Airplane::getSpeed(){
return speed;}

float Airplane::getDistance(){
return distance;}


///////////////////////////////////////////
///////////////////////////////////////////
int main(){
Airplane Airplane1, Airplane2;
unsigned seed;
seed = time(0);
srand(seed);
float Distance;
float Speed;
float Acceleration;
float time;

Airplane1.setAccel(Acceleration);
Airplane2.setAccel(Acceleration);
Airplane1.setTime(time);
Airplane2.setTime(time);
Airplane1.setSpeed(Speed);
Airplane2.setSpeed(Speed);
Airplane1.setDistance(Distance);
Airplane2.setDistance(Distance);
///////////////////////////////////////////
Airplane1.getAccel();
Airplane2.getAccel();
Airplane1.getTime();
Airplane2.getTime();
Airplane1.getSpeed();
Airplane2.getSpeed();
Airplane1.getDistance();
Airplane2.getDistance();
///////////////////////////////////////////
time = Airplane1.getTime();
float s1 = Airplane1.getSpeed();
float s2 = Airplane2.getSpeed();
float d1 = Airplane1.getDistance();
float d2 = Airplane2.getDistance();



while(time >=0);
if(d1 < 10 && d2 < 10)
for(time > 0; time= 0; time+= time){
cout<< time;
}
else{cout<< time;}
return 0;
//{ShowData(float Airplane1.getTime, Airplane1.getSpeed(), Airplane1.getDistance(), Airplane2.getSpeed(), Airplane2.getDistance());}
;

}

/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
/*
void Run(){

};
////////////////////////////////////////////////////////////////////*/
float ShowData(float &time, float s1,float d1,float s2,float d2 ){
static float t;
s1, s2;
d1, d2;
cout<<setw(15) <<"Airplane 1" <<setw(5) <<"" <<setw(11)<<"Airplane 2" << endl;
cout<< "Time"<< setw(5)<< "Km/h"<< setw(5)<< "Km"<< setw(6) <<"" <<setw(5)<< "Km/h"<< setw(5)<< "Km"<< endl;

do{
for(time >=0; d1 < 10 || d2 < 10; time++){
cout<< setw(10)<< s1<< setw(10)<< d1<< setw(10)<< s2<< setw(10)<< d2<< endl;}
return 0;
}while(d1 >0 && d2 >0);}
This is a lot of mess. I will help you best as I can.

1. Always comment on your code. I couldn't tell until my compiler told me where your errors are. It helps me/someone help solve your problem.

2. There is a little format thing that will help with your code. Look to your write as you type and you will see this symbol (<>). Double click it. Then, in between the code comments put your code. // like this

3. Write down your problem in a logical order. It helps more than you would think.

3. I was able to find your errors. I will explain in your program in a while.

I need you to answer these. This seems negative but it will help me and you figure out this problem together.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;

float ShowData(float time, float s1, float d1, float s2, float d2);

class Airplane
{
private:
	float Accel;
	float distance;
	float speed;
	float t;
	float distanceConversion = 1 / 1000;
	float speedConversion = 1 / 3600;
public:
	Airplane()
	{
		float s1 = getSpeed();
		float s2 = getSpeed();
		float d1 = getDistance();
		float d2 = getDistance();
		cout << setw(10) << s1 << setw(10) << d1 << setw(10) << s2 << setw(10) << d2 << endl;
	}

	void setTime(float);
	void setAccel(float);
	void setSpeed(float);
	void setDistance(float);
	float getTime();
	float getAccel();
	float getSpeed();
	float getDistance();
	float ShowData(float &t, float, float, float, float);
	void Run();
};

void Airplane::setTime(float time)
{
	for ((time == 0); t == 0; time++) 
	// This doesn't make sense because of your assigment. Do you want to assign time as 0 or have it equal 0? use = for assignment and == for equal.  
	//What is the function suppose to do? 
	t = time;
}
void Airplane::setAccel(float accel)
{
	accel = rand() % 25 + 1; // what my compiler says is the problem: " '=' conversion from 'int' to 'float' error."  
	Accel = accel;
}

void Airplane::setSpeed(float Speed)
{
	speed = Speed;
}

void Airplane::setDistance(float Distance)
{
	distance = Distance;
}

float Airplane::getAccel()
{
	return Accel;
}

float Airplane::getTime()
{
	return t;
}

float Airplane::getSpeed()
{
	return speed;
}

float Airplane::getDistance()
{
	return distance;
}


int main()
{
	Airplane Airplane1, Airplane2;
	unsigned seed;
	seed = time(0); // "'=' : conversion from 'time_t' to 'float' error."
	srand(seed); // "'argument' : conversion from 'float' to 'unsigned int' error."
	float Distance = 0;
	float Speed = 0;
	float Acceleration = 0;
	float time = 0;

	Airplane1.setAccel(Acceleration);
	Airplane2.setAccel(Acceleration);
	Airplane1.setTime(time);
	Airplane2.setTime(time);
	Airplane1.setSpeed(Speed);
	Airplane2.setSpeed(Speed);
	Airplane1.setDistance(Distance);
	Airplane2.setDistance(Distance);
	///////////////////////////////////////////
	Airplane1.getAccel();
	Airplane2.getAccel();
	Airplane1.getTime();
	Airplane2.getTime();
	Airplane1.getSpeed();
	Airplane2.getSpeed();
	Airplane1.getDistance();
	Airplane2.getDistance();
	///////////////////////////////////////////
	time = Airplane1.getTime();
	float s1 = Airplane1.getSpeed();
	float s2 = Airplane2.getSpeed();
	float d1 = Airplane1.getDistance();
	float d2 = Airplane2.getDistance();

	while (time >= 0) //wrong. never do this while (time >= 0);
	{
		if (d1 < 10 && d2 < 10)
		{
			//time += time; This doenst make sense because your taking time and adding itself 
			cout << time;
		}
		else
		{
			cout << time;
		}
		ShowData(Airplane1.getTime(), Airplane1.getSpeed(), Airplane1.getDistance(), Airplane2.getSpeed(), Airplane2.getDistance()); 
		// never redeclare intialized functions. You already have them stated.
	}
	return 0;
}

/*
void Run()
{
 What did this look like? 
};
*/
float ShowData(float &time, float s1, float d1, float s2, float d2)
{
	static float t;
	/*s1, s2;
	d1, d2; you already declared these. No sense in repeating.*/ 
	cout << setw(15) << "Airplane 1" << setw(5) << "" << setw(11) << "Airplane 2" << endl;
	cout << "Time" << setw(5) << "Km/h" << setw(5) << "Km" << setw(6) << "" << setw(5) << "Km/h" << setw(5) << "Km" << endl;
	return 0;
	/*do{
		for (time >= 0; d1 < 10 || d2 < 10; time++)
		{
			cout << setw(10) << s1 << setw(10) << d1 << setw(10) << s2 << setw(10) << d2 << endl;   
		}
		return 0;
	} while (d1 >0 && d2 >0); This nexted loop loop is nonsense, avoid this. Use different variables.*/
}
Sorry this is much closer to what im looking for... I was just getting used to creating class objects...

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
////////////////////////////////////////////
///Prototype function
////////////////////////////////////////////
float test(float, float);
///////////////////////////////////////////
///unused class objects
class Airplane{
private:
float Accel;
float distance;
float speed;
float Time;
float distanceConversion = 1000;
float speedConversion = 3600;
public:
Airplane(){
static float Time;
static float Speed;
static float Distance;
static float Acceleration = rand() %20 +5;
}

void setTime(float);
void setAccel(float);
void setSpeed(float);
void setDistance(float);
float getTime();
float getAccel();
float getSpeed();
float getDistance();
void Run(){
};
};
////////////////////////////////////////////
////////////////////////////////////////////
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
///lots of unused code
///////////////////////////////////////////
///////////////////////////////////////////

int main(){
Airplane Airplane1, Airplane2;
unsigned seed;
seed = time(0);
srand(seed);
float Acceleration = rand() % 20 +5;
static float Time;
static double Distance=0;
static int Speed=0;
static int D;
float distanceConversion = 1000;
float speedConversion = 3600;

cout<<setw(15) <<"Airplane 1" <<setw(5) <<"" <<setw(11)<<"Airplane 2" << endl;
cout <<Acceleration;
/*
for(Time>=0; Time>0; Time++){
do{

float Acceleration = rand() % 20 +5;
static int S;
Speed = (Time * Acceleration)
S = Speed/speedConversion;
Distance = (0.5 * Time * Time * Acceleration);
D = Distance / distanceConversion;

cout<< "Time"<< setw(5)<< "Km/h"<< setw(5)<< "Km"<< setw(6) <<"" <<setw(5)<< "Km/h"<< setw(5)<< "Km"<< endl;

cout<< setw(5)<< Time<<setw(5)<< S<<setw(6)<< D<< setw(2)<<""<<setw(5)<< S<<setw(6)<< D<<endl;
Time++;}
while(D < 10.00);}*/
//{ShowData(float Airplane1.getTime, Airplane1.getSpeed(), Airplane1.getDistance(), Airplane2.getSpeed(), Airplane2.getDistance());}


}
///////////////////////////////////////////////////////////////////////////
///
///
/* Function definition*///////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
/*float test(float Time, float Acceleration){
unsigned seed;
seed = time(0);
srand(seed);
Acceleration;
static float time=0;
static double Distance=0;
static int Speed=0;
static int D = 0;
float distanceConversion = 1000;
float speedConversion = 3600;
cout<<setw(15) <<"Airplane 1" <<setw(5) <<"" <<setw(11)<<"Airplane 2" << endl;
do{
Time = 5;
Speed = (Time * Acceleration)/speedConversion;
Distance = (0.5 * Time * Time * Acceleration);
D = Distance / distanceConversion;

cout<< "Time"<< setw(5)<< "Km/h"<< setw(5)<< "Km"<< setw(6) <<"" <<setw(5)<< "Km/h"<< setw(5)<< "Km"<< endl;

cout<< setw(5)<< Time<<setw(5)<< Speed<<setw(6)<< D<< setw(2)<<""<<setw(5)<< Speed<<setw(6)<< D<<endl;
time++;}
while(D < 10.00);
}*/
Your on the right track but your errors involve warnings. Warnings indicate a possible loss of data, which is really bad. If you try to stack more code on top of warnings, the program will blow up and or not run. Better to fix these mistakes.
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
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
////////////////////////////////////////////
///Prototype function
////////////////////////////////////////////
float test(float, float);
///////////////////////////////////////////
///unused class objects
class Airplane{
private:
	float Accel;
	float distance;
	float speed;
	float Time;
	float distanceConversion = 1000;
	float speedConversion = 3600;
public:
	Airplane(){
		static float Time;
		static float Speed;
		static float Distance;
		static float Acceleration = rand() % 20 + 5; //Warning 1
	}

	void setTime(float);
	void setAccel(float);
	void setSpeed(float);
	void setDistance(float);
	float getTime();
	float getAccel();
	float getSpeed();
	float getDistance();
	void Run(){
	};
};
////////////////////////////////////////////
////////////////////////////////////////////
/*^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^*/
///lots of unused code
///////////////////////////////////////////
///////////////////////////////////////////

int main(){
	Airplane Airplane1, Airplane2;
	unsigned seed;
	seed = time(0); //Warning 2
	srand(seed);
	float Acceleration = rand() % 20 + 5; //Warning 3
	static float Time;
	static double Distance = 0;
	static int Speed = 0;
	static int D;
	float distanceConversion = 1000;
	float speedConversion = 3600;

	cout << setw(15) << "Airplane 1" << setw(5) << "" << setw(11) << "Airplane 2" << endl;
	cout << Acceleration;


How long have you been programming?
This is my first term. Im learning C+ and im about to start my second term. Do you know of any exercises for beginners?
Topic archived. No new replies allowed.