الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

anybody correct my errores

مغلق
بدأه saroona في 15 مارس 2005 · 4 رد · 386 مشاهدة · في ارشيف قسم C/C++
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

a car is a vehicle that we use on daily basis to commute from one place to another. some of its main operations that makes it usable are:start,drive,stop,fill tank,and service. before we can drive the car we need to start it, we then drive it for a a certain distance and stop.

the car consumes petrol, engine oil, water and many other consumables while it is operating. therefore, we need to go to the petrol station to replenish fuel, and the more we drive the more fuel is consumed. periodically we need to service the car to replenish water and engine oil levels.

it is required from you to develop a data type for a car that has the main functions to start, drive, stope, fill tank, and service. the data members must at least include fuel, engine oil, and water. write a menu driven program that uses your car class to demonstrate its functionality

and this is my answer (please correct thim and send it to my email)

and if you can explain why it have many errores or in the site

lass Car {

private:

float water;

float fuel;

float oil;

int engine;

public:

Car() {

water=0.0; fuel-0.0;

oil=0.0; engine=0;

}

void set_water(float w)

{

water=w;

}

void set_fuel(float f)

{

fuel=f;

}

void set_oil(float o)

{

oil=o;

}

void set_engine(int e)

{

engine=e;

}

float getwater()

{return water;}

float getoil()

{return oil;}

int getengine()

{ return engine;}

float getfuel()

{ return fuel;}

};

void start()

{

cout<<"enter 1 for on/0 for off\n";

cin>>engine;

if(engine=1)

cout<<"engine started\n";

}

void drive()

{

float w1,f1,o1;

float distance;

w1=distance*1;

o1=distance*1;

f1=distance*1;

cout<<"distance=";

cin>>distance;

if(f1>fuel)

cout>>"go to petrol station\n";

if(o1>oil)

cout>>"there no\n ";

oil=oil-o1;

water=water-w1;

fuel=fuel-f1;

}

void filltank()

{

set_fuel(100);

}

void service()

{

set_oil(100);

set_water(100);

}

void stope()

{

engine=0;

cout<<"good bye\n";

}

void main()

{

int choose;

cout<<"please choose one of these:\n 1 to start\n 2 to drive\n 3 to stop\n 4 to fill tank\n 5 to service\n 6 to exit\n";

cin>>choose;

switch(choose)

{

case 1:

start(); break;

case 2:

drive(); break;

case 3:

stope(); break;

case 4:

filltank(); break;

case 5:

service(); break;

case 6:

cout<<"thanks\n";

default:

cout<<"errore\m";

}

}

#2

in

Car() {
water=0.0; fuel-0.0;
oil=0.0; engine=0;
}

change

fuel-0.0

to

fuel = 0.0

notice this mistake:

float getfuel()
{ return fuel;}
};

the last line

};

is wrong, it ends the class!! this means all the functions after it are not part of the class.

remove that thing!

so change this:

float getfuel()
{ return fuel;}
};

void start()
{
cout<<"enter 1 for on/0 for off\n";
cin>>engine;
if(engine=1)
cout<<"engine started\n";
}

to this:

float getfuel()
{ return fuel;}

void start()
{
cout<<"enter 1 for on/0 for off\n";
cin>>engine;
if(engine=1)
cout<<"engine started\n";
}

now, you need to put this AFTER the stope() and BEFORE main() function.

inside main, you have to create a car object like this:

Car car1;

for example, you can name it anything you like, but I called it car1

now, in the switch-case thing, call the methods against the car1 object, like this:

switch(choose)
{
case 1:
car1.start(); break;
case 2:
car1.drive(); break;
case 3:
car1.stope(); break;
case 4:
car1.filltank(); break;
case 5:
car1.service(); break;

There is still a problem, you need some loop around the switch-case statement so that the program doesn't end until the user enters 6

there are few other errors like cout >> instead of cout << but anyway here's a fixed version:

,

class Car {
private:
float water;
float fuel;
float oil;
int engine;
public:
Car() {
water=0.0; fuel=0.0;
oil=0.0; engine=0;
}
void set_water(float w)
{
water=w;
}
void set_fuel(float f)
{
fuel=f;
}
void set_oil(float o)
{
oil=o;
}
void set_engine(int e)
{
engine=e;
}

float getwater()
{return water;}
float getoil()
{return oil;}
int getengine()
{ return engine;}
float getfuel()
{ return fuel;}

void start()
{
cout<<"enter 1 for on/0 for off\n";
cin>>engine;
if(engine=1)
cout<<"engine started\n";
}
void drive()
{
float w1,f1,o1;
float distance;
w1=distance*1;
o1=distance*1;
f1=distance*1;
cout<<"distance=";
cin>>distance;

if(f1>fuel)
cout << "go to petrol station\n";
if(o1>oil)
cout << "there no\n ";
oil=oil-o1;
water=water-w1;
fuel=fuel-f1;
}
void filltank()
{
set_fuel(100);
}
void service()
{
set_oil(100);
set_water(100);
}
void stope()
{
engine=0;
cout<<"good bye\n";
}

};

void main()
{
	Car car1;
	int choose;

	do
	{
  cout<<"please choose one of these:\n 1 to start\n 2 to drive\n 3 to stop\n 4 to fill tank\n 5 to service\n 6 to exit\n";
  cin>>choose;
  switch(choose)
  {
  case 1:
  car1.start(); break;
  case 2:
  car1.drive(); break;
  case 3:
  car1.stope(); break;
  case 4:
  car1.filltank(); break;
  case 5:
  car1.service(); break;
  case 6:
  cout<<"thanks\n";
  default:
  cout<<"errore\n";
  }
	}	while( choose != 6 );

}

now, there are still some bugs, but I'm not sure how to fix them because I'm not sure what you were trying to do, look in this function:

void drive()
{
float w1,f1,o1;
float distance;
w1=distance*1;
o1=distance*1;
f1=distance*1;
cout<<"distance=";
cin>>distance;

if(f1>fuel)
cout << "go to petrol station\n";
if(o1>oil)
cout << "there no\n ";
oil=oil-o1;
water=water-w1;
fuel=fuel-f1;
}

float distance is being used without being initialized, I'm not sure what you were trying to do, maybe you should put this

cin >> distances;

before you use it, but I'm not sure

hope that helps :)

