Hi all fri,

I got some problems in my simple searching program -
what i want to do is to search one or more character ('needle') from the string 'haystack' .
The function " int stripos(char *haystack,char *needle,int offset)" should return -1 when the character was not found;
if found , return the first position .
But i got 2 errors , i use MS VC++6.0.
I have less experience in this compiler.
anybody can help me , Please. :) :'( :'(
The following is my code.

#include<iostream>
//#include<stdio>
#include<string>
#include<cctype>
using namespace std;

int stripos(char *,char *,int);
int main(void)
{
	char *MainMsg[20],*SearchChar;
	//char *M,*S;
	int result;
	//int stripos(char *haystack,char *needle,int offset);

	//clrscr();

	//// User data entry ////
	cout<<"\n Enter desire message.";
	gets(MainMsg);

	cout<<"\n Enter one or more characters to be searched.";
	cin>>SearchChar;

	
	//// View back user data ////
	cout<<MainMsg;
	cout<<SearchChar;
	
	///// Change two strings to lower case character /////
	//M=MainMsg;
	//S=SearchChar;
	for(int i=0;MainMsg[i];i++)
	{
		MainMsg[i]=tolower(MainMsg[i]);
	}

	for(int j=0;SearchChar[j];j++)
	{
		SearchChar[j]=tolower(SearchChar[j]);
	}

	
	//// View back two strings after changing to lower case character/////
	cout<<MainMsg;
	cout<<SearchChar;

	//// Call Search Function ///
	result=stripos(MainMsg,SearchChar,1);
	
	if(result)	cout<<result;

	return 0;

}//main


int stripos(char *haystack,char *needle,int offset)
{
	int i=1,position;
	while(*haystack!=0)
	{
		if(needle[0]==*haystack)
			while(*needle!=0)
			{
				if(needle[i] ==0)				return position;
				if(needle[i] !=haystack[i])		break;
			}

		haystack++;
		position;

	}

	return -1;


}

Error message are :
error C2664: 'gets' : cannot convert parameter 1 from 'char *[20]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

error C2664: 'tolower' : cannot convert parameter 1 from 'char *' to 'int'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast

error C2664: 'stripos' : cannot convert parameter 1 from 'char *[20]' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Thanks a lot in advance to all fri ;) :icon_lol:

Let me start by asking you if there's a reason that you're not using std::strings? Is this homework for example?

Change this :

char *MainMsg[20],*SearchChar;

To

const int MAXSIZE = 100;
char MainMsg[MAXSIZE];
char SearchChar[MAXSIZE];

Better yet, get rid of those and use std::strings.

This article has been dead for over six months. Start a new discussion instead.