Assigning Random Value

Hey guys, here is my issue. As shown in the code below, I'm using getline, and stringstream to assign an input to a value. I, for the life of me, can not find/figure out, how to do something similar with a random output.

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
//Battle with  Samer
#include <iostream>
#include <string>
#include <sstream>
#include <ctime>
#include <cstdlib>
using namespace std;

const char* PokemonSamer[6] = {
	"Charizard", "Umbreon", "Mew", "Arcanine", "Espurr", "Luxaray"
};
int Samerhealth[6] = { 15, 10, 25, 15, 10, 10};
int Trainerhealth[3]={ 15, 20, 10};
int main()
{
	string mystr;
	string Samer;
	string Trainer;
	
	cout << "Musician Samer wants to battle!" "\n";
	cout << "Go, ";
	srand((unsigned)time(0));
	cout << PokemonSamer[rand() % 6] << "!"<< "\n";
	cout << "Please Choose your Pokemon: \n";
	cout << "Charizard, Slowking, Weavile \n";
	getline(cin, mystr);
	stringstream(mystr) >> Trainer;
	cout << Trainer << ", I choose you! \n";
	cout << "\n";
	cout << "Would you like to attack? (yes or no) \n";
	getline(cin, mystr);
	
	system("PAUSE");
	
}


When I run <PokemonSamer[rand() % 6]>, I want to be able to save whatever that output may be, and save it to <string Samer>. Is this possible?At the very least, I'm looking for a hint/point in the right direction.
Would this work for you?
1
2
Samer = PokemonSamer[rand() % 6];
cout << Samer << "! \n" << endl;
Last edited on
I think, and I will try it in a moment, but will Samer always be a random pick them? When I tried it, I did it backwards:

1
2
PokemonSamer[rand() %6] = Samer;
cout << Samer << "! \n"<< endl;


But I did not have success with it. Like I said, I want Samer, to equal whatever value comes from the random selection, not for Samer to always be a random selection.
That seems to have done it. Thank you! Just for my own sanity, why does it work that way? I would think that, like I asked before, Samer would always equal a random selection?
This printed out the same one each time..

1
2
Samer=PokemonSamer[rand() % 6];
cout << Samer << " " << Samer << " " << Samer << " " << Samer << " " << Samer << endl;

Last edited on
Yeah, I got the same result. Thanks!! I'm really frustrated, because I tried it backwards before I came on here and asked. Lol. Thanks again.
No worries, we've all been there! Ha
Topic archived. No new replies allowed.