4 Replies - 528 Views - Last Post: 30 November 2010 - 03:54 PM Rate Topic: -----

#1 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

searching for and comparing array structs

Posted 30 November 2010 - 02:17 PM

hey guys, Im having trouble understanding how I should exactly search for my words in the array I have created. I need to search for the word then figure out if the word is "unique" meaning there are no other words like it, if the word is unique I need to add it to the array and set a count to 1. If it it not unique I need to increment my count to the position where the number was found.

So far I cant seem to even get my search right. I dont know exactly what to compare and what to use as my "size" of the array.
I am using the linear search algorithm for reference.

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

struct word
{
	char singleWord[21];
	int count;
};

int main()
{
	
	ifstream textDoc;
	char text[21];
	const int NUMBER=100;
	word wordList[NUMBER];
	int i=0;
	int j=0;
	char key;

	
	textDoc.open("attempt.txt");

	if (textDoc.fail())
	{
		cout<<"\nThe file didnt work "<<endl;
		exit(1);
	}

	cout<<"\nThe file worked! "<<endl;

	while(!textDoc.eof())
	{
	textDoc>>text;
	strcpy(wordList[i].singleWord,text);
	i++;
	}
	//test stuff delete this later**************************************
	for(j=0; j<i; j++)
	{
		cout<< wordList[j].singleWord<<endl;
	}
	textDoc.close();
	return 0;

}

int search(word wordList[],int count,char key[],word singleWord[])
{
	int pos = 0;
	int i;
    while (pos < count && !strcmp(wordList[pos],singleWord[i]))
        pos++;
	if (pos == count)
    pos = -1;
    return pos;

}


can someone point me in the right direction for the search function?
any help would be much appreciated, thanks!

sidenote: I am using a text document called attempt.txt it is attatched

Attached File(s)


This post has been edited by redrider11: 30 November 2010 - 02:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: searching for and comparing array structs

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: searching for and comparing array structs

Posted 30 November 2010 - 03:01 PM

You're reading into your array here:
while(!textDoc.eof())
{
    textDoc>>text;
    strcpy(wordList[i].singleWord,text);
    i++;
}



You don't see anything there that might be indicative of the size of the array?

As far what you're comparing, it's the TEXT isn't it? Where are you PUTTING the text you're reading?
Was This Post Helpful? 0
  • +
  • -

#3 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: searching for and comparing array structs

Posted 30 November 2010 - 03:25 PM

hmm im going to guess
wordList.singleWord



because the text from the document is being copied from the document into this list?

int search(word wordList[],int count,char key[])
{
	int pos = 0;

    while (pos < count && !strcmp(wordList[pos].singleWord,key ))
        pos++;
	if (pos == count)
	{
    pos = -1;
	}
    return pos;

}



like so?
Was This Post Helpful? 0
  • +
  • -

#4 redrider11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 85
  • Joined: 05-October 10

Re: searching for and comparing array structs

Posted 30 November 2010 - 03:48 PM

played with it a bit and this is what I have so far. I cant figure out why my position isnt returning right.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;

int search(struct word [],int ,char []);

struct word
{
	char singleWord[21];
	int count;
};

int main()
{
	
	ifstream textDoc;
	char text[21];
	const int NUMBER=100;
	word wordList[NUMBER];
	int wordCount=0;		
	int i=0;
	int j=0;
	char key[21];

	
	textDoc.open("attempt.txt");

	if (textDoc.fail())
	{
		cout<<"\nThe file didnt work "<<endl;
		exit(1);
	}

	cout<<"\nThe file worked! "<<endl;

	while(!textDoc.eof())
	{
	textDoc>>text;
	strcpy(wordList[i].singleWord,text);
	wordCount++;
	i++;
	}
	//test stuff delete this later**************************************
	for(j=0; j<i; j++)
	{
		cout<< wordList[j].singleWord<<endl;
	}
	cout<<wordCount<<" word count"<<endl;
	search(wordList, wordList[i].count, key);

	textDoc.close();
	return 0;

}

int search(word wordList[],int wordCount,char key[])
{
	int pos = 0;

    while (pos < wordCount && !strcmp(wordList[pos].singleWord,key ))
        pos++;
	if (pos == wordCount)
	{
    pos = -1;
	}
	cout<<pos<<" position"<<endl;
    return pos;

}


This post has been edited by redrider11: 30 November 2010 - 03:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: searching for and comparing array structs

Posted 30 November 2010 - 03:54 PM

search(wordList, wordList[i].count, key);

So...what's the value of key?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1