Hi..
plz I need ur help..
I wrote my assignment & it runs but, not all statements can be executed I don't know why??
could u help me?? :sad:
this is my code:
#include <iostream.h>
int check(char x){
if( x=='a'||x=='b'||x=='c'||x=='d'||x=='e'||x=='f'||x=='z' )
return 1;
else
return 0;
}
class CarADT{
private:
float fuel, water, oil;
int switchOn;
int speed;
public:
CarADT(){
fuel = 100.0;
water = 4.0;
oil = 5.0;
switchOn = speed = 0;
}
void Switch(){
switchOn = 1;
cout<<"The car is switched ON\n";
}
void setspeed(float s ){
speed = s;
}
void FillTank(){
fuel = 100.0;
}
void service(){
water = 4.0;
oil = 5.0;
}
void drive( distance ){
if( switchOn == 0 )
switchOn = 1;
if( speed > 0 ){
int t = 0, s = 0;
for( int i=1; i<=distance; i++ )
{
fuel -= 1/10.0;
oil -= 1/200.0;
water -= 1/300.0;
if( fuel == 5 ){ //To refill the tank when fuel = 5
FillTank();
t++;
}
if( oil == .5 || water == .5 ){
service();
s++;
}
}
cout << "Station is visited " << t << " times" << endl;
cout << "Service is made " << s << " times" << endl;
}
}
void status(){
cout << "fuel = "<< fuel << endl;
cout << "oil = "<< oil << endl;
cout << "water = "<< water << endl;
};
~CarADT(){ cout << "The trip ended" << endl; };
};
struct node{
char data;
node* next;
};
typedef struct node* ptrtype;
struct listADT{
private:
ptrtype head;
public:
listADT(){ head = NULL; }
~listADT(){ while (head!=NULL) deleteFront(); }
int isEmpty(){ return (head==NULL); }
int isFull( ptrtype temp ){ return (temp==NULL);}
void insertBack ( char newVal ){
ptrtype temp = new node;
if( isFull( temp ) ){
cout << "No enough memory" << endl;
return;
}
else{
temp->data = newVal;
temp->next = NULL;
if( head == NULL )
head = temp;
else{
for( ptrtype tail=head; tail->next!=NULL; tail=tail->next)
tail->next = temp;
}
}
}
void deleteFront (){
if( isEmpty() ){
cout << "Sorry cannot delete - No records" << endl;
return;
}
ptrtype temp = head;
head = head->next;
delete temp;
}
void execute(struct CarADT c,listADT ll){
int d=0;
float s=0.0;
for ( ptrtype temp=head; temp!=NULL; temp=temp->next ){
switch( temp->data ){
case'a':
c.Switch();
break;
case'b':
c.status();
break;
case'c':
c.FillTank();
cout<<"The tank was filled\n";
break;
case'd':
c.service();
cout << "The car was serviced\n";
break;
case'f':
cout << "Enter distance: ";
cin >> d;
c.drive( d );
break;
case'e':
cout << "Enter speed: ";
cin >> s;
c.setspeed(s);
break;
}
}
while(head!=NULL) deleteFront();
}
void traverse(){
for( ptrtype temp=head; temp!=NULL; temp=temp->next )
cout << temp->data << "-";
cout << "\n\n";
}
};
void main(){
struct listADT list;
struct CarADT myCar;
int choice;
char c;
int valid;
cout<<"*****************************\n"
"* Enter a choice: *\n"
"* 1 to insert commands *\n"
"* 2 to delete commands *\n"
"* 3 to print all commands *\n"
"* 4 to execute the list *\n"
"* 5 to exit *\n"
"*****************************\n";
cin >> choice;
while (choice<5){
switch(choice){
case 1:{
cout<<"***************************\n"
"* Enter a character: *\n"
"* a for switch on command *\n"
"* b for status command *\n"
"* c for fill tank command *\n"
"* d for service command *\n"
"* e for set speed command *\n"
"* f for drive command: *\n"
"* g to end: *\n"
"***************************\n";
cin>>c;
while(c!='g'){
valid = check(c);
while(valid==0){
cout << " you've entered an invalid character, please try again: ";
cin >> c;
valid=check(c);
}
list.insertBack(c);
cout << "Enter the next character: ";
cin >> c;
}
break;
}
case 2:
list.deleteFront();
break;
case 3:
list.traverse();
break;
case 4:
list.execute( myCar, list);
break;
case 5:
break;
default:
cout<<"You have entered an invalid number\n";
break;
} // End of switch
cout << "Enter your next choice: ";
cin >> choice;
} // End of while
}
THANKS ALOT,,,,
BMF