Trying to count occurrences of number 3...it's a little off.

Pages: 12
Hey guys, sorry to keep bugging, but my class is online and my professor doesn't provide feedback regularly, and I'm banging my head against a wall with this. The objective is to create a program that opens a file, counts the occurrence of "3" in the file, then reads out the count. I thought I had it working well, but I had no other way of telling whether my count was right so I pointed it to a file that has exactly ONE three in it... but it's coming back and saying I have a little over 223,000 threes. So, you know, the count is just a little off. I tried putting in a cout line to show me the current character that's being read, but it just sends it into an endless loop of nothingness, so I know something is off somewhere. Thanks!!!

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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char character; // Holds the character currently being read
    int count = 0; // Holds the current count of threes found
    ifstream file; // File stream object

    // Open the file
    file.open ("threesData.bin", ios::in);
    
    //Continue
    if (file)
    {
        while (!file.eof())
            {
                // Get a character from the file
                file.get(character);

                if (character == 3)
                {
                    // If it's a three, increment the count
                    count++;
                }
                    
                // Otherwise, just get the next character
                else 
                {
                    continue;
                }
            }
        // Close the file
        file.close();
        
        // Notify if file can't be opened
    }
    else
    {                    
        cout << "File could not be opened." << endl;
    }
    
    cout << "There are " << count << " threes in the file." << endl;
            
    return 0;
}
When you say an endless loop of nothingness, do you mean it's printing out a bunch of garbage letters, or like is it literally printing out nothing?
Last edited on
I literally just get a blank cursor in the console (our assignments have a built in editor/compiler in them that I've been using because most of the IDEs are way over my head). If I take the cout statement out though, it immediately spits back that I have 223k threes in the file.
closed account (48T7M4Gy)
if ( character == '3') might help
One thing I immediately noticed is

1
2
3
4
5
if (character == 3)
{
     // If it's a three, increment the count
     count++;
}


You need to do

 
if (character == '3')


note that you're looking for the char version of '3' but you're comparing it to the integer 3. These are not the same thing. You should google an ascii table to see which integer equals '3'.


I don't think this will completely fix it, but this is definitely a bug.
Last edited on
closed account (48T7M4Gy)
BTW you don't need lines 29 to 33, and the while statement would be better as while(file>>character)
Also

1
2
3
else {
   continue;
}


isn't a bug, but I think it's redundant. Wouldn't you go back to the beginning of the loop regardless?
closed account (48T7M4Gy)
Great minds think alike.
you're right on the Department of Redundancy Department. For some reason (probably because I'm a total n00b) I thought if I had an 'if' I had to have an 'else'. I realize the folly of those ways now lol. We're making progress on the threes count! We're down from 223k to 18,762! The correct number is still, however, 1...so I've still got some work to do.

this is what it looks like now (so you can refer to updated line numbers):

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

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    char character; // Holds the character currently being read
    int count = 0; // Holds the current count of threes found
    ifstream file; // File stream object

    // Open the file
    file.open ("threesData.bin", ios::in);
    
    //Continue
    if (file)
    {
        while (file>>character)
            {
                // Get a character from the file
                file.get(character);

                if (character == '3')
                {
                    // If it's a three, increment the count
                    count++;
                }
            }
        // Close the file
        file.close();
        
        // Notify if file can't be opened
    }
    else
    {                    
        cout << "File could not be opened." << endl;
    }
    
    cout << "There are " << count << " threes in the file." << endl;
            
    return 0;
}


Also, the professor said that the file that is going to need to be read is a 32-bit binary file....how does that change what I need to be doing?
Last edited on
closed account (48T7M4Gy)
you need to delete line 22
Haha apparently so :)

I've just forgotten but why would file>>character be better than ".eof()" and "get()".

I feel like I remember being told that but I can't remember why.
kemort, I just tried that....back up to 37,540 threes in the file. :-\

on a side note, if i don't do file.get(character), then how do I actually GET a character?
Last edited on
for reference, also, the only thing I currently have in my threesData.bin file is "1234567890asd;lkmwe;oriknb;lka". I just bashed the keyboard for a while and made sure there was one three in there so it had something to count but so I also knew the count would come back as 1 if it was correct.
closed account (48T7M4Gy)
I'll have a look
well, fuck. Can I say 'fuck' here? If not, I'm sorry...for both of them.

Someone in my class forum just pointed out that the professor wants the occurrences of the VALUE 3 in a binary file of 32-bit integers. I think my simple character search is WAY off the mark. This feels way above my head. Any ideas? The actual assignment says:

The Count3s program opens a binary file containing 32 bit integers (ints). Your program will count the number of occurrences of the value 3 in this file of numbers.
closed account (48T7M4Gy)
Because it's a binary file the process is significantly different. This tutorial has a short section and sample program under the heading http://www.cplusplus.com/doc/tutorial/files/
I went through that one the other day, but it seems like it only gets me through opening it and reading it into memory. I'm completely lost as to where to go from there in terms of parsing it for a certain value and working with that data. I'm emailing the professor. I'm in desperate need of clarity.
closed account (48T7M4Gy)
FWIW I adapted a program from Stroustrup's book which might help you. (I did it for my benefit too because I needed to refresh my binary file stuff)
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
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    // *** OUTPUT TO A BINARY FILE ***
    cout << "Please enter output file name: ";
    string oname = "xxx.bin";
    //cin >> oname;
    
    ofstream ofs{oname,ios_base::binary};
    if (!ofs) std::cout << "Can't open output file " << oname << '\n';
    int test_no = 0;
    for(int  i = 1; i < 10; ++i)
    {
        test_no = 77 + i;
        ofs.write(reinterpret_cast<char*>(&test_no), sizeof test_no);
    }
    ofs.close();
    
    // *** INPUT FROM A FILE ***
    cout << "Please enter input file name: ";
    string iname = "xxx.bin";
    //cin >> iname;
    
    ifstream ifs {iname,ios_base::binary};
    if (!ifs) std::cout << "Error - can't open input file " << iname << '\n';
    
    // READ THE DATA FROM THE BINARY FILE AND CHECK CONTENTS
    std::cout << '\n';
    
    int zz = 0;
    while(ifs.read(reinterpret_cast<char*>(&zz), sizeof zz))
    {
        std::cout << "Number is: " << zz;
        if(zz == 83)
            std::cout << " *** BINGO! ***";
        
        std::cout << '\n';
    }
    
    ofs.close();
    
    return 0;
}


FILE NAME IS HARD CODED
Please enter output file name: Please enter input file name:
Number is: 78
Number is: 79
Number is: 80
Number is: 81
Number is: 82
Number is: 83 *** BINGO! ***
Number is: 84
Number is: 85
Number is: 86
Program ended with exit code: 0


THANK YOU!!! I think I got it working with this code, but unfortunately I'm still not sure. I now have no choice but to test against the professor's massive binary file (rather than a text file of gibberish I create), and I honestly have NO idea how many threes are in it. My answer is coming out to 185k consistently, but the auto-grader attached to the compiler doesn't give feedback on if the answer is correct. I posted in the class's forum to see if someone can tell me what my target answer is, and I'll come back if for some reason mine is way off.

Thanks again!
I honestly have NO idea how many threes are in it.

At a command line, enter
Unix-like and maybe Apple systems: grep -Ec "3" your-profs-huge-text-file.txt
Windows: findstr -N "3" your-profs-huge-text-file.txt | find /c ":"
Last edited on
Pages: 12