search function not returning anything at all

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 624 Views - Last Post: 13 May 2013 - 08:23 AM Rate Topic: -----

#1 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

search function not returning anything at all

Posted 11 May 2013 - 04:30 PM

Hi everyone,

I am trying to use a search function to find an inputted character, find that characters array position, display the character and one additional detail about the character. However, I don't think the search is returning anything because the space is blank where the character should be. A couple of errors do show up, so perhaps they are the cause, but I haven't been able to resolve them. Firstly,
if(whichLetter[mid] = [b]favorite[/b]) //favorite is undefined even though it is a parameter in the function
				return [b]mid[/b]; //return value type does not match the function type


Secondly,
cout << "The letter " << search([b]whichLetter[/b][favorite]... //argument type "char" is incompatible with parameter of type "char*"


For clarification: the search function:
char search(char whichLetter[], char &favorite, int n);
{
	int top = 0;
	int bottom = n - 1;
	int mid;

	while(top <= bottom)
	{
			mid = (top + bottom) / 2;
			if(whichLetter[mid] = favorite)
				return mid;
			else if(whichLetter[mid] > favorite)
				bottom = mid - 1;
			else
				top = mid + 1;
	}
	return -1;
}


This post has been edited by breezett93: 11 May 2013 - 04:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: search function not returning anything at all

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4027
  • View blog
  • Posts: 12,434
  • Joined: 25-December 09

Re: search function not returning anything at all

Posted 11 May 2013 - 05:07 PM

First in the following snippet do you realize the operator= is the assignment operator? The comparison operator is operator==.
            if(whichLetter[mid] = favorite)


Second please show how you are calling this function. Show how the variables are defined that you use in the function call.

Please don't try to use bold tags within the code tags, they are ignored inside the code and it just makes reading your code harder.

Also why are you returning an int when you told the compiler you would be returning a char?

Jim

This post has been edited by jimblumberg: 11 May 2013 - 05:09 PM

Was This Post Helpful? 0
  • +
  • -

#3 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 11 May 2013 - 05:14 PM

I tried both = and == but the error remained.
Even when I defined the search function as an integer, the error remained.

calling the function in main, after the data files has been read, and the count of each letter in the alphabet has been kind of sorted.
//Declare variables
	int letterCount[26];
	int totalCount = 0;
	char whichLetter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	string filename;
	char favorite;

//Retrieve favorite letter
	cout << "what's your favorite letter?";
	cin >> favorite;

	cout << "The letter " << search(whichLetter[favorite], favorite, 26) << "occurs " << letterCount[whichLetter[favorite]] << " times." << endl;


instead of correctly displaying the letter, there is blank space, and instead of displaying the correct number of times, the highest letter count value is displayed.

This post has been edited by breezett93: 11 May 2013 - 05:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4027
  • View blog
  • Posts: 12,434
  • Joined: 25-December 09

Re: search function not returning anything at all

Posted 11 May 2013 - 07:03 PM

I think you need to review the ASCII table. If you enter a letter A - Z for your input of favorite you will be accessing your array out of bounds with the following statement:
whichLetter[favorite]


You have defined whichLetter with a size of 26. What is the numeric value of 'A'?

Jim
Was This Post Helpful? 0
  • +
  • -

#5 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 11 May 2013 - 07:14 PM

Prior to being sorted, 'A' would be whichLetter[0]. After it is sorted, I have no idea what 'A' is. Basically, that what the search function needs to figure out. How would I avoid exceeding the bound? The search function should take the inputted letter, find its location within whichLetter[], and then with letterCount[] print that letter, and how many times it showed up. If I use Ascii keys, I would have to redesign the whole program, which I can do, but...I am already so far with this setup.
This function counts the number of occurrences of each letter seen in any data file.
void countLetters(ifstream &input, int letterCount[], int &totalCount)
{    
      
      char hold('a');
      totalCount = 0;
     
     
      for(int count = 0; count < 26; count++)
      {
            letterCount[count] = 0;
      }

      while(!input.eof())
      {
            input >> hold;
           
            if(isalpha(hold))
            {
                  letterCount[toupper(hold) - 'A']++;
                  totalCount++;
            }
      }
}


This function sorts these letters greatest to least
void sort(int letterCount[], char whichLetter[], int n)
{
//declare objects
int hold;
char letter;

//sorts both arrays
for(int count = 0; count < n; count++)
{
	for(int count2 = 0; count2 < n; count2++)
	{
		if(letterCount[count] < letterCount[count2 + count])
		{
			hold = letterCount[count2 + count];
			letterCount[count2 + count] = letterCount[count];
			letterCount[count]=hold;
			letter=whichLetter[count2 + count];
			whichLetter[count2 + count]=whichLetter[count];
			whichLetter[count]=letter;
		}
	}
return;
}


Then, in my main, I call them
countLetters(input, letterCount, totalCount);
sort(letterCount, whichLetter, 26);


At this point, then the user indicates which letter he would like to see specifically
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 11 May 2013 - 09:55 PM

Does this make sense?

12	        if(letterCount[count] < letterCount[count2 + count])




You want to check every element against every other element :


12	        if(letterCount[count] < letterCount[count2])


Was This Post Helpful? 0
  • +
  • -

#7 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 02:30 PM

@define--I followed up on your suggested code and my output was still the same. Perhaps other lines are affected too.

Any thoughts on the "argument of type 'char' is incompatible with parameter of type 'char*'" with this line?
cout << "The letter " << search(whichLetter[favorite], favorite, 26) << "occurs " << letterCount[whichLetter[favorite]] << " times." << endl;


Was This Post Helpful? 0
  • +
  • -

#8 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3556
  • View blog
  • Posts: 11,027
  • Joined: 05-May 12

Re: search function not returning anything at all

Posted 12 May 2013 - 02:37 PM

The first parameter of your search function takes a char array (eg. a pointer to char), but you are passing a single character.
Was This Post Helpful? 0
  • +
  • -

#9 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 02:55 PM

Ahh ok, so i should actually just have 2 parameters,
char search(char &favorite, int n);


and define the array needed inside the code.
char whichLetter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


The only problem is, I am still getting a blank answer.

This post has been edited by breezett93: 12 May 2013 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 04:15 PM

View Postbreezett93, on 12 May 2013 - 10:55 PM, said:

Ahh ok, so i should actually just have 2 parameters,
char search(char &favorite, int n);


and define the array needed inside the code.
char whichLetter[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";



No, whichLetter[0] returns a letter/character.
Was This Post Helpful? 0
  • +
  • -

#11 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 04:37 PM

That's true. But I dont necessarily always want the first position in the array. The user enters which letter he wants to see info about. Then the search function should scan whichLetter[], find the entered letter, retrieve it, and print it.
Was This Post Helpful? 0
  • +
  • -

#12 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 04:51 PM

So if we set favorite to 0 in your code, as an example, you would pass a letter as the first argument of the search function, which is supposed to be an array.

favorite = 0;
cout << "The letter " 
     << search(whichLetter[0], favorite, 26) 
     << "occurs " 
     << letterCount[whichLetter[0]] 
     << " times." 
     << endl;




char search(char whichLetter[], char &favorite, int n);




Your definition of the search function in post #1 has a extra semicolon at the end of the function's head.

.

This post has been edited by #define: 12 May 2013 - 04:51 PM

Was This Post Helpful? 0
  • +
  • -

#13 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 05:08 PM

Oh ok. That makes sense. However, this error shows up again on whichLetter[]
"argument type 'char' is incompatible with parameter of type 'char*'". Whats the difference?
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1337
  • View blog
  • Posts: 4,577
  • Joined: 19-February 09

Re: search function not returning anything at all

Posted 12 May 2013 - 05:25 PM

A char is a character or letter, char* is a pointer to (address of) a type of char. In this case the pointer is used to point to the start of a char array.

To pass the array to the function you just use the name of the array.

cout << "The letter "
     << search(whichLetter, favorite, 26) 




You probably want search function to return an integer.

int search(char whichLetter[], char &favorite, int n);


Was This Post Helpful? 0
  • +
  • -

#15 breezett93  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-March 13

Re: search function not returning anything at all

Posted 12 May 2013 - 05:33 PM

Ok. Gotcha.
There are no errors anymore, however, the final cout statement doesnt show up at all. This is making me think that perhaps the contents of my search function are incorrect. I am trying to use the binary search method. It looks correct compared to an example I found, but does it look right to you?
int search(char whichLetter[], char &favorite, int n);
{	
	int top = 0;
	int bottom = n - 1;
	int mid;
	

	while(top <= bottom)
	{
		mid = (top + bottom) / 2;
		if(whichLetter[mid] = favorite)
			return mid;
		else if(whichLetter[mid] > favorite)
			bottom = mid - 1;
		else
			top = mid + 1;
	}
	return -1;
}


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2