Thread: Need help figuring out why program wont work.

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Need help figuring out why program wont work.

    I am writing this program as homework and I've got it all written. It has no "errors". However it doesn't run correctly. The point of the program is to ask the user what type of animal you are checking into the vet's office. Then ask how much the animal weighs. Then it will give you the cost of the visit based off of what the user inputs. The programs works great until you enter the weight. Then it immediately goes to the default statement, then asks again followed by switching to the completely wrong default statement. I appreciate any help anyone can offer.

    Code:
    #include <iostream>
    using namespace std;
    
    
    int main ()
    {
      int weightDog;
      int weightCat;
      int weightHorse;
      char type;
    
    
        cout << "Welcome to the Vet Office!\n " ;
        cout << "Please enter an animal type:\n " ;
        cout << "D - Dog\n " ;
        cout << "C - Cat\n " ;
        cout << "H - Horse\n " ;
        cin >> type;
      switch(type)
    
    
    {
      {
          case 'd':
          case 'D': cout << "How much does your dog weigh? ";
                    cin >> weightDog;
                  switch(weightDog)
                {
                    if (weightDog >= 1 && weightDog < 25)
                    {
                      cout << "Your dog is classified as a small dog.\n";
                      cout << "Your consultation will cost $70.\n";
                    break;
                    }
                    else if (weightDog >= 25 && weightDog < 70)
                    {
                      cout << "Your dog is classified as a medium dog.\n";
                      cout << "Your consultation will cost $90.\n";
                    break;
                    }
                    else if (weightDog >= 70 && weightDog <= 200)
                    {
                      cout << "Your dog is classified as a large dog.\n";
                      cout << "Your consultation will cost $120.\n";
                    break;
                    }
                    default: cout << "You did not enter a valid weight. Please enter a weight under 70 pounds.\n";
                    break;
                }
      }
      {
          case 'c':
          case 'C': cout << "How much does your cat weigh? ";
                    cin >> weightCat;
                   switch(weightCat)
                  {
                    if (weightCat >= 1 && weightCat < 5)
                    {
                      cout << "Your cat is classified as a small cat.\n";
                      cout << "Your consultation will cost $40.\n";
                    break;
                    }
                    else if (weightCat >= 5 && weightCat < 10)
                    {
                      cout << "Your cat is classified as a medium cat.\n";
                      cout << "Your consultation will cost $50.\n";
                    break;
                    }
                    else if (weightCat >= 10 && weightCat < 30)
                    {
                      cout << "Your cat is classified as a large cat.\n";
                      cout << "Your consultation will cost $60.\n";
                    break;
                    }
                    default: cout << "You did not enter a valid weight. Please enter a weight under 30 pounds.\n";
                    break;
                  }
        }
    
    
            {
          case 'h':
          case 'H': cout << "How much does your horse weigh? ";
                    cin >> weightHorse;
                   switch(weightHorse)
                  {
                    if (weightHorse >= 1 && weightHorse < 600)
                    {
                      cout << "Your horse is classified as a small horse.\n";
                      cout << "Your consultation will cost $140.\n";
                    break;
                    }
                    else if (weightHorse >= 600 && weightHorse < 1200)
                    {
                      cout << "Your horse is classified as a medium horse.\n";
                      cout << "Your consultation will cost $190.\n";
                    break;
                    }
                    else if (weightHorse >= 1200 && weightHorse < 3500)
                    {
                      cout << "Your horse is classified as a large horse.\n";
                      cout << "Your consultation will cost $220.\n";
                    break;
                    }
                    default: cout << "You did not enter a valid weight. Please enter a weight under 3500 pounds.\n";
                    break;
                  }
            }
          default: cout << "You did not enter a valid option for animal. Please choose D, C, or H.\n ";
      }
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The `break' keyword honors scope for lack of a better way to put it.

    Do you know what `default' does? Do you know that `switch' chooses based on `case' and not `if'?

    So, long story short... everything for weight is wrong.

    Do away with what you have and get the weight working for just one animal. You don't even need the input for the animal. You can assume I want to check in a dog.

    Write the bit of code to do that, and we will go from there.

    Soma

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Apart from the logic error which Soma has pointed out, your code will reject cats and dogs a little outside the norm, such as this little fellow. His weight is not invalid.

    Need help figuring out why program wont work.-sumatran-tiger-hero_92514619-jpg
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    thanks

    Thanks for the help. Once pointed out what I did wrong I was able to re work code to get it to work properly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why wont isalpha() work in my program?
    By 337higgin in forum C Programming
    Replies: 2
    Last Post: 09-24-2011, 07:04 PM
  2. Replies: 4
    Last Post: 03-22-2010, 12:15 PM
  3. program wont work, why?
    By chasingxsuns in forum C Programming
    Replies: 11
    Last Post: 02-02-2006, 04:38 PM
  4. program from book wont work
    By cemock in forum C Programming
    Replies: 2
    Last Post: 03-06-2003, 09:58 AM
  5. Heeeelpppppp Please! Program Wont Work!
    By MagiZedd in forum Windows Programming
    Replies: 3
    Last Post: 11-18-2001, 12:18 AM