| | |
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:
Solved Threads: 0
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;
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)
#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;
C++ Syntax (Toggle Plain Text)
#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.
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.
Maybe this perhaps...
To handle white space consider changing the main part to...
C++ Syntax (Toggle Plain Text)
#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...
C++ Syntax (Toggle Plain Text)
{ 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(); }
Last edited by iamthwee; Sep 17th, 2006 at 6:29 am. Reason: changing to handle whitespace
*Voted best profile in the world*
•
•
•
•
unfortunately, passing in one parameter is a precondition given to me by my lecturer. I too was puzzled by this.
for example:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by Salem; Sep 17th, 2006 at 12:41 pm. Reason: fix code tags
•
•
•
•
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*
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
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
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Need help writing an algorithm for this program
- Next Thread: Noob Help needed for Time problem
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






