Searching a string in a file and converting string to int

I'm taking my first c++ class currently and have some issues with a searching files. I currently have a text file with SEC football scores from 2011 set up like this:

-----Week 1-----
score team score team


for each team for fourteen weeks.

My assignment is to open the file and allow a user to search the file for a team name and return each teams games with the same "score team score team" format. Here is my current code.

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
   ifstream inFile;
   inFile.open("SEC.txt");

   string team;
   cout << "Enter the team you'd like results for.\n";
   cin >> team;

   if (!inFile)
   {
      cout << "File open failure!\n";
   }

   string s1, s2, s3, s4;
   int score1, score2;

   while (inFile >> s1 >> s2 >> s3 >> s4)
   {
      if (s2 == team)

      else if (s4 == team)

   }

   cout << s1 << s2 << s3 << s4 << endl;

   inFile.close();
   return 0;
}


My main question right now is with the if/else statement. With s1 and s3 being integers in the file, they need to be converted to int variables so they can be printed on the screen. I've read about stringstream and "#include <sstream> on various searches but it's not something that has been covered in class which typically means we can't use it. So I guess I need some ideas on what else I can use and maybe some reading I might need to look at. After staring at this for two days and getting nowhere any help is appreciated. I have no idea where to begin with this.
there is a string function called stoi you can use
http://www.cplusplus.com/reference/string/stoi/?kw=stoi
closed account (S6k9GNh0)
There's also boosts lexical_cast which might be more flexible. Stringstreams can also do what you want with ease albeit slow.
Last edited on
I actually used atoi instead and set score1 = atoi("s1") and score2 = atoi("s3") just before the while loop. It functions... albeit very poorly. It now prints the games for each team you type in but there's no spaces between the first score and the last team.
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
52
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;

int main()
{
   ifstream inFile;
   inFile.open("SEC.txt");

   string team;
   cout << "Enter the team you'd like results for.\n";
   cin >> team;

   string s1, s2, s3, s4;
   int score1, score2;
   score1 = atoi("s1");
   score2 = atoi("s3");

   while (inFile >> s1 >> s2 >> s3 >> s4)
   {
      if (s2 == team)
         cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl;
      else if (s4 == team)
         cout << s1 << " " << s2 << " " << s3 << " " << s4 << endl;
   }

   int wins = 0;
   int losses = 0;

   while (s2 == team)
   {
      if (score1 > score2)
         wins++;
      else if (score2 > score1)
         losses++;
   }

   while (s4 == team)
   {
      if (score2 > score1)
         wins++;
      else if (score1 > score2)
         losses++;
   }

   cout << team << " has " << wins << " wins" << " and " << losses << " losses" << endl;
   inFile.close();
   return 0;
}


Here's my updated code and I've got it print the results for the team searched by the user. The next part I have to do is count the number of wins and losses. Currently my counters aren't working and all it does is print " team has 0 wins and 0 losses"

My guess is that this has to do with my atoi conversion but I really have no idea. Any thoughts on how I should go about trying to fix this?
closed account (S6k9GNh0)
do wat.

score1 = atoi("s1");

What do you think this does?
And when do you think it does it?
1
2
    string s1;
    int score1 = atoi("s1");

The above code sets score1 to 0 since "s1" isn't a valid integer, (and it has no relationship whatsoever with the variable s1 on the previous line).

The correct code would look a bit more like this:
1
2
    string s1;
    int score1 = atoi(s1.c_str());

This time, the actual contents of the variable s1 will be used correctly. But - the result will still be zero. Why? because s1 is an empty string. The conversion using atoi() needs to be done at the correct sequence in the program, after string s1 has been assigned a proper value.

Last edited on
Topic archived. No new replies allowed.