I am trying to write a program that reads words from files and then puts them into arrays and then does a count on certain words. I have moved stuff around and really I am not sure that I am even on the right track. Could someone please look at this for me and tell me what I have wrong. It will not run at all. Also I am having trouble with the counting of the certain word. Any help would be greatly appreciated. This is what I have so far. I think I may have things in the wrong places. I am going to put the instruction for the part that I think I have done.

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace globalType;
void printResults();
print word_count(std::string);

int find(std::string[], int word_count, std::string a_word)
    {
        for(int i=0; i < word_count; ++i) if(words[i] == a_word) return i;
        return-1; // did not find word

    }



int main()
{
    const int MAX_WORDS = 100;
    std::string words[MAX_WORDS];
    int words[100][length of longest word +1]



    std::ifstream("SelectWords.txt");
    while(file >> words[word_count]) ++word_count;  //word_count contains number of words read
    int words[100][30];
    int word_count[100]={0}; //initialize to zeros



    int pos = find( words, word_count, word_read_from_file);
    if(pos != -1) ++word_count;  //if found, increment the count         <<<<<<<<  I am having trouble here

    system("pause");
    return 0;
}

INSTRUCTIONS:

Read the words from a file named “SelectWords.txt” into a 100-element array of strings.
Declare and initialize to zeroes a parallel array of whole numbers for word counts
Declare and initialize to zeroes a parallel array of doubles for word frequencies.
Declare two strings to hold the shortest and longest words in the data file. Initialize the shortest word to “thisisarunonsentencewithlotsofletters” and the longest word to “a”.
Declare a whole number variable to hold the total word count, and initialize it to zero.

Well, in my opinion, this is a nonsensical exercise. I would have my students construct an array with 100 elements that are structures, the first field of which would be the string, and the second would be the count. IE:

struct word_count {
    string word;
    int count;
    };

struct word_count[100] words;
int next_element = 0;

Each word would be looked up in the array, and appended to the array at last_element (with count set to 1), which then would be incremented.

Yes you can use two arrays, but why? If this is required (seems so from the instructions), you would do this:

string words[100];
int word_count[100];
int next_element = 0;

Look up the word from 0 to next_element:

bool found = false;
for (int i = 0; i < next_element && found == false; i++)
{
    if (words[i] == word)
    {
        word_count[i]++;
        found = true;
    }
}
if (!found)
{
    words[next_element] = word;
    word_count[next_element] = 1;
    ++next_element;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.