Function counting

I wrote this program but when I run it and enter in words to search for........they always come back 1 value less than there are. What am I missing in the code?..........it has to be in the function counter I think.

e.g. when you search for the word "past" there should be 5, but i get 4.

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
#include<iostream>
#include<vector>
#include<fstream>
#include<cctype>
#include<sstream>
#include<cmath>
using namespace std;


string fixer_bitches(string fix_my_ass){
        string temp;
        for(int i = 0, j = 0; i < fix_my_ass.length(); i ++){
                for(int t = 0; t < fix_my_ass.length(); t ++){

                        fix_my_ass[i] = tolower(fix_my_ass[i]);// changes everything to lowercase to make it easier to view.
                }//for
                if (fix_my_ass[i] >= 'a' && fix_my_ass[i] <= 'z')//checks for all letters in the string, if it isn't a letter it ignors it.
                {
                        temp.append(1,fix_my_ass[i]);//stores all characters in original string without any non-letter chars in new temp string
                }//if
        }//for
}//fixer_bitches



int main()
{
        cout << "Please enter a word to search my file to see if it is contained within it." <<endl;
        string user;
        string fix_my_ass;
        getline(cin, user);
        fix_my_ass = fixer_bitches(user);
        ifstream inf;
        inf.open("words.txt");
        string s;
 getline(inf, s);
        int counter = 0;
        while(!inf.eof()){
                istringstream instr(s);
                string word;
                instr >> word;
                while(!instr.eof()){
                        fix_my_ass = fixer_bitches(word);
                        if(user == word)
                                counter++;
                                instr >> word;
                }//while
                getline(inf, s);
        }//while

        cout << user << " occurs " << counter << " times in the file" <<endl;
        inf.close();

        return 0;

}
closed account (o3hC5Di1)
Hi there,

Although I'm not able to immediately pinpoint the problem, I would like to offer a few kind words of advice:

- Use descriptive names for your functions and variables. Not only are some of the names in your program inappropriate, they also don't tell anyone reading your code what the function actually does, which makes your program kind of hard to figure out. Variables names like "user, s" don't really mean much in this context either.

- Don't add comments to closing braces, the whole point of proper indentation style (whatever that means to you personally) is to allow for clear opening and closing of code blocks, without having to comment it. The extra comments just distract and clutter the code.

- Your function is supposed to be returning a string, but there is no return statement in it anywhere.

I suggest you make some alterations based on these suggestions, if after that the problems still persists, please feel free to come back to use with your revised code.

All the best,
NwN
Ya I know the names of the variables were a bit rude, but I was looking at this damn code for too long and had to have a bit of fun..........I figured out that I was passing the string to the function wrong.........so when I returned the temp string(which thank you for pointing out no return) it never gave me any string that had punctuation, because it was passing it by value and not by reference. Once it saw punctuation it stopped, and the return was only the exact values of the string. It's all working. Thanks for your help.
Last edited on
Topic archived. No new replies allowed.