Converting lines

Pages: 12
Your conditions are also wrong, you need parenthesis:
1
2
if (a && b || c) // always true if c
if (a && ( b || c)) // need at least b or c 
Last edited on
Then how would I go about fixing the substring and find_first_of functions to fit my requirements ??

@LowestOne, thank you for your correction.
Last edited on
You need to fix your parameters. EG

1
2
int endcard3 = data.find_first_of(" ", endcard2 + 1);
card3 = data.substr(endcard2 + 1, endcard3 - endcard2 - 1);

versus

1
2
int endcard3 = data.find_first_of(" ",endcard2);
card3 = data.substr(endcard2, endcard3);
@fg109

I tried your parameters and they do not seem to work. Maybe it is just me that is not inputting it correctly ??
I talked to the teacher and he gave me this code for the first two cards for the first player:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int test = data.find('A');
          if(test >= 0)
          {
              cout << "Line: "<< lines << " ---> " <<data<<endl;
              for(int p = 0;p<10;p++)
              {
                   int endcard1 = data.find_first_of(" ",startPos);
                    string card1 = data.substr(startPos,endcard1);
                    startPos = endcard1++;
                    cout << "End position:"<< endcard1<<" substr "<< card1 <<endl;
                    
                   int endcard2 = data.find_first_of(" ",startPos);
                   string card2 = data.substr(startPos,endcard1);
                   startPos = endcard2++;
                               if (card1 == "A" && (card2 == "J" || card2 == "K" || card2 == "Q" || card2 == "10"))
                                  {
                                         player1++;
                                  }
                                  
                       startPos = startPos + endcard2 + 1;          
                }              


But the program says there is a unexpected Runtime error and I do not know how to fix it.
I don't know why the parameters I gave you didn't work. You did change the parameters for all the function calls, not just for card3, right? As for the code that your teacher gave you, I'm sure you must have copied it incorrectly.

The problem that probably caused your run time error is line 5. You are going through the loop ten times. Each time through the loop, you read in two cards. The line just isn't that long, and reading what isn't there is causing an error.

You are still using the substr function incorrectly. Check the reference page, the second parameter is supposed to be the length of the sub-string.

http://www.cplusplus.com/reference/string/string/substr/

Think about what values endcard1 and endcard2 has each time you go through the loop.

1. endcard1 = 2, endcard2 = 5
2. endcard1 = 8, endcard2 = 11
etc.

So your sub-strings are going to be much longer than what you probably want.

Your conditions are still incorrect (probably). I think you're trying to read in whole cards, so card1 and card2 are going to be at least two characters long. Your conditions in the if statement are never going to be true.

Is there some reason you must use the find and substr functions? The stringstream method I showed earlier works just fine.

EDIT: Noticed some more possible errors in the code.

On line 9, startPos = endcard1++; means that you set startPos equal to endcard1, then increment endcard1 by 1. You probably meant to do startPos = ++endcard1; which would mean incrementing endcard1 by 1 first, then setting startPos equal to endcard1. I am not sure why you need line 14.

On line 13, you probably meant string card2 = data.substr(startPos,endcard2); instead of string card2 = data.substr(startPos,endcard1);. Although that would still be using the substr function incorrectly.
Last edited on
The teacher thought it would be the best way to complete the assignment. Is string stream easier to use ?? The teacher is just throwing out suggestions but is not teaching us what they mean; which is why I am having such a hard time completing this program.
I wouldn't say that it's easier to use, since really it just depends on how familiar you are with it compared to the string functions you're using here. But it definitely makes the code a lot shorter.

This would be the same thing I posted before, except using the find and substr functions instead of string streams:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    while (getline(fin, line)
    {
        if (line.find('A') != std::string::npos)
        {
            const std::string tens = "1JQK";
            size_t start = 0, end = 0;
            
            for (int player = 0; player < 3; player++)
            {
                std::string first, second;
                start = line.find_first_not_of(' ', end);
                end = line.find_first_of(' ', start)
                first = line.substr(start, end - start);
                start = line.find_first_not_of(' ', end);
                end = line.find_first_of(' ', start);
                if (end == std::string::npos)
                    end = line.length();
                second = line.substr(start, end - start);
                if ((first[0] == 'A' && tens.find(second[0]) != std::string::npos) ||
                   (second[0] == 'A' && tens.find(first[0]) != std::string::npos))
                   playerwins[player]++;
            }
        }
    }
Last edited on
@fg109
It compiles but says there is a runtime error. How would I fix it ??

So far, this is only for one player

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
int main ()
{
    ifstream inputFile;
    string filename = "blackjack2.txt";
    string data;
    int lines = 0;
    int player1wins, player2wins, player3wins; //  (Three players)
      
    inputFile.open (filename.c_str());
    if (inputFile.is_open())
    {                          
          while (getline(inputFile, data));
          lines++;
                  if (data.find('A') != std::string::npos);
                  {
                     const std::string tens = "10JQK";
                     size_t start = 0, end = 0;
                            for(int p = 0; p < 5; p++)                    
                            {           
                                        std::string first, second;
                                        start = data.find_first_not_of(' ', end);
                                        end = data.find_first_of(' ', start);
                                        first = data.substr(start, end - start);
                                        start = data.find_first_not_of(' ', end);
                                        end = data.find_first_of(' ', start);
                                            if (end == std::string::npos)
                                            {
                                            end = data.length();
                                            second = data.substr(start, end - start);
                                                   if ((first[0] == 'A' && tens.find(second[0]) != std::string::npos) ||
                                                      (second[0] == 'A' && tens.find(first[0]) != std::string::npos))
                                                      {
                                                       player1wins++;
                                                      } 
                                             } 
                            }
                   }
     }
     inputFile.close();
     system ("PAUSE");
     return EXIT_SUCCESS;  
}     
Last edited on
fg109 wrote:
The problem that probably caused your run time error is line 5 18. You are going through the loop ten five times. Each time through the loop, you read in two cards. The line just isn't that long, and reading what isn't there is causing an error.
Ok so I tried that, and it is still not working. For some reason my program just does not seem to work.
Topic archived. No new replies allowed.
Pages: 12