Ok so I'm making a program like the card game High-Low where you flip a card and then guess what card is next. well I have 2 arrays. card and cardValue. cardValue holds a number between 1 and 13 and then i have if statements that say if cardValue = 1 make card = "ace", jack for 11 and so on. but when i try to run the program this is the error i get.... invalid conversion from `const char*' to `char' ... how do i fix this? here is my code and thanks in advance.
#include <iostream>
usingnamespace std;
int main()
{
char card[100];
int cardValue[100];
int x;
cout << "\t\tWelcome to the High-Low program\n\n";
cout << "A random card is going to be drawn and you will\n";
cout << "guess if the next card will be higher or lower\n\n";
cout << "If you want to guess High type 'h' or 'H'\n";
cout << "If you want to guess Low type 'l' or 'L'\n\n";
cout << "Hint: Ace is low (1) and if the next card has\n";
cout << "the same value it will automatically be correct\n\n";
for (x = 1; x <= 100; ++x)
{
cardValue[x] = rand() % 13 + 1;
while (cardValue[x] < 1)
{
cardValue[x] = rand() % 13 + 1;
}
card[x] = cardValue[x];
if(cardValue[x] == 1)
card[x] = "Ace";
elseif (cardValue[x] == 11)
card[x] = "Jack";
elseif (cardValue[x] == 12)
card[x] = "Queen";
elseif (cardValue[x] == 13)
card[x] = "King";
else
cout << "";
}
for (x = 1; x <= 100; ++x)
{
cout << card[x] << "\n";
}
cin.get();
return 0;
}
//High-Low card Program
//Written by: George Carlson
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
usingnamespace std;
int main()
{
string card[100];
int cardValue[100];
int x;
cout << "\t\tWelcome to the High-Low program\n\n";
cout << "A random card is going to be drawn and you will\n";
cout << "guess if the next card will be higher or lower\n\n";
cout << "If you want to guess High type 'h' or 'H'\n";
cout << "If you want to guess Low type 'l' or 'L'\n\n";
cout << "Hint: Ace is low (1) and if the next card has\n";
cout << "the same value it will automatically be correct\n\n";
for (x = 1; x <= 100; ++x)
{
srand(time(0)); // seed random number generator
cardValue[x] = rand() % 1 + 1;
card[x] = cardValue[x];
if(cardValue[x] == 1)
card[x] = "Ace";
elseif (cardValue[x] == 11)
card[x] = "Jack";
elseif (cardValue[x] == 12)
card[x] = "Queen";
elseif (cardValue[x] == 13)
card[x] = "King";
else
cout << "";
cout << card[x] << "\n";
}
cin.get();
return 0;
}
Now it outputs the word "Ace" 100 times and then i get an error report thing(the thing that asks if you want to send an error report.
cardValue[x] = rand() % 1 + 1;
When it should have been cardValue[x] = rand() % 13 + 1;
It finally almost works. Now it it outputing the same thing 100 times and not all different things. can someone help me please.
The pseudo-random number generator works like this:
1. It's seeded. This sets the initial value for generation.
2. The next value will be generated based solely on the previous value.
Suppose rand() was this: current_value=current_value+1;
And this is srand(): current_value=parameter;
Now, what time(0) does is return the number of seconds since a fixed point in time (usually, 1970-01-01 00:00:00Z, although I'm not sure if it's standard). The computer can run the program fast enough to call the function a hundred times in less than a second.
So what your program is doing is basically this:
current_value=some_constant_value;
cardValue[x]=current_value+1;
The short version of what helios said (no jab at you intended, helios, it's a good explanation of what's happening in his program): seed the RNG only once, before your for() loop.