Trying to make a user loop on which for when the user either enters (1 for attack or 2 for heal) on which it then activates the random number generator and displays the number. For the users attack or heal values. What is the best way I can do this.
int userAnswer, generatedValue, playeraction;
int lowerBound = 1, upperBound = 10;
int main() {
cout << "Player HP: " << endl; // *addiing hit points for player should be 100
cout << "Enemy HP : " << endl; // ** "
std::cout << "Select an action..." << endl;
cout << "1.) Attack" << endl; // *** set 1 as attack and combined the random value gen to it
cout << "2.) Heal" << endl; // **** set 2 as heal option to heal player also random value gen to it
cout << ":" << input << endl;
#if 0 /// your code
#include "pch.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <stdio.h> /// use <cstdio> don't mix the 2 types
#include <string>
#include <windows.h>
#include <cstdlib>
usingnamespace std; /// so don't keep using std:: later on...
// Why these bunch of unused (global) variables?
int userAnswer, generatedValue, playeraction;
int lowerBound = 1, upperBound = 10;
int main() {
cout << "Player HP: " << endl; // *addiing hit points for player should be 100
cout << "Enemy HP : " << endl; // ** "
std::cout << "Select an action..." << endl;
cout << "1.) Attack" << endl; // *** set 1 as attack and combined the random value gen to it
cout << "2.) Heal" << endl; // **** set 2 as heal option to heal player also random value gen to it
cout << ":" << input << endl; /// using input before declaring it
std::string playeraction[] = { "1", "2" }; /// error redefining playeraction
int input;
std::cin >> input;
// rndom number gen.
int max, random_number;
cout << "Max integer" << endl;
cin >> max;
srand(time_t(0)); /// so not even a little bit random then
random_number = (rand() % 10) + 1;
cout << random_number << endl;
return 0;
}
#endif
/// Now for a naive solution, but still a reasonable starting point:
#include <cstdlib>
#include <ctime>
#include <iostream>
usingnamespace std;
int main()
{
cout << "Select an option: 1 for attack, 2 for healing.\n";
int userAnswer = 0;
cin >> userAnswer;
srand((unsigned) time(0)); // seed random number generator with current time
int random_number = rand() % 10 + 1;
if (userAnswer == 1)
{
cout << "You have " << random_number << " attack points.\n";
}
elseif (userAnswer == 2)
{
cout << "You have " << random_number << " healing points.\n";
}
else
{
cout << "You made an invalid selection.\n";
// be prepared to handle stream input failure here.
}
return 0;
}