Question

In: Computer Science

How to transfer the below two global varibale into local variable where this two variable are...

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.

Solutions

Expert Solution

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......................:-))


Related Solutions

What is the difference between local and global variable? Functions are also called ____________ In python,...
What is the difference between local and global variable? Functions are also called ____________ In python, to import functions from modules we use the keyword ‘import’, assume that you have a module named “mySelf” the mySelf module has “getMyName”, “getMyage” , and many more functions. 3 points. Write a statement that imports 3.a. Only getMyName function 3.b all other functions from the module How do you differentiate a variable from a function? Write a function named times_ten. The function should...
The Classical School and the identification of a local/global problem. a) Identify a local/global problem that...
The Classical School and the identification of a local/global problem. a) Identify a local/global problem that might be caused by the application of a policy that is based on a theory or concept of the Classical school. Your answer needs to provide at least three paragraphs. The first paragraph identifies and explains in your own words the concept or theory of the Classical School The second paragraph identifies and explains the a local/global problem. The third paragraph explains the connection...
comment on how local business can be more competitive in the global market
comment on how local business can be more competitive in the global market
Apply variable costs into these questions below. MINIMUM 200 words ONLY! Describe a situation where variable...
Apply variable costs into these questions below. MINIMUM 200 words ONLY! Describe a situation where variable costs are applied outside the classroom. In your career? Your personal life? A current event? Another class? Did you describe variable costs in a few sentences with enough detail to indicate that you understand the concept? Does the topic you described relate to the situation or incident that you described? (For example, do not describe the IMA Statement of Ethics, then explain how you...
How do the global and local reflexes address the optimization of the ventilation : perfusion ratio...
How do the global and local reflexes address the optimization of the ventilation : perfusion ratio (keeping it close to 1)?
Understand how an intercompany sale; where the book value exceeds the transfer price of a depreciable...
Understand how an intercompany sale; where the book value exceeds the transfer price of a depreciable asset is handled.
Explain how intersectionality impacts gender health disparities – local or global – by discussing an example...
Explain how intersectionality impacts gender health disparities – local or global – by discussing an example from the news.
Discuss the six types of worldwide strategies (local, regional, global, etc.). What are the two major...
Discuss the six types of worldwide strategies (local, regional, global, etc.). What are the two major factors that change when comparing these strategies (product standardization, etc.)? with real world example
1. What are. different approaches. to global market? global versus. local responsiveness.?
1. What are. different approaches. to global market? global versus. local responsiveness.?
To provide three examples of how global, regional and local problems can affect the environment. half...
To provide three examples of how global, regional and local problems can affect the environment. half page per example.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT