1 Replies - 1819 Views - Last Post: 08 December 2012 - 09:37 PM Rate Topic: -----

#1 pe123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 24-November 12

Reading words from a file.

Posted 08 December 2012 - 12:19 PM

I have a question about reading words from a file. Could someone please look at this and tell me if I am making the program better or worse:) I don't understand how to write the code to make the program read the words from the file.
I am sending the direction and what I have so far but the program will not run at all. I also have another question, I keep getting error messages that say "col, row, and matrix are undeclared identifier" why does this happen when I don't have those things in the program? I used them at one time but I erased them.

   #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

	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.
Open and read one of three data files, provided by the instructor, examining each word as it is read.
If the word matches one in the SelectWords array, increment the corresponding count in the parallel array of whole numbes.
If the word is shorter than the word currently in the ShortestWord string, replace the contents of that string with the word from the data file.
If the word is longer than the word currently in the LongestWord string, replace the contents of that string with the word from the data file.
Increment the TotalWordCount by one for each word read from the data file.
After all words have been read from the data file, calculate the frequency of occurrence of each of the SelectedWord’s by dividing its count by the TotalWordCount, and save the calculated value in the corresponding element of the array of doubles.
Display the name of the data file, the shortest word, longest word, and total word count.
Display the arrays of SelectedWord’s, their counts, and their frequency of occurrence in a columnar fashion. Format the frequency of occurrence with 2 significant digits and a field width of 6 characters.
For bonus credit: Declare an array of at least 30 integers and initialize each of them to zero. Use each to accumulate a count of all the words of that length. For instance, array[5] should hold a count of all words that were read from the data file that had a length of 5 characters. After all other output is complete, display the contents of this array, formatted as “N words of length M”, substituting the count for N and the array index for M. Do not display word lengths for which the count is zero

Is This A Good Question/Topic? 0
  • +

Replies To: Reading words from a file.

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1276
  • View blog
  • Posts: 4,395
  • Joined: 19-February 09

Re: Reading words from a file.

Posted 08 December 2012 - 09:37 PM

You need a name for the file stream. The class ifstream is basically a data type.

26	    std::ifstream file("SelectWords.txt");



The array will store unique words : eg if 'the' is already in the array and another 'the' is found in the file, it is not added to the array - the corresponding word count array value is incremented.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1