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:
const char * strstr( const char *, const char * );
const char * strchr( const char *, char );
You can find the match through indexing or pointer advance!
Try again and post your results!
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
>search is a standard name declared in the 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?
g++ main.cpp header.cpp -o out
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
A standard ASCIIz C string buffer requires a space to store the terminator.
['A'][0] [ ] [ ] [ ] [ ]
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
['A']['P']['E']['\0']
3 = strlen( "APE" );
But actually occupies 4 bytes!
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
I just reglanced at your first post and you have the same (no room for terminator) problem as well.
char mycstring[number + 1];
mycstring[number]='\0';
You need to add 1 to the buffer length to store the terminator!
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
Then you've revised your code from the first post because it doesn't show the +1. I added the +1 in my previous code snippet posting to indicate how to do it.
You should probably repost your current revised code then so everyone that is helping has a cleaner base to view!
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
You've almost have your +1 covered but I believe you have a problem.
char mycstring[number+1];
Firstly Your buffer is {0...number-1, number }
Where as number index is the terminator.
Incorrect:
mycstring[ number+1]='\0';
Correct:
mycstring[ number ]='\0';
Secondly the array index requires a constant. Not a dynamic value. That buffer is declared in the scope of the function call. that is it exists on the stack as
FUNCTION RETURN
Local Stack Arguments
Other FUNCTIONS RETURNS when called within the braces {}.
So you either have to dynamically allocte memory, or use an local buffer large enough to contain your largest string. And keep a count of how many characters you've stuffed into it.
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
Also you're still using a second loop for the single character entry.
You don't need the loop. Or is this a setup for testing your other function which finds substrings within strings?
Also you have two prototypes for find.
One is ASCIIz string and character, but you're passing ASCIIz string, ASCIIz string.
wildgoose
Practically a Posting Shark
896 posts since Jun 2009
Reputation Points: 546
Solved Threads: 99
cout<<"enter max number of characters in the sentence";
int number;
cin>>number;
cout<<"enter a sentence";
char let;
char mycstring[number+1];
This is actually compiling for me and I'm a little surprised, since I was taught to never do this. Aren't you either supposed to make number a constant or allocate the array with the new command? Why isn't this code giving me an error?
Looks like wildgoose has the same idea regarding dynamic allocation, but again, shouldn't this not compile at all?
[EDIT]
This is one fast moving thread. 20 posts in less than an hour!
[/EDIT]
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711