why do i keep getting the same1??

#include <iostream>

#include <ctime>

using namespace std;

struct card
{
	int value;

	char suit[10];

	char faceCard[10];
};

int deal(card deck[maxcards], int decksize, int &z);

void shuffle(card deck[], int decksize);

int score =0;

int main()
{
	int scoretracker =0;

	card deck[52]={{11, "s", "ace"}, {11, "h", "ace"}, {11, "c", "ace"}, {11, "d", "ace"}, {10, "s", "jack"}, {10, "h", "jack"}, {10, "c", "jack"}, {10, "d", "jack"}, {10, "s", "queen"}, {10, "h", "queen"}, {10, "c", "queen"}, {10, "d", "queen"}, {10, "s", "king"}, {10, "h", "king"}, {10, "c", "king"}, {10, "d", "king"}, {1, "s", "" }, {2, "s", "" }, {3, "s", "" }, {4, "s", "" }, {5, "s", "" }, {6, "s", "" }, {7, "s", "" }, {8, "s", "" }, {9, "s", "" }, {1, "h", "" }, {2, "h", "" }, {3, "h", "" }, {4, "h", "" }, {5, "h", "" }, {6, "h", "" }, {7, "h", "" }, {8, "h", "" }, {9, "h", "" }, {1, "c", "" }, {2, "c", "" }, {3, "c", "" }, {4, "c", "" }, {5, "c", "" }, {6, "c", "" }, {7, "c", "" }, {8, "c", "" }, {9, "c", "" }, {1, "d", "" }, {2, "d", "" }, {3, "d", "" }, {4, "d", "" }, {5, "d", "" }, {6, "d", "" }, {7, "d", "" }, {8, "d", "" }, {9, "d", "" }};  
	
	while(true)
	{
		int userinput;

		cout<<"If you want to play, press 1, othwerwise press 2 to exit"<<endl;
		cin>>userinput;

			if(userinput ==1)
			{
		
				shuffle(deck, 52);
			
				for(int i = 0, x = 0; i < 52; i++, x++)
				{

				deal(deck, 52, x);

				scoretracker = score;

				
			}

				cout<<"Your correct score so far is: "<<scoretracker<<endl;

				continue;

			}

		else
		break;
	}

}

void shuffle(card deck[], int decksize)
{
	for(int i =0; i < decksize; i++)
	{
		srand(time(0));
		
		int number = rand() % 52;

		if(true)
		{
			deck[i] = deck[number];

		}
	}
}

int deal(card deck[], int decksize, int &z)
{
	
	char guess[10];

	cout<<"Now guess high or low"<<endl;
	cin>>guess;

	for(; z < decksize;)
	{
		if(deck[z].value == 10 || deck[z].value == 11)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<deck[z].faceCard<<endl;
	
			score++;
		
			 break;
		}
	
		else if(deck[z].value == 1)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;
		
			break;
		}
		
		else if(deck[z].value == 2)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}

		else if(deck[z].value == 3)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}
	
		else if(deck[z].value == 4)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}
	
		else if(deck[z].value == 5)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;
		
			break;
		}

		else if(deck[z].value == 6)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}

		else if(deck[z].value == 7)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}
	
		else if(deck[z].value == 8)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;

			break;
		}

		else if(deck[z].value == 9)
		{
			cout<<"The card is high "<<deck[z].value<<" "<<deck[z].suit<<" "<<endl;
	
			score++;


			break;
		}
	}

	return score;
}

Dani AI

Generated

Short diagnosis and next steps (builds on and ):

Calling the pseudo-random generator seed inside the shuffle loop causes the same sequence to appear repeatedly because the seed source (time in seconds) doesn't change between rapid calls. Seeding belongs once at program startup, or better yet replace C-style rand/srand with the C++11 random facilities for higher quality and clearer code (std::rand/std::srand behavior).

There is also a fundamental bug in the shuffle itself: assigning deck[i] = deck[number] copies a card into position i and loses the card originally at i, producing duplicates. Either swap the two elements or use the standard library shuffle which is simple and correct:

#include <algorithm>
#include <random>

std::random_device rd;
std::mt19937 rng(rd());
std::shuffle(deck, deck + decksize, rng);

See and std::mt19937.

Other logic issues to fix while refactoring: the deal routine reads a guess but never compares it, and it uses many repeated else if blocks that always increment score. Replace that with a single comparison between the current and previous card values and apply the player's guess (use std::string for input). Keep index advancement in one place (either main or deal), avoid globals for score, and use decksize rather than hard-coded 52. Quick debugging: print generated indices or a few shuffled decks to confirm randomness and correctness before wiring up the game logic.

Recommended Answers

All 2 Replies

In your "shuffle" function, try moving the random number seed function "srand" out of the loop. You're re-seeding the generator with the same number on each pass, since you're in and out of that routine within a second.

Yep you only need it once for the entire program. Then just call rand() % 52 wherever you need it.

If you're going to be doing a bunch of random numbers at a time, its a good idea to have it wait a bit before generating the next number.

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.