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

Edited 3 Years Ago by Dani: Formatting fixed

Without taking such a close look at this code, tell us the following:

What exactly goes wrong? What does it print/not print that it's supposed to/not supposed to do?

I tried compiling via gcc and it spit out like 100 syntax errors. I haven't taken a close look at it yet. (busy with my own schoolwork at the moment)

CSCGal is right on the money there BMF. Check you code over dude, as there seems to be some inconsistencies.

ex:

void execute(struct CarADT c.listADT 11)

CarADT you declare as a Class earlier in the code, and now you say that it is a struct?

So I think if you fix that issue, and the line

void drive ( distance )

as distance is not declared anywhere in your code. Data type, etc...

We really would like to help but, I would check your code over a bit first, and get back to us.

There are simply too many problems with your code as it is at the moment for someone to come along and fix it for you. You're a long way off having working code. I do wonder how you've come to have such code, whether you got it from somewhere else rather than writing it yourself. But certainly, as it is I can't see any way that it should compile, not even on an old compiler. There's just too much wrong with it.

The first piece of advice I have, then, is to effectively start again, and build up your code gradually. Don't throw this code out, just start again with a clean source file and add bits in slowly, testing along the way that it will compile, and running the bits that you have working.

For example, you could develop your program by getting your CarADT class up and running first. Not the whole class at once, to begin with just the constructor, then start adding some of the member functions and write a short main() function that exercises them. Don't add more code till you get the existing stuff working; that gives you something solid to build on.

So basically, you're trying to do too much at once right now. Too many problems with your code, too many errors to fix, and no hope of getting it all sorted any time soon unless you're reasonably experienced.

Here's some help to get you started. I've rewritten your CarADT class so that it compiles. I haven't exercised the class yet, and I can already see that you'll find further problems with this code along the way, but it should move you along a little. With the advice above about building up your code again from scratch, gradually, you may be able to make more progress. If this is still beyond you, you may need to put this program off until you've had more practice with smaller stuff.

class CarADT{
private:
    float fuel;
    float water;
    float oil;
    int switchOn;
    int speed;
public:
    CarADT();
    ~CarADT();
    void Switch();
    void setspeed(int s);
    void FillTank();
    void service();
    void drive(int distance );
    void status();
};

CarADT::CarADT()
{
    fuel = 100.0;
    water = 4.0;
    oil = 5.0;
    switchOn = 0;
    speed = 0;
}

void CarADT::Switch()
{
    switchOn = 1;
    cout<<"The car is switched ON\n";
}

void CarADT::setspeed(int s )
{
    speed = s;
}

void CarADT::FillTank()
{
    fuel = 100.0;
}

void CarADT::service()
{
    water = 4.0;
    oil = 5.0;
}

void CarADT::drive(int 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 ){
                FillTank();
                t++;
            }

            if( oil == .5 || water == .5 ){
                service();
                s++;
            }
        }

        cout << "Station is visited " << t << " times" << endl;
        cout << "Service is made " << s << " times" << endl;
    }
}

void CarADT::status(){
    cout << "fuel = "<< fuel << endl;
    cout << "oil = "<< oil << endl;
    cout << "water = "<< water << endl;
};

CarADT::~CarADT()
{ 
    cout << "The trip ended" << endl;
}

You definatly have some significant syntactical errors... Just to touch on some of the other replies...

struct listADT list;
struct CarADT myCar;

You probably should refresh your knowledge of structures, they are quite different entities from classes :)

Bob's changes to your code are a great starting point, study his changes... But don't just take his code and go from there. Make sure you understand his changes.

Here's a tip, especially if you're programming for a college course... When you write a code, write 3 or 4 lines, re-compile, then write 3 or 4 more lines, etc... Build your code from the bottom up, making sure each new section compiles correctly and functions correctly. Working in small chunks will ease long nights, decrease your Mountain Dew consumption and preserve your sanity.


Good luck :)

-Metal Rob

This article has been dead for over six months. Start a new discussion instead.