C++ Help!! 1.1 Pass-by-reference Assignment For this part of the lab (lab3a), we
ID: 3797328 • Letter: C
Question
C++ Help!!
1.1 Pass-by-reference Assignment
For this part of the lab (lab3a), we are going to pretend that we are on a text-based adventure game. Our life will be measured via hit points and we start with 10 hit points. If we want to increase our hit points, we can quaff a potion using the command quaff potion and it will increase our hit points by 2.
· Ask the user what they would like to do. Have the user enter quaff potion using two words. As a reminder, you will have to use getline because it is two words. You can use a string or a c-string.
· Using pass-by-reference, create a void function called quaff that adds two hit points.
· In main, display the original hit points (10) and new hit points after quaffing the potion (12)
Here is an example execution of the program:
--bash-4.1$ ./lab3a
You have 10 hit points.
What would you like to do?:
quaff potion
You have quaffed the potion!
You have 12 hit points.
-bash-4.1$
--bash-4.1$ ./lab3a
You have 10 hit points.
What would you like to do?:
quaff potion
You have quaffed the potion!
You have 12 hit points.
-bash-4.1$
Explanation / Answer
#include <iostream>
using namespace std;
void quaff (int &hitPoints){
hitPoints = hitPoints + 2;
}
int main() {
int hitPoints = 10;
string line;
cout<<"You have "<<hitPoints<<" hit points."<<endl;
cout<<"What would you like to do?:"<<endl;
getline(cin, line);
quaff(hitPoints);
cout<<"You have quaffed the potion!"<<endl;
cout<<"You have "<<hitPoints<<" hit points."<<endl;
return 0;
}
Output:
You have 10 hit points.
What would you like to do?:
quaff potion
You have quaffed the potion!
You have 12 hit points.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.