Frequency

Pages: 12
Not sure if this is for my problem, completely lost now.
I assure you that it does apply to your issue. We are done with the variable "Temp" and the while loop. Right now we need a seperate for loop from the one that we already have to iterate through the vector. Honestly, all of this stuff that I'm saying can be written in two lines one line of code, I would use four but that's purely a style preference.
Last edited on
1
2
3
4
5
6
7
8
for(int i = 0; i < OcVec.size(); i++)
		{
			if(Temp == OcVec[i].Word)
			{
				cout << Word << endl;
			}
		}
	}

This is what I came up with with trying to understand what you said, how far am I off?
- Drop the if statement and you have it. We've already retreved all of the we need, WE ARE DONE with the variable "Temp".

- We are addressing the elements of "OcVec" so we need to tell the compiler that.
1
2
3
4
for(int i = 0; i < OcVec.size(); i++)
{
    cout << OcVec[i].Word << '\t' << OcVec[i].Freq << '\n';
}

If the close bracket on Line 8 isn't the end of your Main() function then you do not need it.
Alright, thats where I was confused was the Temp thing. However when I run it is gives me a blank console. Do you want the file? I understand the last for statement, not sure why I wouldn't run right now though.
Your current code would be more useful to resolve this issue.
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
#include <iostream>
#include <fstream>					
#include <string>
#include <vector> 
using namespace std;

class Occurances 
{
	public:
	string Word;
	int Freq;
	
	Occurances() {}
	Occurances(string Arg) : Word(Arg), Freq(1) {}	
};



int main()
{

	ifstream Line("KeyWordsOnLineHelp.txt");
	vector<Occurances> OcVec; 
	string Temp;
	if (Line.fail())				
	{
		if(Line.fail())
		{

			cout << "Error: main(): Failed to open the file: ";
			cout << "KeyWordsOnLineHelp.txt" << endl;
		}
	}
	while(Line >> Temp) 
	{
		Line >> Temp;
		for(int i = 0; i < OcVec.size(); i++)
		{
			if(Temp == OcVec[i].Word)
			{
				OcVec[i].Freq++;
			}
			else
			{
				Occurances Another(Temp);
				OcVec.push_back(Another);
			}
			Temp = " ";
		}
		for(int i = 0; i < OcVec.size(); i++)
		{
			cout << OcVec[i].Word << '\t' << OcVec[i].Freq << '\n';
		}
	}


	cout << endl;
	system("pause");
	return 0;
}
Again, the second for loop goes OUTSIDE the while loop.
Got it, however it still gives me a blank console when I run it. There are at least 5-10 words that are repeated and should be shown.
Yeah, there are some bugs here. I'll look into it.
There were a few bugs, a missing break statement and some flow control problems. I believe this will be a working example:
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
#include <iostream>
#include <fstream>					
#include <string>
#include <vector> 
using namespace std;

class Occurances 
{
	public:
	string Word;
	int Freq;
	
	Occurances() {}
	Occurances(string Arg) : Word(Arg), Freq(1) {}	
};



int main()
{

	ifstream Line("KeyWordsOnLineHelp.txt");
	vector<Occurances> OcVec; 
	Occurances T("Control");
	   OcVec.push_back(T);
	   
	string Temp;
	if (Line.fail())				
	{
		if(Line.fail())
		{

			cout << "Error: main(): Failed to open the file: ";
			cout << "KeyWordsOnLineHelp.txt" << endl;
		}
	}
	while(Line >> Temp)
    {
		for(int i = 0; i < OcVec.size(); i++)
		{
			if(Temp == OcVec[i].Word)
			{
				OcVec[i].Freq++;
                break;				
			}
        }

        Occurances Another(Temp);
        OcVec.push_back(Another);
    }
		
	for(int i = 0; i < OcVec.size(); i++)
	{
		cout << OcVec[i].Word << '\t' << OcVec[i].Freq << '\n';
	}
	


	cout << endl;
	system("pause");
	return 0;
}
Topic archived. No new replies allowed.
Pages: 12