تم تعديل هذه المشاركة بواسطة hasan_aljudy في 15 مارس 2005 في 17:20

#3

i but my proplem and i'm sure that u will answer me(95%(

thanx alot about your help it is very useful to me and now there is7 ;)

errores instead of 15 :rolleyes:

i don't know why

as you said that function (drive) and start had errores i don't know what's the rong with them :wacko:

in function (drive) i'm trying to cheack if the oil&fuel&water is ok to cut away the distance

and in fuction start mybe can i delet the 2first stepes(cout&cin)it's ok

i don't know please can you cheak it again

thanx alooooooooot to help me :rolleyes:

my name is sara

thanx again

#4

OK Sarah

I don't know why you are still getting errors, the code I posted above compiles with no errors with me.

anyway, I just noticed another error, in start:

if(engine=1)

should be

if(engine == 1)

I think the engine should be a bool instead of an int, but that's no big deal ..

You shouldn't ask for on or off, the start is obviously on .. so just make it

engine = 1;

for the drive method, you first need to make sure that the engine is on, so you might add something like this:

	if( engine == 0 )
	{
  cout << "can't drive when engine is off\n";
  return;
	}

also, you should ask for distance before makinig other calculations, so here's a fixed version:

void drive()
{
	if( engine == 0 )
	{
  cout << "can't drive when engine is off\n";
  return;
	}
	float distance;
	float w1,f1,o1;

	cout<<"distance=";
	cin>>distance;

	w1=distance*1;
	o1=distance*1;
	f1=distance*1;


	if(f1>fuel)
	cout << "go to petrol station\n";
	if(o1>oil)
	cout << "there no\n ";
	oil=oil-o1;
	water=water-w1;
	fuel=fuel-f1;
}

also, notice the incomplete messege "there's no" should be "there's no oil", also, you probably need one for "there's no water".

you need a "break;" after

 case 6:
 cout<<"thanks\n";

now, for services and feul, you may need to print some messege to tell the user that services are done.

void filltank()
{
set_fuel(100);
cout << "Fuel is now full\n";
}
void service()
{
	set_oil(100);
	set_water(100);

	cout << "Oil now ok\n";
	cout << "Water now ok\n";
}

same for driving:

	cout << "Drove " << distance << " kilometers \n";

same for start

void start()
{
	engine = 1;
	cout << "Engine is On\n";
}

now, I added these changes to your code, thsi code compiles with no errors with me, if you get any error messege, post it here .. the only thing I didn't write here is the

#include <iostream>

using namespace std;

but I might as well include it!

so here you go:

#include <iostream>
using namespace std;

class Car {
private:
float water;
float fuel;
float oil;
int engine;
public:
Car() {
water=0.0; fuel=0.0;
oil=0.0; engine=0;
}
void set_water(float w)
{
water=w;
}
void set_fuel(float f)
{
fuel=f;
}
void set_oil(float o)
{
oil=o;
}
void set_engine(int e)
{
engine=e;
}

float getwater()
{return water;}
float getoil()
{return oil;}
int getengine()
{ return engine;}
float getfuel()
{ return fuel;}

void start()
{
	engine = 1;
	cout << "Engine is On\n";
}
void drive()
{
	if( engine == 0 )
	{
  cout << "can't drive when engine is off\n";
  return;
	}

	float distance;
	float w1,f1,o1;

	cout<<"distance = ";
	cin>>distance;

	w1=distance*1;
	o1=distance*1;
	f1=distance*1;

	cout << "Drove " << distance << " kilometers \n";


	if( f1 > fuel )
  cout << "go to petrol station\n";

	if( o1 > oil )
  cout << "there's no oil\n";

	if( w1 > water )
  cout << "there's no water \n";

	oil = oil - o1;
	water = water - w1;
	fuel = fuel - f1;
}
void filltank()
{
set_fuel(100);
cout << "Fuel is now full\n";
}
void service()
{
	set_oil(100);
	set_water(100);

	cout << "Oil now ok\n";
	cout << "Water now ok\n";
}

void stope()
{
engine=0;
cout<<"good bye\n";
}

};

void main()
{
Car car1;
int choose = 0;

do
{
 cout<<"please choose one of these:\n 1 to start\n 2 to drive\n 3 to stop\n 4 to fill tank\n 5 to service\n 6 to exit\n";
 cin>>choose;
 switch(choose)
 {
 case 1:
 car1.start(); break;
 case 2:
 car1.drive(); break;
 case 3:
 car1.stope(); break;
 case 4:
 car1.filltank(); break;
 case 5:
 car1.service(); break;
 case 6:
 cout<<"thanks\n"; break;
 default:
 cout<<"errore\n";
 }
} while( choose != 6 );

}
#5

THANX HASSAN VERY MUCH TO HELP ME

I CHEAK IT AND NO ERRORES INSTED OF #INCLUDE<ISOTREAM.H>

(.H)IS MISSED

when i cheak it before i forgot write the hidder file & (==) it is simple mistake but it makes the errores

thanx again :rolleyes:

thanx :rolleyes:

you are the bist ;)

bye

هذا الموضوع مغلق.

مواضيع مشابهة