// P41.cpp - A simple elevator for 4 floors and a basement with a // close door button and 5 keys for floors #include #include using namespace std; void close_door(); int change_floor(int choice, int floor); int main( ) { int key; int floor = 0; cout << "Press a Key \n"; cin >> key; if(key == 0) { close_door(); if( floor != 0){ // otherwise nothing happens cout << "I am moving to the Basement \n"; floor = change_floor(0, floor); } } else if(key == 1) { close_door(); if( floor != 1){ // otherwise nothing happens cout << "I am moving to the the First Floor \n"; floor = change_floor(1, floor); } } else if(key == 2) { close_door(); if( floor != 2){ // otherwise nothing happens cout << "I am moving to the the Second Floor \n"; floor = change_floor(2, floor); } } else if(key == 3) { close_door(); if( floor != 3){ // otherwise nothing happens cout << "I am moving to the the Third Floor \n"; floor = change_floor(3, floor); } } else if(key == 4) { close_door(); if( floor != 4){ // otherwise nothing happens cout << "I am moving to the the Fourth Floor \n"; floor = change_floor(4, floor); } } else { close_door(); } return 0; } void close_door() { cout << "I am closing the door \n"; } int change_floor(int choice, int floor) { int move; move = floor - choice; // Note floor = choice is already considered in main if(move < 0) { cout << "Move up by " << abs(move) << endl; } else { cout << "Mode down by " << abs(move) << endl; } floor = choice; cout << "You are at floor " << floor << endl; return floor;

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter6: User-defined Functions
Section: Chapter Questions
Problem 34SA
icon
Related questions
Question

// P41.cpp - A simple elevator for 4 floors and a basement with a

// close door button and 5 keys for floors

#include<iostream>

#include<cmath>

using namespace std;

void close_door();

int change_floor(int choice, int floor);

int main( )

{

int key;

int floor = 0;

cout << "Press a Key \n";

cin >> key;

if(key == 0)

{

close_door();

if( floor != 0){ // otherwise nothing happens

cout << "I am moving to the Basement \n";

floor = change_floor(0, floor);

}

}

else if(key == 1)

{

close_door();

if( floor != 1){ // otherwise nothing happens

cout << "I am moving to the the First Floor \n";

floor = change_floor(1, floor);

}

}

else if(key == 2)

{

close_door();

if( floor != 2){ // otherwise nothing happens

cout << "I am moving to the the Second Floor \n";

floor = change_floor(2, floor);

}

}

else if(key == 3)

{

close_door();

if( floor != 3){ // otherwise nothing happens

cout << "I am moving to the the Third Floor \n";

floor = change_floor(3, floor);

}

}

else if(key == 4)

{

close_door();

if( floor != 4){ // otherwise nothing happens

cout << "I am moving to the the Fourth Floor \n";

floor = change_floor(4, floor);

}

}

else

{

close_door();

}

return 0;

}

void close_door()

{

cout << "I am closing the door \n";

}

int change_floor(int choice, int floor)

{

int move;

move = floor - choice;

// Note floor = choice is already considered in main

if(move < 0)

{

cout << "Move up by " << abs(move) << endl;

}

else

{

cout << "Mode down by " << abs(move) << endl;

}

floor = choice;

cout << "You are at floor " << floor << endl;

return floor;

}

Exercise 4.3
Modify the P41.cpp program and use a switch statement instead of the if... else if...else statement
in the main. Call your new program ex43.cpp.
Transcribed Image Text:Exercise 4.3 Modify the P41.cpp program and use a switch statement instead of the if... else if...else statement in the main. Call your new program ex43.cpp.
Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Mathematical functions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning