Hey guys what is wrong in this code?
this is a very easy code...but gives me a headache
I get this error "Cannot convert 'int to 'void *'" and"Code has no effect"
#include<iostream.h>
#include<conio.h>
int main()
{
int findpos(char s[],char c);
char string[80],ch;
cout<<"Enter the string:";
cin.getline(string,80);
cout<<"Enter the character to be searched:";
cin.get(ch);
int y=0;
y= findpos(string,ch);
if(y==-1);
cout<<"There is no such charater";
getch();
return 0;
}
int findpos(char s[],char c)
{
int flag=-1;
for(int i=0;s[i] !='\0';i++)
{
if(s[i]==c)
{
flag=0;
cout<<"The character is found in the position:"<i+1;
}
}
return (flag);
}
#include<iostream>/// there is no file called iostream.h
#include<conio.h>
usingnamespace std;/// provide this namespace or else qualify names defined here with std::
int findpos(char s[],char c);///declare your functions outside main
int main()
{
char string[80],ch;
cout<<"Enter the string:";
cin.getline(string,80);
cout<<"Enter the character to be searched:";
cin.get(ch);
int y=findpos(string,ch); ///prefer intializing variables on declaration rather than assigning to them later
if(y==-1)///removed a semicolon
cout<<"There is no such charater";
else
cout<<"The character is found in the position:"<<y;///<<
getch();
return 0;
}
int findpos(char s[],char c)
{
int flag=-1;
for(int i=0;s[i] !='\0';i++)
{
if(s[i]==c)
{flag=i;} ///better
}
return (flag);
}
You are not going to find multiple letters using your way. After finding the last instance of the character, flag will be set to that location number, and any others will be forgotten. Starbug7's function works much better, for showing all the locations.