Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Strange C++ homework issue.

$
0
0
Hello guys,

I would really appreciate it if someone would help me with this.
I don't need any complete solutions or someone to make my homework for me, but I just need someone to point out to me where the main issue is.

Here's the assignment:

Quote

Write a word counter that reads from a file and counts the amount of lines and word frequency. Case sensitivity needs to be lower case.


It's a really simple assignment, but for some reason, my program doesn't work.
The main issue is that while the words are being read, split and inserted into their appropriate places, the counter itself isn't incrementing and always shows "1" for every word, even when there are 4-5 instances of it in the file.
I'm sure it's some minor thing I've missed, but both me and my class mate have been scratching our heads over this for more than an hour. Any help would be greatly appreciated. Thanks in advance!!!

#include <cstdlib>
#include <iostream>
#include <String.h>
#include <fstream>
#include <cctype>

using namespace std;

string word_db[100]; //Words array (no duplicates)
int word_db_count[100]; //Count for every word in the array

string change_lower(string line) //Change all words to lowercase.
{
	for(int i=0;line[i];i++) 
	{
		if((line[i] > 64)&&(line[i] < 91)) //Check if in range of upper case characters
		{
			line[i] += 32; //If upper, offset 32 to make lower
		}
	}
	return line;
}

int scan(string word, int size) // Scan the word array to see if the word already exists.
{	
	for(int c=0; c<size; c++)
	{
		cout << word_db[c] << " " << word << "\n";
		if(word_db[c].compare(word))
		{
			return c; // If already in array, return possition.
		}
		else
		{
			return 0; //If not in the array, return 0.
		}
	}

}	

string word_count(string lines[], int line_count) 
{
	string temp_word; //Temporary storage for words.
	int count = 0; //Counter.
	int pos; // Position.
	
	for(int x=0; x< line_count; x++) //For every line
	{
		for(int y=0; y<lines[x].length();y++) //For every character in the line
		{
			if(lines[x].at(y)!=' ') //If the curren't character isn't a space.
			{
				temp_word += lines[x].at(y); //Add to temporary word.
			}
			else //If the character is a space.
			{ 
				pos = scan(temp_word, count); // Scan if the formed word is already in the word array.

				if(pos == 0) //Word wasn't in the array.
				{
					
					word_db[count] = temp_word; //Insert the new word into the array.
					
					word_db_count[count] = 1; //Increment count for the new word.
					count++;
				}
				else //Word already exists in the word array
				{
					word_db_count[pos] = word_db_count[pos] + 1; //Only increment count for the word.
				}
				temp_word.clear(); //Reset temporary word for the next one.
			}
		}
	}
	
}

int main()
{
	
	//**********************************************************************
	//This part definitely works and isn't where the problem is.
	//It reads the file and places every line of the file into the array as a separate element
	//plus counts all the lines.
	
	string input_line = ""; 
	int line_count = 0;
    string array_lines[100];
	ifstream input_file;

    input_file.open ("C:\\Dev-Cpp\\input.txt"); 
    while (!input_file.eof())
    {
		getline(input_file, input_line);
	 	array_lines[line_count] = change_lower(input_line);;
		line_count+=1;
	}

	//**********************************************************************			
	word_count(array_lines, line_count); //Problem starts from here
	
    cout<< word_db[1] << "\n"; //I can see that every word exists in the word_db if I change the position manually, but, if I go through it in a loop, the program always crashes.
    cout<< word_db_count[1] << "\n"; //Always shows count for the word as 1, even for words that have 4-5 instances in the file.

    system("PAUSE");
}




Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>