2 Replies - 766 Views - Last Post: 20 March 2011 - 12:41 AM Rate Topic: -----

#1 J-e-L-L-o   User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

need help with declaration errors in function

Posted 19 March 2011 - 11:57 PM

it's me again!

I really need to practice on declarations in c++. I don't entirely get when it needs to be done and where...

But for the most part my code compiles except for line 125: "in function void getChoice(char) then the 4 character instances in my while loop all say "not declared in this scope"

At first I tried a reference variable named &letter in the function getChoice and that didn't work either. I need it to finish compiling to so I can debug it.

here is my code for my function. how do i get the compiler to accept it?

/* This program is for a hospital to show fees for an inpatient or outpatient.

 */
 
 #include <iostream>
 #include <iomanip>
 using namespace std;
 
 // Function prototypes
 void getChoice(char);
 double totalCharges(double, double);
 
 // Default RATE argument included. If rate changes update here. 
 double totalCharges(int, double, double, double RATE= 50.00);
 
 int main()
 {
 
 	char choice;								// used for menu selection. 
 	double medicationCost;
 	double servicesCost;
 	int daysStayed;
 	double total;								// Calculated sum of all totals. 
 
 	cout << "Which records would you like to look up. (I) for inpatient\n";
 	cout << "or (O) for outpatient.\n";
 	getChoice(choice);		// Displays options and validates input.
 	
 	// Format output
 	cout << fixed << showpoint << setprecision(2);
 
 	
 	switch(choice)
 	{
 		//Inpatient choice
 		case 'i' :
 		case 'I' : cout << "How long in days did the patient stay at the hospital: ";
 					  cin   >> daysStayed;
 					  
 					  // Validate input for daysStayed. Cannot be less than 0.
 					  while (daysStayed < 0)
 					  {
 					   		cout << "Please enter a number 0 or more: ";
 					   		cin   >> daysStayed;
 					   }
 					   
 					  cout << "Enter the total cost of all medications: ";
 					  cin    >> medicationCost;
 					
 					// Validate input for medicationCost. Cannot be less than 0
 					 while (medicationCost < 0)
 					 {
 					 	cout << "Please enter a number 0 or more: ";
 					 	cin	>> medicationCost;
 					 }
 					 
 				
 					  cout  << "Enter the total cost of all hospital services: ";
 					  cin	  >> servicesCost;
 					 // Validate input for servicesCost. Cannot be less than 0
 					 while (servicesCost < 0)
 					 {	
 					 	cout <<  "Please enter a number 0 or more: ";
 					 	cin	>> servicesCost;
 					 }
 					  
 					  // Calculate the total charges
 					  total =  totalCharges(daysStayed, medicationCost, servicesCost);
 					  
 					  // Display the total charges.
 					  cout << "The total cost of this patient's hospital stay is $ "
 					  	     << total << ".\n";
 					  break;
 	
 	// Outpatient choice
 	   case 'o' :
 	   case 'O' : cout << "Enter the total cost of all medications: ";
 					  cin   >> medicationCost;
 					
 					// Validate input for medicationCost. Cannot be less than 0
 					 while (medicationCost < 0)
 					 {
 					 	cout << "Please enter a number 0 or more: ";
 					 	cin   >> medicationCost;
 					 }
 					 
 				      cout  << "Enter the total cost of all hospital services: ";
 					  cin	  >> servicesCost;
 					 
 					 // Validate input for servicesCost. Cannot be less than 0
 					 while (servicesCost < 0)
 					 {	
 					 	cout <<  "Please enter a number 0 or more: ";
 					 	cin   >> servicesCost;
 					 }
 					  
 					// Calculate the total charges
 					total = totalCharges(medicationCost,servicesCost);
 					
 				   // Display the total charges.
 					cout << "The total cost of this patient's hospital stay is $ "
 					  	   << total << ".\n";
 					break;
	}
	return 0;
}
 	
 	
 	
 	
 	
//**********************************************************************
// Definition of function getChoice.
// function recieves user character input to select an option. Uses reference variable.
//**********************************************************************

void getChoice(char choice)
{
		cout << "Enter choice I or O: " << endl;
		cin   >> choice;
		
		// Validatation: Make sure choices I or O are used.
		while(choice != O && choice != o && choice != I && choice != i)
			{ 
				cout << "Enter only O for outpatient records or I\n";
				cout << "for inpatient records.\n";
				cin   >> choice;
			}
}
	
//**********************************************************************
// Definition of function totalCharges. Called from inpatient selection.
// Function calculates the total charges and returns result for inpatients.
//**********************************************************************

double totalCharges(int daysStayed, double medicationCost, double servicesCost, double RATE)
{	
	// Calculate total charges. 
	return (daysStayed * RATE) + servicesCost + medicationCost;
	
}

//**********************************************************************
// Definition of function totalCharges. Called from outpatient selection. 
// Function calculates the total charges and returns result for outpatients. 
//**********************************************************************

double totalCharges(double medicationCost, double servicesCost)
{
	// Calculate total charges. 
	return (medicationCost + servicesCost);
	
}


}


*edit included all source code.

This post has been edited by J-e-L-L-o: 20 March 2011 - 12:04 AM


Is This A Good Question/Topic? 0
  • +

Replies To: need help with declaration errors in function

#2 EarthShaker   User is offline

  • D.I.C Head

Reputation: 55
  • View blog
  • Posts: 186
  • Joined: 16-March 11

Re: need help with declaration errors in function

Posted 20 March 2011 - 12:08 AM

If you're trying to compare a char to I and O, you'll need to put them in single quotes.

while(choice != 'O' && choice != 'o' && choice != 'I' && choice != 'i')


Also, you can use tolower so you would only compare your char to lower case chars instead of lower or uppercase.

Regards
Es.

This post has been edited by EarthShaker: 20 March 2011 - 12:10 AM

Was This Post Helpful? 1
  • +
  • -

#3 J-e-L-L-o   User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 204
  • Joined: 23-January 11

Re: need help with declaration errors in function

Posted 20 March 2011 - 12:41 AM

thanks thats what i needed!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1