In: Computer Science
How to transfer the below two global varibale into local variable where this two variable are used. PLEASE HELP
// Read in the user's height
int customerAge = 0;
double customerHeight = 0;
// *******************************************
// Functions
// *******************************************
void readHeight();
void printCustomerInfo();
bool askToDoAnother();
int main() {
bool keepReading = 1;
while (keepReading) {
// Read in the customer info:
// ... Read the height
readHeight();
// ... Read Age
cout << "Enter their age: ";
cin >> customerAge;
// Display a summary:
printCustomerInfo();
// Do another?
keepReading = askToDoAnother();
}
return 0;
}
// Read in the user's height
void readHeight() {
cout << "Enter customer height (feet): ";
cin >> customerHeight;
while (customerHeight > MAX_HEIGHT || customerHeight < MIN_HEIGHT) {
cout << fixed << setprecision(1);
cout << "ERROR: Height must be between " << MIN_HEIGHT
<< " and " << MAX_HEIGHT << " feet.\n";
cout << "Enter customer height (feet): ";
cin >> customerHeight;
}
}
// Print the customer's info to the screen.
void printCustomerInfo() {
cout << fixed << setprecision(1);
cout << "\n";
cout << "Customer summary: \n";
cout << "\tHeight: " << customerHeight << " feet\n";
cout << "\tAge: " << customerAge << endl;
cout << "\n";
}
// Ask the user if they want to enter more data.
bool askToDoAnother() {
char choice = ' ';
// Read in the user's selection
cout << "Enter another customer? (Y/N) ";
cin >> choice;
return (choice == 'Y' || choice == 'y');
}
// Ask for a customer's info, and display it to the screen.
Solution:
Look at the below code for better understanding..........
You just need to modify the functions as
void readHeight(double &);
void printCustomerInfo(double,int);
Full Code:
void readHeight(double &);
void printCustomerInfo(double,int);
bool askToDoAnother();
int main() {
int customerAge;
double customerHeight;
bool keepReading = 1;
while (keepReading) {
// Read in the customer info:
// ... Read the height
readHeight(customerHeight);
// ... Read Age
cout << "Enter their age: ";
cin >> customerAge;
// Display a summary:
printCustomerInfo(customerHeight,customerAge);
// Do another?
keepReading = askToDoAnother();
}
return 0;
}
// Read in the user's height
void readHeight(double &customerHeight) {
cout << "Enter customer height (feet): ";
cin >> customerHeight;
while (customerHeight > MAX_HEIGHT || customerHeight < MIN_HEIGHT) {
cout << fixed << setprecision(1);
cout << "ERROR: Height must be between " << MIN_HEIGHT
<< " and " << MAX_HEIGHT << " feet.\n";
cout << "Enter customer height (feet): ";
cin >> customerHeight;
}
}
// Print the customer's info to the screen.
void printCustomerInfo(double customerHeight,int customerAge) {
cout << fixed << setprecision(1);
cout << "\n";
cout << "Customer summary: \n";
cout << "\tHeight: " << customerHeight << " feet\n";
cout << "\tAge: " << customerAge << endl;
cout << "\n";
}
// Ask the user if they want to enter more data.
bool askToDoAnother() {
char choice = ' ';
// Read in the user's selection
cout << "Enter another customer? (Y/N) ";
cin >> choice;
return (choice == 'Y' || choice == 'y');
}
I hope this would help......................:-))