Untitled
unknown
plain_text
4 years ago
1.3 kB
9
Indexable
#include <iostream> using namespace std; class Game{ public: //Running mostly in here //Constructor Game(){ cout << "You encounter a bear in the woods, and you can choose what happens." << endl; cout << "1. RUN!" << endl; cout << "2. Use bear spray on its eyes." << endl; cout << "3. Poke it with a stick" << endl; choice = getInput(3); } private: //adding variables here only access in Game class int choice; private: //add methods like functions that we can call inside the game int getInput(int numChoices){ while(true){ int input = 0; cin >> input; cin.get(); if(cin.fail()){ cout << "Please enter a number: " << endl; cin.clear(); cin.ignore(256, '\n'); } else{ if(input > 0 && input <= numChoices){ return input; } else{ cout << "Please enter a number between 1 and " + to_string(numChoices) << endl; } } } } }; int main() { new Game(); return 0; }
Editor is loading...