Determine if a certain ivalue is in a string

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Determine if a certain ivalue is in a string

 
1
  #1
Sep 17th, 2006
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;

  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;
  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);
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: Determine if a certain ivalue is in a string

 
1
  #2
Sep 17th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Determine if a certain ivalue is in a string

 
1
  #3
Sep 17th, 2006
Maybe this perhaps...

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

  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
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 16
Reputation: hay_man is an unknown quantity at this point 
Solved Threads: 0
hay_man hay_man is offline Offline
Newbie Poster

Re: Determine if a certain ivalue is in a string

 
1
  #4
Sep 17th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 223
Reputation: Anonymusius is on a distinguished road 
Solved Threads: 10
Anonymusius's Avatar
Anonymusius Anonymusius is offline Offline
Posting Whiz in Training

Re: Determine if a certain ivalue is in a string

 
1
  #5
Sep 17th, 2006
Originally Posted by hay_man View Post
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 84
Reputation: rinoa04 is on a distinguished road 
Solved Threads: 4
rinoa04's Avatar
rinoa04 rinoa04 is offline Offline
Junior Poster in Training

Re: Determine if a certain ivalue is in a string

 
1
  #6
Sep 17th, 2006
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Determine if a certain ivalue is in a string

 
1
  #7
Sep 17th, 2006
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().
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Determine if a certain ivalue is in a string

 
1
  #8
Sep 17th, 2006
Originally Posted by Anonymusius View Post
Try using an global variable instead of passing one
Ssshhhhh...
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Determine if a certain ivalue is in a string

 
1
  #9
Sep 17th, 2006
Originally Posted by Anonymusius View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Determine if a certain ivalue is in a string

 
1
  #10
Sep 17th, 2006
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC