What do I need to do?

This is a discussion on What do I need to do? within the C++ Programming forums, part of the General Programming Boards category; Okay I am writing a program that is called tollbooth and I had to add a second tollbooth to it. ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    7

    What do I need to do?

    Okay I am writing a program that is called tollbooth and I had to add a second tollbooth to it. But why does my code not work?

    Code:
    // ex6_2.cpp
    // uses class to model toll booth
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    const char ESC = 27;           //escape key ASCII code
    const double TOLL = 0.5;       //toll is 50 cents
    ////////////////////////////////////////////////////////////////
    class tollBooth
       {
       private:
          unsigned int totalCars;      //total cars passed today
          double totalCash;            //total money collected today
       public:                        //constructor
          tollBooth() : totalCars(0), totalCash(0.0)
             {  }
          void payingCar()                         //a car paid
             { totalCars++; totalCash += TOLL; }
          void nopayCar()                          //a car didn't pay
             { totalCars++; }
          void display() const                     //display totals
             { cout << "\nCars=" << totalCars
                    << ", cash=" << totalCash
                    << endl; }
        };
    //---------------------------------------------------------------------
    class tollBooth2
       {
       private:
          unsigned int totalCars;  //total cars passed today
          double totalCash;        //total money collected today
       public:                     //constructor
          tollBooth() : totalCars(0), totalCash(0.0)
             {  }
          void payingCar()                       //a car paid
             { totalCars++; totalCash += TOLL; }
          void nopayCar()                        //a car didn't pay
             { totalCars++; }
          void display() const                   //display totals
             { cout << "\nCars=" << totalCars
                    << ", cash=" << totalCash
                    << endl; }
        };
    //---------------------------------------------------------------------
    
    int main()
       {
       tollBooth booth1, booth2;           //create a toll booth
       char ch;
    
       cout << "\nPress 0 for each non-paying car,"
            << "\n      1 for each paying car,"
            << "\n      2 to go to second tool booth,"
            << "\n      Esc to exit the program.\n";
       do {
          ch = getche();           //get character
          if( ch == '0' )          //if it's 0, car didn't pay
             booth1.nopayCar();
          if( ch == '2' )
             goto tollBooth2;
          if( ch == '1' )          //if it's 1, car paid
             booth1.payingCar();
          } while( ch != ESC );    //exit loop on Esc key
       booth1.display();           //display totals
       getch();
       return 0;
       }
    Here are the errors:
    [C++ Warning] Ex6_2.cpp(4): W8058 Cannot create pre-compiled header: write failed
    [C++ Error] Ex6_2.cpp(34): E2040 Declaration terminated incorrectly
    [C++ Error] Ex6_2.cpp(44): E2040 Declaration terminated incorrectly
    [C++ Error] Ex6_2.cpp(44): E2190 Unexpected }
    [C++ Error] Ex6_2.cpp(44): E2190 Unexpected }
    [C++ Error] Ex6_2.cpp(68): E2448 Undefined label 'tollBooth2'
    What do I do?


    Code Tags changed from [ Quote ] tags by Kermi3....good effort .

  2. #2
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Insteed of using quote-tags use code tags ( [ code ] [ /code] , without blank spaces) because itīs easer to "follow" the code.

    Declaration terminated incorrectly
    means that a declaration is declared incorrectly

    Unexpected }
    Means that you have an extra/missing { or }

    Undefined label 'tollBooth2'
    You havenīt declared a label named 'tollBooth2'.

    Quite self explaning

    Hope that it helps and donīt forget to use code tags, the formating is so much better.

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    1
    hi, try this one. Is it OK?


    Code:
    // ex6_2.cpp
    // uses class to model toll booth
    #include <iostream>
    using namespace std;
    #include <conio.h>
    
    const char ESC = 27;           //escape key ASCII code
    const double TOLL = 0.5;       //toll is 50 cents
    ////////////////////////////////////////////////////////////////
    class tollBooth
       {
       private:
          unsigned int totalCars;      //total cars passed today
          double totalCash;            //total money collected today
       public:                        //constructor
          tollBooth() : totalCars(0), totalCash(0.0)
             {  }
          void payingCar()                         //a car paid
             { totalCars++; totalCash += TOLL; }
          void nopayCar()                          //a car didn't pay
             { totalCars++; }
          void display() const                     //display totals
             { cout << "\nCars=" << totalCars
                    << ", cash=" << totalCash
                    << endl; }
        };
    
    
    int main()
       {
       tollBooth booth1, booth2;   //create a toll booth
       tollBooth *current_booth = &booth1;
    
       char ch;
    
       cout << "\nPress 0 for each non-paying car,"
            << "\n      1 for each paying car,"
            << "\n      2 to go to second tool booth,"
            << "\n      Esc to exit the program.\n";
       do {
          ch = getche();           //get character
          if( ch == '0' )          //if it's 0, car didn't pay
             current_booth->nopayCar();
          if( ch == '2' )
             current_booth = &booth2;
          if( ch == '1' )          //if it's 1, car paid
             current_booth->payingCar();
          } while( ch != ESC );    //exit loop on Esc key
       current_booth->display();           //display totals
       getch();
       return 0;
       }
    [edit]Code tags added by Hammer. Please use them in future.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    And as you probably know by now, "goto:" does not work like that! Besides, DON"T use them. They are fine constructs for ASM but have little value in this language...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    In the class tollBooth2 you have defined the constructor wrong.

    Code:
    class tollBooth2
       {
       private:
          unsigned int totalCars;  //total cars passed today
          double totalCash;        //total money collected today
       public:                     //constructor
          //constructor should be
          //tollBooth2() : totalCars(0), totalCash(0.0)
          tollBooth() : totalCars(0), totalCash(0.0)
             {  }
          void payingCar()                       //a car paid
             { totalCars++; totalCash += TOLL; }
          void nopayCar()                        //a car didn't pay
             { totalCars++; }
          void display() const                   //display totals
             { cout << "\nCars=" << totalCars
                    << ", cash=" << totalCash
                    << endl; }
        };
    Change this and the goto label and it should work. I would strongly advise you to listen to Sebastiani and donīt use goto unless you are absolutly sure that it works as intended.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    7
    Thank you very much. You all helped greatly.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21