4 Replies - 354 Views - Last Post: 27 June 2012 - 05:20 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Quick logic question

Posted 27 June 2012 - 04:30 PM

Hello all, been awhile since I have posted, and I have a quick question. I am just running through every exercise in my C++ books to keep up on coding while i'm on summer break, and I seem to have come across a slight problem...

I am reading in a text file and basically, when a line has the user defined string, it prints the line and counts each time that word occurred. However, according to Notepad++, the word "the" should occur 3,781 times, but when I run the program I only get 3,404 times... and I believe it has to do with the string member function find().

/********************************************
* File: stringSearch.cpp
* Purpose: 
*	Write a program that asks for a user to
*	enter the name of a file and a string
*	to search for. The program will then
*	display all lines the string occurs in
*	and how many times it occurs.
**********************************************/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

/*the = 3404 times, should be 3,781*/
int main()
{
	string infile, stringBuffer, stringBufCopy, userString;
	size_t strFound;
	int stringCount = 0;
	int stringIt = 0;

	cout << "Enter file name: ";
	getline(cin, infile);
	fstream file(infile, ios::in);
	if(!file)
	{
		while(!file)
		{
			cout << "Invalid file entry!" << endl;
			cout << "Re-enter file: ";
			getline(cin, infile);
			fstream file(infile, ios::in);
		}
	}
	cout << "Enter string to search for: ";
	getline(cin, userString);
	while(!file.eof())
	{
		getline(file, stringBuffer, '.');	// Get line from file until a '.' occurs
		stringBufCopy = stringBuffer;
		strFound = stringBuffer.find(userString);
		if(strFound != string::npos)
		{
			while(strFound != string::npos)
			{
				stringCount++;
				cout << stringBuffer << endl;
				strFound = stringBuffer.find(userString, strFound+1); // HERE maybe?
			}
		}
	}
	cout << "The string: \"" << userString << "\" was found " << stringCount << " times!" << endl;
	
	file.close();
	cin.clear();
	cin.sync();
	cin.get();
	return 0;
}



Also, one more question... What does string::npos mean?

EDIT:: I know there are some redundancies and I will change and modify, but I have just been moving things around and adding and removing stuff, so for now, it's just getting it working properly, and will then take out redundancies and useless code.

Attached File(s)

  • Attached File  taz.txt (256.71K)
    Number of downloads: 33

This post has been edited by IngeniousHax: 27 June 2012 - 04:35 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Quick logic question

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5507
  • View blog
  • Posts: 11,808
  • Joined: 02-June 10

Re: Quick logic question

Posted 27 June 2012 - 04:50 PM

My first thought is "the" occuring in a sentence twice or more, but the sentence only being counted as 1 "found".

Try the same code with a far more obscure word that you know doesn't happen more than once in a sentence.
Was This Post Helpful? 0
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Quick logic question

Posted 27 June 2012 - 04:57 PM

tlhIn`toq appears to be correct about the main issue but you have other issues too. a different algorithm would be beneficial as the number of instances of a word are not correlated to sentences.

in words like "there" you have "the" that std::string::find() will identify. you need to split your input into tokens and use something more like std::count or std::find(in <algorithm>). it's not very hard to write the function that splits a string by. this also solves the problem that tlhIn`toq pointed out.

also, on line 33 you have fstream file(infile, ios::in); which shadows your previous declaration meaning that it doesn't effect the 'file' that the while checks hence that if that loop enters it will loop forever.

This post has been edited by ishkabible: 27 June 2012 - 05:01 PM

Was This Post Helpful? 1
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4062
  • View blog
  • Posts: 12,541
  • Joined: 25-December 09

Re: Quick logic question

Posted 27 June 2012 - 05:14 PM

Quote

Also, one more question... What does string::npos mean?

This is the value returned from the string.find() functions. It is the largest number that can be expressed by std::string::size_t.

Also it is not usually recommended to use eof() to control your loop, use the return value of the actual read operation to control your loop.

You also may want to consider continuing your search after the searched word instead of one character after your word. The string.find() function returns the starting point of the found string, so you would use that plus the length of the searched string.

And don't forget that string.find() is case sensitive.


Jim

This post has been edited by jimblumberg: 27 June 2012 - 05:15 PM

Was This Post Helpful? 1
  • +
  • -

#5 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: Quick logic question

Posted 27 June 2012 - 05:20 PM

I believe my problem comes in because of case sensitivity... t != T, and therefore, would not match "the" with "The"... So now I just gotta convert the user input and the read input to either upper or lower, I think?


NVm, jim beat me to it. :D

This post has been edited by IngeniousHax: 27 June 2012 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1