Variable problems

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.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>	

using namespace 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";
      else if (cardValue[x] == 11)
           card[x] = "Jack";
      else if (cardValue[x] == 12)
           card[x] = "Queen";
      else if (cardValue[x] == 13)
           card[x] = "King";
      else
          cout << "";
  }   
  
  for (x = 1; x <= 100; ++x)
  {
      cout << card[x] << "\n";
  }
    
            
  cin.get();
  return 0;
}
Last edited on
"Ace" is a string (a zero-terminated array of char).
card[x] is a single char.

I suspect what you would like to do is something like:
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstring>

...

char theCards[ 100 ] = "";
...
strcat( theCards, "Ace " );
...
strcat( theCards, "King" );
...

cout << theCards << endl;


BTW, you also need to #include <cstdlib> to use the rand() function there.

Hope this helps.
Ok so now my code is :

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//High-Low card Program
//Written by: George Carlson

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

using namespace 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";
      else if (cardValue[x] == 11)
           card[x] = "Jack";
      else if (cardValue[x] == 12)
           card[x] = "Queen";
      else if (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.
Ooops is see my mistake i said

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;
Last edited on
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.
Topic archived. No new replies allowed.