954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Determine if a certain ivalue is in a string

Here's a really simple problem. I understand the basic search algorithms however, is there a difference when implemeting them in regards to vectors or strings? I want to determine if a certain value using a sequential search, the code would look something like this;

#include <iostream> 
#include <fstream> 
#include <cstring> 
#include <vector>
using namespace std;
 
int isPresent(string value);
 
int main()//start of the main funtion
{
    string value;
    string string;
    cout <<"Enter a string";
    cin >> string;
    cout <<"Enter element to query"; 
    cin>> value;
    cout << isPresent(value); 
 
  system("PAUSE");
  return EXIT_SUCCESS;  
}
int isPresent(string value)
     {
 
     for (int i = 0; i < value.length()-1; i++)
       {
          if ( string[i] == value)
          return false;
          }
          return true;
     }


however a syntax error keeps occuring, I cant seem to find the problem,
thanks for your help!


in regards to implementing this in a class, the same problem arrises;

#include <vector>
#include <string>
#include <iostream>
using namespace std;
class LongString 
{
 private: 
  vector <string> contents;
 
 public:
 
  int isPresent(string value);
hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 38
Solved Threads: 0
 

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
 

unfortunately, passing in one parameter is a precondition given to me by my lecturer. I too was puzzled by this.

hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 38
Solved Threads: 0
 
unfortunately, passing in one parameter is a precondition given to me by my lecturer. I too was puzzled by this.


Try using an global variable instead of passing one
for example:

include <iostream>;

using namespace std;

int global;

void show()
{
    cout << "The value is: " << global << endl;
}

int main()
{
    global = 10;
    show();
    global = 988;
    show()
}


I haven't tested this, but you should get the idea.

Anonymusius
Posting Whiz in Training
238 posts since Aug 2006
Reputation Points: 129
Solved Threads: 11
 

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


Ssshhhhh...

Grunt
Junior Poster
152 posts since Jul 2006
Reputation Points: 197
Solved Threads: 12
 
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
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Ha.! I remember my C++ instructor promising not to give us a bad grade on an assignment if we promised not to use global variables...lol

FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 

Here's the big glaring error I see in your code...

string string;


You can't use "string" as a variable name.

FC Jamison
Posting Pro in Training
Team Colleague
446 posts since Jun 2004
Reputation Points: 92
Solved Threads: 21
 

Thanks to all for your suggestions, FC Jamison, ye I also saw that big glaring error once i had posted my problem- thanks anyway.

hay_man
Newbie Poster
16 posts since Jun 2006
Reputation Points: 38
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You