Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,144 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,077 people online right now. Registration is fast and FREE... Join Now!




Help with find of string library

 
Reply to this topicStart new topic

Help with find of string library

emi
16 May, 2007 - 04:15 AM
Post #1

New D.I.C Head
*

Joined: 16 May, 2007
Posts: 1


My Contributions
Hello everyone,
I am writing a code at the moment where I whant to find a substring in a string. I am using Microsoft Visual Studio C++ 6.0. My problem is that I am using method find of the string library and something is going whrong. My code is something like that:

#include <string.h>
#include <iostream.h>
main()
{
char* st;
strcpy(st,"Hello, can you find Word1?");
unsigned int position =unsigned int(st.find("Word1"));
cout << "First occurrence of Ben was found at: " << position << endl;
}


And I keep getting those 2 messages:
error C2228: left of '.find' must have class/struct/union type

fatal error C1903: unable to recover from previous error(s); stopping compilation
I'm freaked out, I trieded to run it on another machine but it was the same. Any help would be greate!!!!!!!!!

User is offlineProfile CardPM
+Quote Post

qdoom
RE: Help With Find Of String Library
16 May, 2007 - 04:56 AM
Post #2

D.I.C Head
**

Joined: 31 Aug, 2006
Posts: 82


My Contributions
Well, if you're using <string> libarary, why are you using char*?
Try changing char* to string. strcpy won't work then though, so you need to change that too.

CODE
//Compiled with Dev-Cpp.

#include <string>
#include <iostream>

using namespace std;

main()
{
string st;
st = "Hello, can you find Word1?";
int position = int(st.find("Word1"));
cout << "First occurrence of Ben was found at: " << position << endl;
}

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Help With Find Of String Library
16 May, 2007 - 05:24 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
string.h and string are two different libraries...in c++, the string.h library functions can be found in cstring. there is no find() function in string.h...it is the .find() method from the string object.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:51PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month