Using user inputs in functions

Im writing a code for class that takes 7 inputs from the user as cards and tells them their best poker hand. The input is #S where # is the card number and S is the suit. Using pointers, the program recognizes the first character is 1 for example and the 2nd is H (ace of hearts) and the 1 is assigned to a1 and the H to a2 but in the function if (a1==1), the inner code of the if statement wont execute. This is a simplified form of what I have.

1
2
3
4
5
6
7
8
9
10
11
12
  string a;
  string aval;
  cin >> a;
  char * a3 = &a[0];
  char * a4 = &a[1];
  char a1 = *a3;
  char a2 = *a4;
  if(a1==1){
      if(a2=="H"){
           aval = "Ace of Hearts";
      }
  }
Last edited on
Also for the record, as a test, I wrote cout << a1<<a2; and it recognizes that a1 is 1 and a2 is H but the if statement still wont execute.
Using pointers

Why are you trying to use pointers?

Do you realize that a string is basically an array of characters?

Do you know that there is a difference between character constants 'a' and string constants "a"?

Do you know that there is a difference between the number 1 and the character '1'?


Why not something more like:

1
2
3
4
5
6
7
8
9
string input_value;
string card_value = "INVALID CARD";

cin >> input_value;

// Note the use of the character '1' not the number 1.
if(input_value[0] == '1' && input_value[1] == 'H')
    card_value = "Ace of Hearts";


Ok thanks. I just figured assigning them to a variable would make it easier to use later, and the error messages were making it seem like a pointer was a cleaner option but I'll try this.
God I'm stupid! Your comment made me think why i used pointers in the first place and its because its being inputted through a function,therefore is in a different scope so if I assign variable 'a' to a new variable in scope, treating it like an array would work just fine
Topic archived. No new replies allowed.