need some help with C string

Thread Solved
Reply

Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

need some help with C string

 
0
  #1
Jun 29th, 2009
i am supposed to make 2 functions called find and they are supposed to find a character or string if they are similar

when i do this progam gives me weird error:
"use of search ambigious"
"first declared as class search here.
//main
  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. #include "strextra.h"
  5.  
  6. int main()
  7. {
  8. search a;
  9. cout<<"enter max number of characters in the sentence";
  10. int number;
  11. cin>>number;
  12. cout<<"enter a sentence";
  13. char let;
  14. char mycstring[number];
  15. for(int i=0; i<number; i++)
  16. {
  17. cin>>let;
  18. mycstring[i]=let;
  19. }
  20. mycstring[number]='\0';
  21. cout<<"my string is\n"<<mycstring<<"\n";
  22. cout<<"enter the character you are trying to find in the string above\n";
  23. char alpha;
  24.  
  25. char lett[1];
  26. for(int a=0; a<1; a++)
  27. {
  28. cin>>alpha;
  29. lett[a]=alpha;
  30. }
  31. lett[1]='\0';
  32. a.find(mycstring, lett);
  33.  
  34. }
//interface
  1. #ifndef STREXTRA_H_INCLUDED
  2. #define STREXTRA_H_INCLUDED
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class search
  8. {
  9. public:
  10. int find(char mystring[],char letter[]);
  11. int find(char mystring,string word);
  12.  
  13. };
  14.  
  15. #endif // STREXTRA_H_INCLUDED


//implementation
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5. #include "strextra.h"
  6.  
  7. int search::find(char mystring[],char letter[])
  8. {
  9. return strcspm(mystring[],letter[]);
  10. }
Last edited by lotrsimp12345; Jun 29th, 2009 at 3:07 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 675
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 131
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster

Re: need some help with C string

 
0
  #2
Jun 29th, 2009
search is a standard name declared in the <algorithm> header. It's a very bad idea to use standard names even if you don't include the header because other standard headers could include it. Try capitalizing your class from search to Search, or put it in a namespace.
-Tommy (For Great Justice!) Gunn
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #3
Jun 29th, 2009
thanks for the help now it says undefined reference to search::find(char*,char*). does that mean my return statement doesn't match up?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #4
Jun 29th, 2009
i am guessing the problem is that the parameters and arguments are matching up.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: need some help with C string

 
0
  #5
Jun 29th, 2009
You have several problems.
But first, was it requested that you enter the maximum number of characters or did you decide that your self?
If yourself then merely use a large static buffer and watch when you're approaching the end of buffer. And set a certain keyboard character to be the end of buffer indicator like the carriage return!

Also you created an array of [1] but you're writing one character past it? Should be [2].
char lett[2];
And why are you using a loop for handling a single character compare? First you should just be comparing a single character to a string
if (isMatch( const char *str, char c ))

but if you insist on using a string then remove the loop and just do the one character input and set your terminator like you are.

You are trying to replicate two standard library functions:
  1. const char * strstr( const char *, const char * );
  2. const char * strchr( const char *, char );

You can find the match through indexing or pointer advance!
Try again and post your results!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #6
Jun 29th, 2009
i haven't learnt about pointers yet. That's the next section. He says to use c-string and overloading
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 791
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 134
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: need some help with C string

 
0
  #7
Jun 29th, 2009
>search is a standard name declared in the <algorithm> header. It's a very bad
>idea to use standard names even if you don't include the header because other
>standard headers could include it.
Indeed, This is why you are advised to explicitly qualify the names of a namespace rather than using the 'stupid' "using namespace std;" on top of your program file.


>thanks for the help now it says undefined reference to
>search::find(char*,char*). does that mean my return statement doesn't match
>up?
Are you compiling your implementation CPP as well?
  1. g++ main.cpp header.cpp -o out
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #8
Jun 29th, 2009
yeap i am compiling all of my files.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #9
Jun 29th, 2009
a c-string start's storing at 0 and then when i have lett[1] it stores the '\0' so it only stores one character correct?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 246
Reputation: lotrsimp12345 is an unknown quantity at this point 
Solved Threads: 0
lotrsimp12345 lotrsimp12345 is online now Online
Posting Whiz in Training

Re: need some help with C string

 
0
  #10
Jun 29th, 2009
it compiled.
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



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC