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);

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.

Member Avatar for iamthwee

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();
  
}

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

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.

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;
}
Member Avatar for iamthwee

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().

Try using an global variable instead of passing one

Ssshhhhh...

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.

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

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

string string;

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.