Jul 22, 2013 at 6:10pm UTC
Hi i'm looking for a program to read a file to look for strings and add a count.
kill.log in the same folder as program
player1 was killed by player2
player2 was killed by player1
4 strings
player1 was for "d1" add count
player2 was for "d2" add count
by player1 for "k1" add count
by player2 for "k2" add count
cout << Player 1 kills: << k1 << deaths << d1;
cout << Player 2 kills: << k2 << deaths << d2;
add up each time. Thanks guys.
Jul 22, 2013 at 11:06pm UTC
i think it would be better if you try to write it yourself first then ask for help.
Jul 23, 2013 at 4:24pm UTC
Ok so I worked on most of it but there's one problem. if it picks up d1, it won't add k1, seems like it ends the line.
input.txt would have one line with "The Governor was killed by The Governor"
picks up d1 for The Governor was but not k1 for by The Governor
Here is the code, I don't know how to put it in that blue box.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
// basic file operations
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int k1=0;
int k2=0;
int k3=0;
int k4=0;
int k5=0;
int d1=0;
int d2=0;
int d3=0;
int d4=0;
int d5=0;
int main ()
{
// Change these names to make the code work
string s1 ("The Governor" );
string s2 ("Eric" );
string s3 ("Gumaro" );
string s4 ("Jeff" );
string s5 ("Chris" );
string str1 ("by " + s1);
string str2 ("by " + s2);
string str3 ("by " + s3);
string str4 ("by " + s4);
string str5 ("by " + s5);
string str6 (s1 + " was" );
string str7 (s2 + " was" );
string str8 (s3 + " was" );
string str9 (s4 + " was" );
string str10 (s5 + " was" );
string a;
ifstream file("input.txt" );
if (file.fail())
{
cerr<<"\n The file does not exist." ;
return -1;
}
while (!file.eof() )
{
getline(file,a);
if (!a.find(str1))
k1++;
if (!a.find(str2))
k2++;
if (!a.find(str3))
k3++;
if (!a.find(str4))
k4++;
if (!a.find(str5))
k5++;
if (!a.find(str6))
d1++;
if (!a.find(str7))
d2++;
if (!a.find(str8))
d3++;
if (!a.find(str9))
d4++;
if (!a.find(str10))
d5++;
}
cout << s1 << " killed " << k1 << " noobs and died " << d1 << " times!!" <<endl;
cout << s2 << " killed " << k2 << " noobs and died " << d2 << " times!!" <<endl;
cout << s3 << " killed " << k3 << " noobs and died " << d3 << " times!!" <<endl;
cout << s4 << " killed " << k4 << " noobs and died " << d4 << " times!!" <<endl;
cout << s5 << " killed " << k5 << " noobs and died " << d5 << " times!!" <<endl << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
cout << str6 << endl;
cout << str7 << endl;
cout << str8 << endl;
cout << str9 << endl;
cout << str10 << endl;
}
Last edited on Jul 23, 2013 at 4:57pm UTC
Jul 23, 2013 at 4:41pm UTC
Please use code tags when posting code, to make it more readable.
Jul 23, 2013 at 4:47pm UTC
Sorry I apologized beforehand but found out how to add the code box.
Jul 23, 2013 at 6:52pm UTC
my string::find returns 0 or 1. When it's 1 it increases my variable but the problem is that it doesn't read the second half of the sentence. My output is The Governor killed 0 noobs and died 1 times!!
The line in the file is The Governor was killed by The Governor.
It should output The Governor killed 1 noobs and died 1 times!!
Please guide me a little more.
Last edited on Jul 23, 2013 at 6:52pm UTC