word count

I'm writing code to keep track of every word in a file in a sorted list and how often it occurs, print out each word and how many times it occurs, total words, and unique words. I'm having trouble printing out a string with a numeric value stored in it. It prints out the ASCII character. How can I print out the actual integer? I marked off with a bunch of "*"s where I need help. Thank you.

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

#include <fstream>
#include <iostream>// for input and output files
#include<string>
using namespace std;
int main()
{
    ifstream infile;//input file name
    ofstream outfile;//output file name
    char r = 0, c = 0, achar;//char variable names
    char inputfile[ 250 ];//input file name
    int len = 0, pos1 = 0, pos2 = 0, count = 1, numberofwords = 0, i = 0, j = 0, a = 0, n = 0, uniquewords = 0;//int variable names
    string mystring, myarr[ 250000 ], temp, word;//string names

    cout << "Enter the name of the file you would like to open (including extension): \n \n";//user enters name of txt file they want to oen
    cin >> inputfile;//assigns user-typed file to "inputfile"

    infile.open( inputfile );//opens user selected input file
    if( !infile )//if infile does not open
    {
        cerr << "ERROR 001 OCCURRED WHEN ATTEMPTING TO OPEN FILE\n\n" << endl;//prints error
        exit( 001 );//exits
    }//end if

    outfile.open( "outputfile.txt" );//opens outputfile
    if( !outfile )//if outfile does not open
    {
        cerr << "ERROR 002 OCCURRED WHEN ATTEMPTING TO OPEN OUTPUTFILE.TXT\n\n" << endl;//prints error
        exit( 002 );//exits
    }//end if

    while( infile.get( achar ) )//gets each character from the input file
    {
        if( achar != '\n' && achar != '`' && achar != '~' && achar != '!' && achar != '@' && achar != '#' && achar != '$' && achar != '$' && achar != '%' && achar != '^' && achar != '*' && achar != '(' && achar != ')' && achar != '_' && achar != '-' && achar != '+' && achar != '=' && achar != '[' && achar != '{' && achar != '}' && achar != ']' && achar != '|' && achar != ';' && achar != ':' && achar != ',' && achar != '<' && achar != '.' && achar != '>' && achar != '/' && achar != '?' )//if achar is not a symbol
        {
            if ( achar == 'A' || achar == 'B' || achar == 'C' || achar == 'D' || achar == 'E' || achar == 'F' || achar == 'G' || achar == 'H' || achar == 'I' || achar == 'J' || achar == 'K' || achar == 'L' || achar == 'M' || achar == 'N' || achar == 'O' || achar == 'P' || achar == 'Q' || achar == 'R' || achar == 'S' || achar == 'T' || achar == 'U' || achar == 'V' || achar == 'W' || achar == 'X' || achar == 'Y' || achar == 'Z' )//if achar is a capital letter
            {
                mystring += achar + 32;//change it to a lower-case letter
            }//end if
            else if ( achar == '—' )//if achar is an emdash
            {
                infile.get( achar );//get the next character
            }//end else if
            else
                mystring += achar;//add the achar to the end of mystring
        }//end if
    }//end while
  
    len = mystring.length( );//finds the length
  
    for ( n = 0; n < len - 1; n++ )//finds number of words
    {
        n = mystring.find( " ", n + 1 );//finds a space
        numberofwords++;//adds 1 to the number of words
    }//end for

    pos1 = 0;//sets pos1 to 0
    pos2 = mystring.find( " ", pos1 + 1 );//finds the first blank space
    word = mystring.substr( pos1, pos2 );//read in a substring of my string starting at the first char of length pos2 into word
    myarr[0] = word;//reads in the first word as myarr[0]
  
    for ( n = 1; n <= numberofwords; n++ )//finds the rest of the words and stores them in myarr
    {
        pos1 = pos2;
        pos2 = mystring.find( " ", pos1 + 1 );
        word = mystring.substr( pos1 + 1, pos2 - pos1 - 1 );
        if( ( pos2 - pos1 ) > 1 )
        {
            myarr[ n ] = word;
        }//end if   
    }//end for

    for ( j = 1; j <= numberofwords; j++ )//sorts array into ascending order
    {
        for ( a = 0; a < ( numberofwords - 1 );  a++ )
        {
            if ( myarr[ a + 1 ] < myarr[ a ] )
            {
                temp = myarr[ a ];
                myarr[ a ] = myarr[ a + 1  ];
                myarr[ a + 1 ] = temp;
            }//end if
        }//end for
    }//end for



    for (n=0; n<numberofwords; n++ )
    {
        if ( myarr[ n ] == myarr[ n + 1 ] )
        {
            count++;
        }//end if
        else
/*******************************************************************************
The next 3 or 4 lines is where I'm having trouble.
*******************************************************************************/
        {
            mystring = count;
            mystring = mystring.append( "  " );
            mystring = mystring.append( myarr[ n ] );
            myarr[ n ] = mystring;
            uniquewords++;
            cout << myarr[ n ] << endl;
            count = 1;
        }//end else
    }//end for
    
    for ( j = 1; j <= numberofwords; j++ )//sorts array into ascending order
    {
        for ( a = 0; a < ( numberofwords - 1 );  a++ )
        {
            if ( myarr[ a + 1 ] < myarr[ a ] )
            {
                temp = myarr[ a ];
                myarr[ a ] = myarr[ a + 1  ];
                myarr[ a + 1 ] = temp;
            }//end if
        }//end for
    }//end for
    
    for( n = 0; n <= uniquewords - 1; n++ )
    {
        outfile << myarr[ n ] << endl;
    }//end for
    cout<<endl<<endl<<endl;
    for( n = uniquewords - 1; n > uniquewords-15; n-- )
    cout << myarr[ n ]<<endl;
    
    outfile << endl << "There are a total of: " << uniquewords << " unique words in this file.";

    infile.close();//closes the input file
    outfile.close();//closes outputfile.txt
    
    system( "PAUSE" );
    return 0;
}//end main
Last edited on
What is the problem?
Please use [code][/code] tags
Topic archived. No new replies allowed.