I'm trying to create a program that will deal a random card, and I am VERY confused about how to go about making it. I understand arrays/strings in general but I have no idea where to start. My professor states that we will use the function rand() which returns pseudo- random numbers starting with the seed. Seed the random number generator with the function srand({number}). He also suggests to use the function time(); xTime = time(NULL); (I have no idea what that means) Also, something about stand(time(NULL));

I THINK Ive created my arrays right, but thats about it. Im very new to programming and its not coming easy to me, so please dont be too hard on me. Thanks

#include <iostream>
#include <string>
#include "math.h"
#include "time.h"


int main()
{
        int suit[4] = {heart, diamond, club, spade};

        int rank[13] = {ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king};

        system("pause");
}
srand()
{
        std::cout >> suit[4] >> rank[13];
       
        return 0;
}

Recommended Answers

All 4 Replies

>>(I have no idea what that means)
time() is a function that's declared in time.h (or in c++ <ctime>). It returns an unsigned integer that contains the number of seconds that has elapsed since some predetermined date -- it used to be Jan 1970, but I'm not sure if that has changed or not.

So the easiest way to seed the random number generator with a unique value is to use the time() function because it will never ever return the same number twice. srand( (unsigned int) time(0) ); srand() is a standard C function so you don't have to declare it yourself. Just call it from the beginning of main(). So change the name of the function that begins on line 15 to something else.

#include <ctime>

int main()
{
   srand( (unsigned int) time(0) );
   // blabla
}

I'm trying to create a program that will deal a random card, and I am VERY confused about how to go about making it. I understand arrays/strings in general but I have no idea where to start. My professor states that we will use the function rand() which returns pseudo- random numbers starting with the seed. Seed the random number generator with the function srand({number}). He also suggests to use the function time(); xTime = time(NULL); (I have no idea what that means) Also, something about stand(time(NULL));

I THINK Ive created my arrays right, but thats about it. Im very new to programming and its not coming easy to me, so please dont be too hard on me. Thanks

#include <iostream>
#include <string>
#include "math.h"
#include "time.h"


int main()
{
int suit[4] = {heart, diamond, club, spade};

int rank[13] = {ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king};

system("pause");
}
srand()
{
std::cout >> suit[4] >> rank[13];

return 0;
}

Code tags please. It makes it easier to read.

[code]

// paste code here

[/code]

You are going to have a problem with these lines:

int suit[4] = {heart, diamond, club, spade};
        int rank[13] = {ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king};

If you haven't defined "heart", "diamond", "club", "spade", "ace", "jack", "queen", "king", which you haven't, then C++ isn't going to know what to do with them. I assume you intend them to be strings, in which case you can't have them as elements in an integer array.

Get rid of the brackets on lines 14 and 16.

#include <iostream>
#include <string>
#include "math.h"
#include "time.h"


int main()
{
int suit[4] = {heart, diamond, club, spade};

int rank[13] = {ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king};

system("pause");
}
srand()
{
std::cout >> suit[4] >> rank[13];

return 0;
}

Line 17. cout uses the << operator, not the >> operator.

Line 15: Change it to srand (time (NULL)); Look at the links below on how you can use random numbers.

http://www.cplusplus.com/reference/clibrary/cstdlib/srand.html
http://www.cplusplus.com/reference/clibrary/cstdlib/rand.html

I don't know that this assignment requires an array. I would leave the array out for now and just have a variable called suit and a variable called rank . Your job is to generate a number from 1 to 4 and a number from 1 to 13. Get that working and displaying those numbers first, then worry about turning them into strings and displaying them. Tackle the assignment a step at a time.

I used a program I found to output 2 random numbers. the first is out of 4 and the 2nd is out of 13.

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
		int random_integer; 
		for(int index=0; index<1; index++)
	{ 
        random_integer = (rand()%4)+1; 
        cout << random_integer << endl; 
    } 
	srand((unsigned)time(0)); 
		int random_integer2; 
		for(int index=0; index<1; index++)
	{ 
        random_integer2 = (rand()%13)+1; 
        cout << random_integer2 << endl; 
    } 
}

For example, the output says:

4
12
Press any key to continue...


edit: I dont know why my code isn't tagging.. I used the

"code here"

Now how would I go about providing the strings, and giving the number character values?

I used a program I found to output 2 random numbers. the first is out of 4 and the 2nd is out of 13.

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
		int random_integer; 
		for(int index=0; index<1; index++)
	{ 
        random_integer = (rand()%4)+1; 
        cout << random_integer << endl; 
    } 
	srand((unsigned)time(0)); 
		int random_integer2; 
		for(int index=0; index<1; index++)
	{ 
        random_integer2 = (rand()%13)+1; 
        cout << random_integer2 << endl; 
    } 
}

For example, the output says:

4
12
Press any key to continue...


edit: I dont know why my code isn't tagging.. I used the

"code here"

Now how would I go about providing the strings, and giving the number character values?

Your code tags worked. Notice that the background is tan and the spacing is retained. To use C++-style code tags, do this:

[code=cplusplus] // pasted code here

[/code]

Regular code tags:

[code]

// pasted code here

[/code]

Example of regular code tags:

int main ()
{
     return 0;
}

Example of C++ code tags:

int main ()
{
     return 0;
}

As to your code:

#include <cstdlib> 
#include <ctime> 
#include <iostream>

using namespace std;

int main() 
{ 
    srand((unsigned)time(0)); 
		int random_integer; 
		for(int index=0; index<1; index++)
	{ 
        random_integer = (rand()%4)+1; 
        cout << random_integer << endl; 
    } 
	srand((unsigned)time(0)); 
		int random_integer2; 
		for(int index=0; index<1; index++)
	{ 
        random_integer2 = (rand()%13)+1; 
        cout << random_integer2 << endl; 
    } 
}

You can delete line 16. You already seeded the random number generator in line 9. You only need to do it once.

As for how you can use a string array to do the output, here's a code sample:

string suits[] = {"diamonds", "spades", "hearts", "clubs"};
cout << "Enter a number from 1 to 4: ";
int suit;
cin >> suit;
cout << suits[suit - 1];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.