Hello. I haven't done any c/c++ in quite some time and I'm rusty so please bear with me. It's been equally fun and frustrating trying to relearn it.
I need some assistance with a snippet of code. Hopefully I can explain this clearly. What I would like it to do is:
1.) Read in a .txt file. Each entry is on it's own line and is a name (string), followed by a space (' '), followed by a number (int).
2.) Search by name if there is an entry by that name in the file;
a.)If name is in file, add +1 to associated number
b.)If name is not in file, add the name with a number (1)
c.)If name is in .txt but not being searched for, do not modify.
For example my .txt file is:
If I give it three names to check, ex. craig, jim, bill, it would not find craig and add craig to the list with 1, find jim and add 1 to jim, find bill and add 1 to bill, tim was not searched for so he stays at 15, making the text file at the end of execution:
Similarly, running it again querying for craig, jim, and bill would produce:
I have the rest of the code working but the problem I have is I can't seem to figure out the go about cycling through each name given and either updates the entry or adds it. Here's the code I have so far:
(Loop to iterate through each name I want to search for. This I have working perfectly.)
guy = xxxx; //name of current person being searched for, iterated through by my loop
while (file >> user >> pts) // cycling through entrys. this seems to work somewhat.
if (guy == user) //entry with same name as searched for found, updating entry by adding 1 to their number
//not sure how to code this
if (!(guy == user)) //entry with name searched for not found, add new entry with name guy and a number value of 1.
ofstream file("c:\\text.txt", ios::app);
file << guy << ' ' << "1" << endl;
So basically everything inside of the "while (file >> user >> pts)" loop, including the loop, I am not sure how to do. Perhaps I'm going about it completely wrong? Any help or insight would be much appreciated!