0

why when i test this the result is 1 when i wrote 1

#include <iostream> using namespace std; class elevator { private: int elevator_loc; public: void initdata() { cout << "\n Enter the floor you on it: "; cin >> elevator_loc; cout << "Elevator is now on floor " << elevator_loc; } void move_up() { elevator_loc = elevator_loc + 3; } void move_down() { --elevator_loc; } void displaydata() { cout << "\nYou are now on floor:" << elevator_loc; } }; void main() { char choice = 'q'; elevator elev1; elev1.initdata(); while (choice != 'q') { cout << "\n Enter + to go up, Enter - to go down, q to quit"; cin >> choice; if (choice == '+') { cout << "You will move up"; elev1.move_up(); } if (choice == '-') { cout << "You will move down"; elev1.move_down(); } } elev1.displaydata(); }

19th Mar 2020, 12:18 PM
Maher Talip
Maher Talip - avatar
4 Answers
+ 2
Maher Talip , I can think of 2 approaches. 1st will be to initialize choice variable to something other than 'q' char choice ='\0'; 2nd approach will be to replace while loop by a do while loop. So that it executes at least once. do{ //loop body }while(choice!='q');
19th Mar 2020, 2:16 PM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 3
That's how you have designed it to work. It shows what you give as input. `choice` is initialzed to 'q' so while loop is never executed.
19th Mar 2020, 12:27 PM
šŸ‡®šŸ‡³OmkaršŸ•‰
šŸ‡®šŸ‡³OmkaršŸ•‰ - avatar
+ 1
thank you bro it's work and i learned something today.thank you again.
19th Mar 2020, 4:19 PM
Maher Talip
Maher Talip - avatar
0
i need to use while loop how i can?
19th Mar 2020, 2:03 PM
Maher Talip
Maher Talip - avatar