user number display

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.

#include "pch.h"
#include <ctime>
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <windows.h>
#include <cstdlib>
using namespace std;

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;

std::string playeraction[] = { "1", "2" };
int input;
std::cin >> input;

// rndom number gen.
int max, random_number;

cout << "Max integer" << endl;
cin >> max;

srand(time_t(0));
random_number = (rand() % 10) + 1;

cout << random_number << endl;

return 0;
}
Last edited on
I'll try and point out a few of the problems using ///, and then make some suggestions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#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>
using namespace 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>

using namespace 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";
	}
	else if (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;
}
Last edited on
Topic archived. No new replies allowed.