Substrings and Length bracketing

closed account (91AfSL3A)
I am trying to create a program that reads card notation from ex "AS" to "Ace of Spades I am having difficulty formatting my variables with out an error


1
2
3
string card;
   string value = card.substr(0;card.length() -1);// I want this to return First character in string
   string suit = card.substr(1; card.length() 1);// I want this to return second character in string 
Last edited on
std::string overloads the [] operator, so you can do this:
1
2
3
std::string str = "AS"
char c = str[0]; // first character A
char d = str[1]; // second character S 
Last edited on
closed account (91AfSL3A)
Thanks for the response I'm not sure if I asked the question correctly, new to the site but If I did it like that wouldn't I have to list all possibilities like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main ()
{
    std::string str = "A123456789TJQK";
    char a = str[0]; // first character A
    char b = str[1]; // second character 1
    char c = str[2]; // so do I have to keep listing all variables
    //Is there a shortcut for this??
    std::string str1 = "HSDC";
    cout << "Please enter a card notation: ";// The input is picked by user
    cin >> str;

    if (str == char a)
	{
    cout << "Ace of " << // I also need to output suit here too 
    } else if { (str == char b)
    cout << "Two of " << // etc.. etc 
    
    return 0;
}
Last edited on
There are two stages, first get the separate characters of the user input. That was the original question.

Now you want to translate that single char into a whole word. You could use a series of if-else statements, or a switch-case with all the possibilities.

An alternative is to lookup the values in a table, here's an example:
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
51
#include <iostream>
#include <string>

    using namespace std;

bool translateCard (string input, string & result);

int main()
{
    string str, cardname;

    cout << "Please enter a card notation: "; // The input is picked by user
    cin >> str;

    if (translateCard(str, cardname))
        cout << cardname << endl;
    else
        cout << "Invalid input" << endl;

    return 0;
}

bool translateCard (string input, string & result)
{
    static const string rank = "A23456789TJQK";
    static const string suit = "HSDC";

    static const string rname[13] =
        { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
          "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
    static const string sname[4] =
        { "Hearts", "Spades", "Diamonds", "Clubs" };

    result = "";

    if (input.size() != 2)      // Input must be exactly 2 chars
        return false;

    char a = toupper(input[0]); // first character
    char b = toupper(input[1]); // second character

    size_t apos = rank.find(a);    // validate a and b
    size_t bpos = suit.find(b);

    if ((apos == string::npos) || (bpos == string::npos))
        return false;

    result = rname[apos] + " of " + sname[bpos];

    return true;
}
Topic archived. No new replies allowed.