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

Recommended Answers

[quote=Anonymusius;253177]Try using an global variable instead of passing one
[/quote]

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 …

Jump to Post

All 11 Replies

Re: Determine if a certain ivalue is in a string 80 80

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.

Re: Determine if a certain ivalue is in a string 80 80

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();
  
}
Re: Determine if a certain ivalue is in a string 80 80

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

Re: Determine if a certain ivalue is in a string 80 80

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.

Re: Determine if a certain ivalue is in a string 80 80

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;
}
Re: Determine if a certain ivalue is in a string 80 80

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

Re: Determine if a certain ivalue is in a string 80 80

Try using an global variable instead of passing one

Ssshhhhh...

Re: Determine if a certain ivalue is in a string 80 80

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.

Re: Determine if a certain ivalue is in a string 80 80

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

Re: Determine if a certain ivalue is in a string 80 80

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

string string;

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

Re: Determine if a certain ivalue is in a string 80 80

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 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.