Substring???

closed account (91AfSL3A)
1
2
3
4
5
// How do I do assign card.substring here???
   string value = card.substring(0, card.length() -1);
   string suit = card.substring(card.length() -1);
   cout << "Please enter a card notation: ";
   cin >> card;
Last edited on
1
2
3
4
5
if ( card.length() != 0 )
{
   std::string value = card.substr( 0, card.length() - 1 );
   std::string suit = card.back();
}


Or

1
2
3
4
5
if ( card.length() != 0 )
{
   std::string value( card.begin(), std::prev( card.end() ) );
   std::string suit = card.back();
}


I wrote the if condition only to show that you should guarantee that card is not empty. You should not use such declarations inside the if code block because in this case names value and suit will be local relative to it.
Last edited on
Topic archived. No new replies allowed.