Thread: Vet program help

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    2

    Question Vet program help

    I'm trying to create a program for a veterinary clinic. But I'm having some issues. It isn't calculating the total bill for the pet, and some of the menu options (and the menu itself) is an infinite loop, the program never ends.

    Code:
    //VetClinic.cpp
    //Created/revised by <mcn29> on <12/3/10>
    
    #include <iostream>
    #include <iomanip>
    #include <string>
    using namespace std;
    
    double totalBill = 0.0;
    double vetVisit = 0.0;
    double kennelService = 0.0;
    int numOfDays = 0;
    double totalKennel = 0.0;
    double nailClipping = 0.0;
    double bath = 0.0;
    
    //delcare arrays
    int BATH_TYPE[3] = {1, 2, 3};
    double BATH_PRICE[3] = {30.00, 20.00, 15.00};
    
    //function prototypes
    void displayCatMenu();
    void displayDogMenu();
    double catBathOption();
    double dogBathOption();
    double vetOption();
    double kennelOption();
    double nailOption();
    double calculateTotalBill();
    
    int main()
    {
    	//declare variables
    	string name = "";
    	string lastName = "";
    	int animalType = 0;
    	int option = 0;
    	double price = 0.0;
    
    	
    
    	//get animal name and animal type
    	cout << "Animal First Name: ";
    	cin >> name;
    	cout << "Animal Last Name: ";
    	cin >> lastName;
    	cout << "Enter 1 for Cat or 2 for Dog: ";
    	cin >> animalType;
    
    	if (animalType == 1)
    	{
    		displayCatMenu();
    		cout << "Enter 1, 2, 3 or 4: ";
    		cin >> option;
    	}
    
    	while (option > 0 && option < 5)
    	{
    		displayCatMenu();
    		cout << "Enter 1, 2, 3 or 4: ";
    		cin >> option;
    
    		if (option == 1)
    			catBathOption();
    		if (option == 2)
    			vetOption();
    		if (option == 3)
    			kennelOption();
    		if (option == 4)
    			totalBill = bath + vetVisit + totalKennel + nailClipping;
    			cout << "Total Bill = $" << totalBill << endl;
    	}
    	
    
    	//end while
    	//end if
    
    
        if (animalType == 2)
    	{
    		displayDogMenu();
    		cout << "Enter 1, 2, 3, 4 or 5: ";
    		cin >> option;
    	}
    	while (option > 0 && option < 6)
    	{
    		if (option == 1)
    			dogBathOption();
    		if (option == 2)
    			vetOption();
    		if (option == 3)
    			kennelOption();
    		if (option == 4)
    			nailOption();
    		if (option == 5)
    			
    			cout << "Total Bill = $" << totalBill << endl;
    
    	}//end while
    	//end if
    
    	if (option != 1 && option != 2)
    	cout << "Invalid Number" << endl;
    //end if
    
    	system("pause");
    	return 0;
    	} //end of main function
    
    
    //*****function definitions*****
    
    void displayCatMenu()
    {
    	cout << "1 Bath " << endl;
    	cout << "2 Vet Visit " << endl;
    	cout << "3 Kennel Service " << endl;
    	cout << "4 Total Bill " << endl;
    } //end displayCatMenu
    
    void displayDogMenu()
    {
    	cout << "1 Bath " << endl;
    	cout << "2 Vet Visit " << endl;
    	cout << "3 Kennel Service " << endl;
    	cout << "4 Nail Clipping " << endl;
    	cout << "5 Total Bill " << endl;
    } //end of displayDogMenu
    
    double catBathOption()
    {
    	double bath = 0.0;
    	int sub = 1;
    	bath = BATH_PRICE[2];
    	totalBill = totalBill + bath;
    	return bath;
    
    }//end of catBathOption function
    
    double dogBathOption()
    {
    	double bath = 0.0;
    	int sub = 1;
    	cout << "Enter 1 for Large Dog or 2 for Small Dog: ";
    	cin >> bath;
    	if (bath == 1) 
    		bath = BATH_PRICE[0];
    	else if (bath == 2)
    		bath = BATH_PRICE[1];
    	else if (bath < 0 && bath > 3) 
    		cout << "Invalid Number" << endl;
    	totalBill = totalBill + bath;
    		return bath;
    		//end if
    }//end of dogBathOption
    
    
    
    double vetOption()
    {
    	double vetVisit = 75.00;
    	totalBill = totalBill + vetVisit;
    	return vetVisit;
    } //end of vetOption function
    
    double kennelOption()
    {
    	double kennelService = 30.00;
    	int numOfDays = 0;
    	double totalKennel = 0.0;
    
    	cout << "Enter Numer of Days in Kennel: ";
    	cin >> numOfDays;
    	totalKennel = (kennelService*numOfDays);
    	totalBill = totalBill + totalKennel;
    	return totalKennel;
    } //end of kennelOption function
    
    double nailOption()
    {
    	double nailClipping = 8.0;
    	totalBill = totalBill + nailClipping;
    	return nailClipping;
    } //end of nailOption function
    
    double calculateTotalBill()
    {
    	totalBill = bath + vetVisit + totalKennel + nailClipping;
    	return totalBill;
    } //end calculateTotalBill function

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, to point out some issues. You have some logic problems:
    Code:
    	if (animalType == 1)
    	{
    		displayCatMenu();
    		cout << "Enter 1, 2, 3 or 4: ";
    		cin >> option;
    	}
    
    	while (option > 0 && option < 5)
    As you see, the while is executed regardless of what value animalType has. There is a similar problem elsewhere. You probably meant to put it inside the if statement.

    You might also lookup else if.

    Code:
    		if (option == 4)
    			totalBill = bath + vetVisit + totalKennel + nailClipping;
    			cout << "Total Bill = $" << totalBill << endl;
    Braces are missing here.

    You should also make all your global variables const, because as I understand them, they are constants.

    Finally, after making these fixes and it still isn't working, please provide a description on how to reproduce the bug. It makes it easier to find your mistake.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    2
    Thanks that fixed the calculating issues. My while statement is still an infinite loop.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Did you ever bother to change option inside the while loop?

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    6
    Try putting your while loops inside the animal if statements. It looks like the while loops get executed no matter what animal you select.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM