Thread: What may I do to fix this problem? :(

  1. #1
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68

    Question What may I do to fix this problem? :(

    Hi,

    I have the created the following code....

    ___________________________________________

    #include<iostream.h>
    class HotDogStand
    {
    private:
    int HotDogsOnHand; // dogs remaining
    int BunsOnHand; // buns remaining
    float CashOnHand; // cash remaining
    public:
    void displayData() // display data
    {
    cout << "\n\tHot dogs on hand = "
    << HotDogsOnHand << endl;
    cout << "\tBuns on hand = "
    << BunsOnHand << endl;
    cout << "\tCash on hand = Lm"
    << CashOnHand << endl;
    }
    void initData() // get initial data from user
    {
    cout << "\n\tEnter dogs on hand: ";
    cin >> HotDogsOnHand;
    cout << "\tEnter buns on hand: ";
    cin >> BunsOnHand;
    cout << "\tEnter Cash on hand: ";
    cin >> CashOnHand;
    }
    void SoldOneDog() // adjust data to reflect sale
    {
    --HotDogsOnHand;
    --BunsOnHand;
    CashOnHand = CashOnHand - 1.95;
    }
    }; // end of class HotDogStand

    void main()
    {
    int stands;
    cout<<"HOTDOG STAND ORGANIZER:";
    cout<<"\n\nHow many stands do you own?";
    cin>>stands;
    HotDogStand standno[stands];
    char choice = 'x';
    int stand;
    cout<<"\n\nPlease choose:"<<endl;
    cout<<"-To initialize the data at a particular stand press 'i' "<<endl;
    cout<<"-To record a sale at a particular stand press 's' "<<endl;
    cout<<"-To report the current data for all the stands press 'r' "<<endl;
    cout<<"-To quit the program press 'q' "<<endl;
    cin>>choice;
    while ( choice != 'q' )
    {
    if ( choice == 'i' || choice == 's' )
    {
    cout<<"\nPlease choose from these stands: "<<endl;
    for (int j=1; j <= stands; j++)
    cout<<"[ Stand "<<j<<" ]"<<endl;
    cout<<"\nStand Number? ";
    cin>>stand;
    if (stand <= 0 || stand > stands )
    continue;
    }
    switch (choice)
    {
    case 'i':
    cout<<"\nInitializing the data at stand "<<stand<<"...\n";
    standno[stand].initData();
    break;
    case 's':
    cout<<"\nRecording a sale at stand "<<stand<<"...\n";
    standno[stand].SoldOneDog();
    break;
    case 'r':
    {
    cout<<"\nReporting data for all the stands...\n";
    for (int d=0; d<stands; d++)
    {
    cout<<"Data for Stand"<<stand+1<<"...\n";
    standno[d].displayData();
    }
    break;
    }
    default:
    cout<<"ERROR! You will return to the main menu.";
    }

    cout<<"\n\nPlease choose:"<<endl;
    cout<<"-To initialize the data at a particular stand press 'i' "<<endl;
    cout<<"-To record a sale at a particular stand press 's' "<<endl;
    cout<<"-To report the current data for all the stands press 'r' "<<endl;
    cout<<"-To quit the program press 'q' "<<endl;
    cin>>choice;
    switch (choice)
    {
    case 'i':
    cout<<"\nYou have chosen to initialize the data at a particular stand...\n\n";
    break;
    case 's':
    cout<<"\nYou have chosen to record a sale at a particular stand...\n\n";
    break;
    case 'r':
    cout<<"\nYou have chosen to report the data for all the stands...\n\n";
    }
    }
    }

    ______________________________________________


    Try to compile it and you'll see my problem!
    I'm trying to create an array of objects...

    If I write: HotDogStand standno[5];

    it works, however if I try to use:

    HotDogStand standno[stands]; // when stands = 5

    it doesn't

    What may I do to fix this problem?

    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  2. #2
    Unregistered
    Guest
    instead of HotDogStand standno[stands];

    try:

    HotDogStand *standno = new HotDogStand[stands];

  3. #3
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    Thanks,

    As much as I hate fixing the code that way (coz I haven't learnt pointers yet), I guess I'll have to use it since it works

    Now I'm gonna keep fixing the code to make it more user friendly

    Say... what level of a C++ programmer do you think I am?
    A total idiot?
    A total Newbie?
    Beginner?
    Beginner with potential?

    heheh

    Thanks,
    Marc
    No matter how much you know, you NEVER know enough.

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    void SoldOneDog() // adjust data to reflect sale
    {
    --HotDogsOnHand;
    --BunsOnHand;
    CashOnHand = CashOnHand - 1.95;
    }
    }; // end of class HotDogStand

    lookin at that code of yours i see you take money away from the CashOnHand variable if you sell a dog.
    Is that a logic error? dont you want to add 1.95 to the total?

    and i think you are a Beginner with potential

  5. #5
    Registered User marCplusplus's Avatar
    Join Date
    Nov 2001
    Posts
    68
    oh thats why the more i sold the more i lost money...
    hehe 10x
    No matter how much you know, you NEVER know enough.

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    int size=5;
    int array[size]; // compiler error. not allowed to be a variable.

    const int size=5;
    int array[size]; // perfectly ok. size is constant and so will compile
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User edshaft's Avatar
    Join Date
    Nov 2001
    Posts
    45
    you could try making int stands
    const int stands
    that might work

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem located but not sure how to fix MouseWheel problem
    By jeppesenbrian in forum C# Programming
    Replies: 2
    Last Post: 07-01-2006, 01:43 PM
  2. small reference problem
    By DavidP in forum C++ Programming
    Replies: 6
    Last Post: 06-21-2004, 07:29 PM
  3. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM