Can someone further explain how to use sub string and string find ? I am a beginner in an intro C++ class. I am trying to read a text file for a blackjack game and I need to separate each line into 2 cards per player and count the number of wins each player has at the end of the file.
Ex. JH 8D QH AC 8H 9S
I know you would have to convert each line into a string but what happens next ??
By the way, I have read the reference articles for substring & string find so do not link those pages as a response.
So there are 100 lines of six cards in the file, so would I increment the lines as so ??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Before this the file would be read
// string data = text within file
int main ()
(i, i<0, i++);
string line = data;
string word1, word2;
while (ss >> word1 >> word2)
cout << word1 << " " << word2 << endl;
Then for each blackjack I have to record it with a star, so if player one has 6 wins the output should be :
At line 6, int i; i is declared but is not given an initial value.
At line 7, (i, i<0, i++); i is not given an initial value, so its value is unknown, it could be anything. Then the test i<0 specifies that the loop will continue so long as i is negative. The line ends with a semicolon making this an empty loop which doesn't control anything.
@LowestOne I understand I have to use sub string and string find but I did not want someone's response to be a link and no explanation, since I have already looked at those pages.
Will your code read every line in the txt file ??
From what I can tell, it cuts out all of the white space and counts the numbers am i correct ??
What would happen with your code if I had this line of code ?
10S KD QD QS 2C 6H
there is a 10 and queens. Would a switch be needed to identify the letters ?
Thank you for the corrections. I made a couple mistakes, I just wanted to make sure the concept was there.
Your output makes sense, but I do not know how to make sure the program knows every third line would be the first player and so on.
while (data[c] != '\0' /* or '\n' */)
// One Line
card = 0;
person = 0;
first_card = 1;
value = 0;
while (data[c] != ' ' && first_card)
case'1': value += 10; first_card--; break;
case'A': value += 11; first_card--; break;
default: first_card--; break;
So far this will read in lines from the file and check for special cases in the first card. How do I continue this program with arrays?? My teacher told us to use them but never explained how to use them for this particular game.
It's kind of hard to give guidance on what to do next, as it depends on the exact requirements. For example I assumed each card was represented by two letters, such as TS for ten of spades. I see from an earlier post that you will have three characters, 10S in this case. Either way is ok, but it needs to be clearly specified.
To be frank, I'm not really a card player so some of the rules of the game are not familiar to me. What you need to do is to describe in words what it is you need to do, and then set about translating that into code. My sticking point here is not having a clear description of the exact requirements.
We have to get the lines from a txt file, seperate the cards (two per each player) and give the total number of blackjacks represented by stars at the end of the program for each player. The teacher strongly suggest using arrays, but says there are other ways to complete the program.
If player1 has 6 blackjacks throughout the whole game, then the table will show:
Player 1: 6 wins *****
As for the rules of the game:
A blackjack is defined as an Ace of any suit and a 10 valued card (Jack, Queen ,King or 10) of any suit
Ok, but how many players will there be? Is it a fixed number or can it vary?
When allocating cards to each player, initially I was thinking of reading two cards, giving them to the first player, and so on. But maybe it is supposed to happen like this, give each player one card, then give each player another card, and so on.
I had also initially assumed that there were a fixed number of cards on each line of the file, but maybe there is just one card per line. And maybe it doesn't even matter, we should just read one card at a time?
As you can see, I have plenty of questions. For me, the most difficult part of any programming task, and this is no exception, it to get the exact requirements.
^^ For example, do you have to use substring? You could simply iterate through the string:
1 2 3 4
int spaceCount = 0;
for (unsignedint i = 0; i < str.length(); i++)
if (str.at(i) == ' ') spaceCount++;
if (spaceCount != 5) std::cout << "Hey, I was excepting six cards!\n";
To get substrings for various lengths is a little tricky. You need to keep track of where you were before you looked for the next space:
1 2 3 4 5 6 7 8 9 10
// #include <iomanip>
size_t start = 0;
size_t end = str.find(' ', start);
while (end != std::string::npos)
std::cout << "Line from:" << setw(3) << start << " to" << setw(3) << end
<< " is: " << str.substr(start, end - start) << '\n';
start = end + 1; // you may have to play around with ones to get rid of empty spaces
end = str.find(' ', start);
Ok I shall further explain. I will use find to search for an 'A' within the line, if an 'A' is found, then the card before it also must be accounted for. In the example above: JH 8D QH AC 8H 9S
An 'A' is found, then you must find a K,Q,J or 10 as the second card, if one of the three options is there, then the player gets a blackjack and a point is given to the player. After going through many lines of code, the output should read the number of wins each player received throughout the txt file.
:Player 1: 6 wins ******
You are creating strings by using the substring function, but not doing it correctly.
The first parameter is the position you want the substring to start from. The second parameter is how long you want the substring to be (ie how many characters to copy). What you did was fine for the first card, but not for the others. (EG if endcard3 was the 3rd space, it would be in position 9 or more, so your card3 string would be 9 or more characters long.)
You're also not really using the find_first_of function correctly...
The first parameter is the string or character to search for. The second parameter is the position to start searching from. If you start searching for spaces from where you found your last space, it will return the position of the same space.