Check it out

Jan 20, 2015 at 3:05pm
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"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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);
}

Last edited on Jan 20, 2015 at 3:07pm
Jan 20, 2015 at 3:13pm
ition:"<i+1

you need double arrows there (line 26).

plus <iostream.h> shouldn't be used. use <isostream>
Last edited on Jan 20, 2015 at 3:15pm
Jan 20, 2015 at 3:26pm
Function declaration on line 5 should be moved out of main. Put it right below your #included headers.

Line 13 should not end in a semicolon.

For your headache problem, try some indentation.
Jan 20, 2015 at 3:28pm
In addition to what mutexe said..

Declare this function, int findpos(char s[],char c); outside of main.

Also, remove the semicolon after if(y==-1);, as it makes it an empty if statement, and the next line will print, no matter what the value of y, is.
Jan 20, 2015 at 3:29pm
closed account (SECMoG1T)
like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<iostream>/// there is no file called iostream.h
#include<conio.h>

using namespace 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);
}
Jan 20, 2015 at 3:38pm
@andy1992

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.
Last edited on Jan 20, 2015 at 3:40pm
Jan 20, 2015 at 3:42pm
thank you @mutexe and @whitenite1 and @andy1992 and yeah sure, :) booradely60
Last edited on Jan 20, 2015 at 3:46pm
Topic archived. No new replies allowed.