I am not too sure of what your algorithm does but from your logic I assume that your program is to search whether the character entered is exist in the string you type.
As for your program, why don't you use character array data type ( char str[100] ) which usually representing strings in C++ or character data type ( char value ) which represent a single character. I am not sure of the use of data type string in C++ except in Java. Beside that, your "isPresent" function only accept one parameter "value". If so, you can't compare the string entered with the value entered in the if statement. The string entered is not pass in to the "isPresent" function to be compared.
Hope this will help you.
rinoa04
Junior Poster in Training
84 posts since Sep 2006
Reputation Points: 52
Solved Threads: 4
Maybe this perhaps...
#include <iostream>
#include <string>
using namespace std;
void isPresent(string,string);
int main()
{
string value;
string myString;
cout <<"Enter a string:";
cin >> myString;
cout <<"Enter element to query:";
cin>> value;
isPresent(value,myString);
cin.get();
cin.get();
}
void isPresent(string var,string myString)
{
string::size_type loc = myString.find( var, 0 );
if( loc != string::npos )
cout << "Found at " << loc << endl;
else
cout << "Didn't find " << endl;
}
To handle white space consider changing the main part to...
{
string value;
string myString;
cout <<"Enter a string:";
getline(cin,myString,'\n');
cout <<"Enter element to query:";
getline(cin,value,'\n');
isPresent(value,myString);
cin.get();
}
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Since your requirement is using only one parameter, there is another way to solve this problem. The solution is using strcpy. The string entered can be copy to a global variable to store the content for the use in isPresent function. You can refer the coding below:
#include <iostream>
#include <cstring>
using namespace std;
void isPresent(char value);
char str1[100];
int main()
{
char value;
char str[100];
cout <<"Enter a string: ";
cin.getline(str,100);
strcpy(str1, str);
cout <<"Enter element to query: ";
cin>> value;
isPresent(value);
return 0;
}
void isPresent(char value)
{
for (int i = 0; i <= strlen(str1); i++)
{
if ( str1[i] == value )
{
cout << value << " is IN " << str1 << endl;
exit(0);
}
}
cout << value << " is NOT IN " << str1 << endl;
}
rinoa04
Junior Poster in Training
84 posts since Sep 2006
Reputation Points: 52
Solved Threads: 4
unfortunately, passing in one parameter is a precondition given to me by my lecturer. I too was puzzled by this.
Why don't you explain how your class you are supposed to design works. It might explain why you teachers only expects one parameter to the function isPresent().
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Try using an global variable instead of passing one
Yes i completely agree with Mr. Grunt, passing global variables is not the best programming practice around. And to suggest this to someone who has just started programming is not a good thing since they dont know the side effects or bad effects associated with them.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734