944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3097
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2006
1

Determine if a certain ivalue is in a string

Expand Post »
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;

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int isPresent(string value);
  8.  
  9. int main()//start of the main funtion
  10. {
  11. string value;
  12. string string;
  13. cout <<"Enter a string";
  14. cin >> string;
  15. cout <<"Enter element to query";
  16. cin>> value;
  17. cout << isPresent(value);
  18.  
  19. system("PAUSE");
  20. return EXIT_SUCCESS;
  21. }
  22. int isPresent(string value)
  23. {
  24.  
  25. for (int i = 0; i < value.length()-1; i++)
  26. {
  27. if ( string[i] == value)
  28. return false;
  29. }
  30. return true;
  31. }

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;
C++ Syntax (Toggle Plain Text)
  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. class LongString
  6. {
  7. private:
  8. vector <string> contents;
  9.  
  10. public:
  11.  
  12. int isPresent(string value);
Similar Threads
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

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.
Reputation Points: 52
Solved Threads: 4
Junior Poster in Training
rinoa04 is offline Offline
84 posts
since Sep 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

Maybe this perhaps...

C++ Syntax (Toggle Plain Text)
  1.  
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6. void isPresent(string,string);
  7. int main()
  8.  
  9. {
  10. string value;
  11. string myString;
  12. cout <<"Enter a string:";
  13. cin >> myString;
  14. cout <<"Enter element to query:";
  15. cin>> value;
  16. isPresent(value,myString);
  17.  
  18.  
  19. cin.get();
  20. cin.get();
  21. }
  22. void isPresent(string var,string myString)
  23. {
  24. string::size_type loc = myString.find( var, 0 );
  25. if( loc != string::npos )
  26. cout << "Found at " << loc << endl;
  27. else
  28. cout << "Didn't find " << endl;
  29. }

To handle white space consider changing the main part to...

C++ Syntax (Toggle Plain Text)
  1. {
  2. string value;
  3. string myString;
  4. cout <<"Enter a string:";
  5. getline(cin,myString,'\n');
  6. cout <<"Enter element to query:";
  7. getline(cin,value,'\n');
  8. isPresent(value,myString);
  9.  
  10. cin.get();
  11.  
  12. }
Last edited by iamthwee; Sep 17th, 2006 at 6:29 am. Reason: changing to handle whitespace
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

unfortunately, passing in one parameter is a precondition given to me by my lecturer. I too was puzzled by this.
Last edited by hay_man; Sep 17th, 2006 at 8:54 am.
Reputation Points: 38
Solved Threads: 0
Newbie Poster
hay_man is offline Offline
16 posts
since Jun 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

Click to Expand / Collapse  Quote originally posted by hay_man ...
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:
C++ Syntax (Toggle Plain Text)
  1. include <iostream>;
  2.  
  3. using namespace std;
  4.  
  5. int global;
  6.  
  7. void show()
  8. {
  9. cout << "The value is: " << global << endl;
  10. }
  11.  
  12. int main()
  13. {
  14. global = 10;
  15. show();
  16. global = 988;
  17. show()
  18. }

I haven't tested this, but you should get the idea.
Reputation Points: 129
Solved Threads: 11
Posting Whiz in Training
Anonymusius is offline Offline
223 posts
since Aug 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

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:

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. void isPresent(char value);
  6.  
  7. char str1[100];
  8.  
  9. int main()
  10. {
  11. char value;
  12. char str[100];
  13. cout <<"Enter a string: ";
  14. cin.getline(str,100);
  15. strcpy(str1, str);
  16. cout <<"Enter element to query: ";
  17. cin>> value;
  18. isPresent(value);
  19. return 0;
  20. }
  21.  
  22. void isPresent(char value)
  23. {
  24. for (int i = 0; i <= strlen(str1); i++)
  25. {
  26. if ( str1[i] == value )
  27. {
  28. cout << value << " is IN " << str1 << endl;
  29. exit(0);
  30. }
  31. }
  32. cout << value << " is NOT IN " << str1 << endl;
  33. }
Last edited by Salem; Sep 17th, 2006 at 12:41 pm. Reason: fix code tags
Reputation Points: 52
Solved Threads: 4
Junior Poster in Training
rinoa04 is offline Offline
84 posts
since Sep 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

Quote originally posted by hay_man ...
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().
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

Try using an global variable instead of passing one
Ssshhhhh...
Reputation Points: 197
Solved Threads: 12
Junior Poster
Grunt is offline Offline
147 posts
since Jul 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Sep 17th, 2006
1

Re: Determine if a certain ivalue is in a string

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
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Algorithm doubt
Next Thread in C++ Forum Timeline: Noob Help needed for Time problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC