Rand() not working

Nov 22, 2012 at 4:58pm
I'm trying to create and simple card game. When i test it, it gives me a random symbol that has nothing to do with the code. Am I using rand() wrong?
#include "Header.h"

class Opponent_AI{
	public:
		void Swap_Cards(){
		
		}
	
};

int main ()
{
	int clubs[13], diamonds [13], hearts[13], spades[13]; //creates sub-sects of the deck 
	int rand_num1, rand_num2, rand_num3, rand_num4; //creates some random nums
	int randClubs, randDiamonds, randHearts, randSpades; //creates nums for the card
	string card1, card2, card3, card4; 

	srand(time(NULL));
	rand_num1 = rand() % 4 + 1;//
	rand_num2 = rand() % 4 + 1;//Generates random nums for what suit
	rand_num3 = rand() % 4 + 1;//
	rand_num4 = rand() % 4 + 1;//
	
	randClubs = rand() % 13 + 1;
	randDiamonds = rand() % 13 + 1;//generates a card for the random suit
	randHearts = rand() % 13 + 1;
	randSpades = rand() % 13 + 1;
	
	switch (rand_num1) //determines the card
		{
		case 1://clubs
			card1 = randClubs;
			break;

		case 2://diamonds
			card1 = randDiamonds;
			break;

		case 3://hearts
			card1 = randHearts;
			break;

		case 4://spades
			card1 = randSpades;
			break;

		default:
			cout<<"error";
			break;
		}

	cout<<card1;
	
	system("pause");
	return 0;
}

My header file includes iostream, string, ctime, Time.h
Also, can someone tell me the corrects headers to use?
Nov 22, 2012 at 5:12pm
You can't assign integers to strings like that. The reason it works is because string has an operator= that takes a char as argument so it implicitly casts the int to a char and put that in the string. It is not really what you want. If you want to convert an int to a string you can use std::ostringstream or if your compiler is up to date you can use the std::to_string function.
Last edited on Nov 22, 2012 at 5:12pm
Nov 22, 2012 at 5:15pm
The problem is that randClubs is an integer and card1 is a string. Conversion between them is not that simple in c++. See http://www.cplusplus.com/articles/D9j2Nwbp/

About headers, <ctime> is just the way "time.h" is supposed to be called in C++. There is no need to have both.
Nov 22, 2012 at 5:20pm
card1 is a string. When you set the first character of the string to a number between 1 and 13, you are setting it to one of the values found here: http://www.asciitable.com/

If you actually want it to read as 13, make card1 an int, long or short. Note that card1 does not actually contain a suit. it just contains that number.

Here's another solution using stringstreams:
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
#include <cstdlib>
#include <ctime>
#include <sstream>
#include <iostream>

int main()
{
    srand(time(NULL));
    std::stringstream card1;

    rand_suit = rand() % 4 + 1;
    rand_val  = rand() % 13 + 1;


    card1 << rand_val;

    switch (rand_suit)
    {
    case 1: card1 << " Clubs"; break;
    case 2: card1 << " Diamonds"; break;
    case 3: card1 << " Spades"; break;
    case 4: card1 << " Hearts"; break;
    }

    cout << card1.str();
    return 0;
}
Last edited on Nov 22, 2012 at 5:20pm
Nov 22, 2012 at 6:00pm
Thanks for all the help, ill fix it now. The to_string function solved my problem
Last edited on Nov 22, 2012 at 6:05pm
Topic archived. No new replies allowed.