Count Controlled Loops and File usage.

I've been working on this for a couple of hours now, rereading the textbook and tutorials to try to help, and I can't seem to graps this section (though I am proud that I got other looping aspects down). I think the problem with this one is I also have to use a file that already contains data.

The program I need to design reads data from an input file and counts all of the 3, 4, 5, and 6 letter words within the file. It then takes the count information and outputs it to another file. I know the program needs to loop four times, the first searching and counting all 3 letter words, etc. The problem is, the more I write the code, the more frustrated I become. Here is the code I have so far, complete with errors.

#include <fstream>
#include <iomanip>
using namespace std;

int main()
{
ifstream inFile;
ofstream outFile;

int count ;
int charCount ;
int count3 ;
int count4 ;
int count5 ;
int count6 ;
char inChar ;

inFile.open ("QUOTES.TXT") ; //The file to be read.
outFile.open ("NUMWORDS.TXT") ; //The file that will be created with the count information.


while (inFile)
count = 0 ;
inFile.get (inChar);
while (count != 8)
{
inFile.get (inChar) ;
count ++ ;
while (inChar >= 'A' && inChar <= 'Z' || inChar >= 'a' && inChar <= 'z')
if (charCount == 3);
count3++;
else
if(charCount == 4);
count4++;
else
if(charCount == 5);
count5++;
else
if(charCount == 6);
count6++
}


The problem is, how do I write the code to read the characters in the file and count the 3, 4, 5, and 6 letter words? I've been at it for a few hours and even slept on it to try to clear my head. I can't wrap my brain around this one. Thanks for any and all help.
Try somehting like this.

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
while (inData)// end of file control loop
		{
			count1 = 0;
			inData.get (inChar); // end of word loop
			while (inChar >= 'A' && inChar <= 'Z' || inChar >= 'a' && inChar <= 'z')
			{
				inData.get (inChar);
				count1 ++;
			}// end while
			if (count1 == 3)
				count3 = count3 ++;
			else
				if (count1 == 4)
					count4 = count4 ++;
				else 
					if (count1 == 5)						
						count5 = count5 ++;
					else
						if (count1 == 5)
							count5 = count5 ++;
						else 
							if (count1 == 6)
								count6 = count6 ++;
		}// end while
		outData << "3 letter words: ";
		outData << count3 << endl;
		outData << "4 letter words: ";
		outData << count4 << endl;
		outData << "5 letter words: ";
		outData << count5 << endl;
		outData << "6 letter words: ";
		outData << count6 << endl;
		outData << "\n";// blank line
		inData.close ();
		inData.clear ();
Last edited on
That creates the output file, and it also compiles correctly, but the program doesn't count the words in the input file. If I identify counts 3, 4, 5, and 6 to be equal to 1, then the program outputs them as 1 without counting anything. If I don't identify them, then it errors when I run the program and outputs the counts as a large negative number.
Sorry, I used inData for my file. Did you change all my inData to inFile?
Yeah. I made all of the necessary changes to be able to read the data file, but the counts are still not working out correctly.
This was typed on my phone. If it doesn't work I'll check it when I get home.
closed account (1yvXoG1T)
I have a similar approach. This one has been tested to properly count the words.
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
#include <iostream>

using namespace std;

int main()
{
    ifstream inFile;
    ofstream outFile;

    int count;
    int charCount;
    int count3 = 0;
    int count4 = 0;
    int count5 = 0;
    int count6 = 0;
    char inChar;

    inFile.open ("QUOTES.TXT") ; //The file to be read.
    outFile.open ("NUMWORDS.TXT") ; //The file that will be created with the count information.

    charCount = 0;
    while (!inFile.eof())
    {
        inFile.read(reinterpret_cast<char*>(&inChar), sizeof(char));
        if ((inChar >= 'a' && inChar <= 'z')||(inChar >= 'A' && inChar <= 'Z'))
            charCount++;
        else {
            if (charCount == 3)
                count3++;
            else if(charCount == 4)
                count4++;
            else if(charCount == 5)
                count5++;
            else if(charCount == 6)
                count6++;

            charCount = 0;
        }
    }

    charCount--;

    if (charCount == 3)
        count3++;
    else if(charCount == 4)
        count4++;
    else if(charCount == 5)
        count5++;
    else if(charCount == 6)
        count6++;

    outFile << "3 Letter words: " << count3 << endl;
    outFile << "4 Letter words: " << count4 << endl;
    outFile << "5 Letter words: " << count5 << endl;
    outFile << "6 Letter words: " << count6 << endl;

    outFile.clear();
    outFile.close();

    return 0;
}
I tried it in my program, and it's yielding a blank document. I'm not sure what I'm doing wrong. It compiles correctly, but the command screen doesn't say anything, and the output file is empty when I open it. It doesn't even give me false numbers.
closed account (1yvXoG1T)
The console is going to be blank since there's no cout statements. Just to make sure, do you have a QUOTES.TXT that contains some text?
Sorry it doesn't work for. Works fine for me.

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
#include <iostream>// for cout, endl
#include <iomanip>// for setw() and setprecision()
#include <fstream>// for ifstream and iostream
#include <string>

using namespace std;// "just do it"

int main ()
{
	ifstream inData;
	ofstream outData;

	int count1;
	int count3 = 0;
	int count4 = 0;
	int count5 = 0;
	int count6 = 0;
	char inChar;

	/// open file
                inData.open ("QUOTES.txt");
	outData.open ("NUMWORDS.txt");

	if (inData) // Did input file open?
	{
		while (inData)// end of file control loop
		{
			count1 = 0;
			inData.get (inChar); // end of word loop
			while (inChar >= 'A' && inChar <= 'Z' || inChar >= 'a' && inChar <= 'z')
			{
				inData.get (inChar);
				count1 ++;
			}// end while
			if (count1 == 3)
				count3 = count3 ++;
			else
				if (count1 == 4)
					count4 = count4 ++;
				else 
					if (count1 == 5)						
						count5 = count5 ++;
					else
						if (count1 == 5)
							count5 = count5 ++;
						else 
							if (count1 == 6)
								count6 = count6 ++;
		}// end while
		outData << "3 letter words: ";
		outData << count3 << endl;
		outData << "4 letter words: ";
		outData << count4 << endl;
		outData << "5 letter words: ";
		outData << count5 << endl;
		outData << "6 letter words: ";
		outData << count6 << endl;
		outData << "\n";// blank line
		inData.close ();
		inData.clear ();
	}// end if
	else
	{
		cout << "Can't open file." << endl;
	}// end else
	cout << "file NUMWORDS.TXT created" << endl;
}// end main 
I have a feeling that my computer is jacked up. This isn't the first time I've experienced problems. I will work on this at the school's computers and see what happens. Thanks again for all of the help everyone.
And yeah, I have a QUOTES.txt. The last program posted will compile and display the else information, but I can't get it to put anything in the numwords file. Why do I always have trouble with reading from data files? :P

The programs dealing with For statements that I had to do last week were easy as pie. *sigh*
I have had problems with VS2010 also. But normaly sutting it down and reopening the project fixes the problem.

The program is creating the output file but leaving it empty or you made the output file?
Last edited on
It creates the output file but leaves it empty.
Can you post your code you are using now
#include <iostream>// for cout, endl
#include <iomanip>// for setw() and setprecision()
#include <fstream>// for ifstream and iostream
#include <string>

using namespace std;

int main ()
{
ifstream inData;
ofstream outData;

int count1;
int count3 = 0;
int count4 = 0;
int count5 = 0;
int count6 = 0;
char inChar;

inData.open ("QUOTES.txt");
outData.open ("NUMWORDS.txt");

if (inData)
{
while (inData)
{
count1 = 0;
inData.get (inChar);
while (inChar >= 'A' && inChar <= 'Z' || inChar >= 'a' && inChar <= 'z')
{
inData.get (inChar);
count1 ++;
}
if (count1 == 3)
count3 = count3 ++;
else
if (count1 == 4)
count4 = count4 ++;
else
if (count1 == 5)
count5 = count5 ++;
else
if (count1 == 5)
count5 = count5 ++;
else
if (count1 == 6)
count6 = count6 ++;
}
outData << "3 letter words: ";
outData << count3 << endl;
outData << "4 letter words: ";
outData << count4 << endl;
outData << "5 letter words: ";
outData << count5 << endl;
outData << "6 letter words: ";
outData << count6 << endl;
outData << "\n";
inData.close ();
inData.clear ();
}
else
{
cout << "Can't open file." << endl;
}
cout << "file NUMWORDS.TXT created" << endl;
}
Sorry, I didn't know you were using my code as posted. Where is your QUOTES.txt file located? I removed the path before posting the code. The code will run as posted but if your QUOTES.txt file if not in the same Dir as the .cpp file it will just give you a blank NUMWORDS.TXT file.
My quotes file is in the same directory. I'm going to try it at school tomorrow. Failing that, I will go to the instructor, though I don't learn anything from him.
Topic archived. No new replies allowed.