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

#include <iostream>
#include <cstdlib>
using namespace std;
#include "strextra.h"

int main()
{
    search a;
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number];
    for(int i=0; i<number; i++)
    {
        cin>>let;
        mycstring[i]=let;
    }
     mycstring[number]='\0';
     cout<<"my string is\n"<<mycstring<<"\n";
    cout<<"enter the character you are trying to find in the string above\n";
    char alpha;
    
    char lett[1];
    for(int a=0; a<1; a++)
    {
        cin>>alpha;
        lett[a]=alpha;
    }
    lett[1]='\0';
    a.find(mycstring, lett);
    
}

//interface

#ifndef STREXTRA_H_INCLUDED
#define STREXTRA_H_INCLUDED
#include <iostream>

using namespace std;

class search
{
    public:
    int find(char mystring[],char letter[]);
    int find(char mystring,string word);
    
};

#endif // STREXTRA_H_INCLUDED

//implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(char mystring[],char letter[])
{
    return strcspm(mystring[],letter[]);
}

Recommended Answers

All 38 Replies

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.

thanks for the help now it says undefined reference to search::find(char*,char*). does that mean my return statement doesn't match up?

i am guessing the problem is that the parameters and arguments are matching up.

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!

i haven't learnt about pointers yet. That's the next section. He says to use c-string and overloading

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

g++ main.cpp header.cpp -o out

yeap i am compiling all of my files.

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?

A standard ASCIIz C string buffer requires a space to store the terminator.

[0] [ ] [ ] [ ] [ ]


3 = strlen( "APE" );
But actually occupies 4 bytes!

nvm. It gives me some conversion error.

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!

that's what i have the +1 for.

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!

most recent code

//main

#include <iostream>
#include <cstdlib>
using namespace std;
#include "strextra.h"

int main()
{
    Search a;
    cout<<"enter max number of characters in the sentence";
    int number;
    cin>>number;
    cout<<"enter a sentence";
    char let;
    char mycstring[number+1];
    for(int i=0; i<number; i++)
    {
        cin>>let;
        mycstring[i]=let;
    }
     mycstring[number+1]='\0';
     cout<<"my string is\n"<<mycstring<<"\n";
    cout<<"enter the character you are trying to find in the string above\n";
    char alpha;
    
    char lett[2];
    for(int a=0; a<1; a++)
    {
        cin>>alpha;
        lett[a]=alpha;
    }
    lett[1]='\0';
    cout<<lett[0]<<lett[1];
    //a.find(mycstring, lett);
    
}

//interface

#ifndef STREXTRA_H_INCLUDED
#define STREXTRA_H_INCLUDED
#include <iostream>

using namespace std;

class Search
{
    public:
    int find(char mystring,char letter);
    int find(char mystring,string word);
    
};

//implementation

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
#include "strextra.h"

int search::find(char mystring,char letter)
{
    return strchr(mystring,letter);
}

#endif // STREXTRA_H_INCLUDED

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.

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.

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]

If you are passing two strings

Incorrect:

int search::find(char mystring,char letter)

Correct:

int search::find(char * mystring, char * letter)

a.find( "Apple Jacks", "Jack" );

String and a single character

int search::find( char * mystring, char letter)

a.find( "Apple Jacks", 'a' );

actually i don't think i need that +1 because it starts counting at 0.
So if i entered in "ab" mycstring[0]=a mycstring[1]=b mycstring[2]='\0'.

Let's scale this down.

#define MY_BUF_SIZE (3 +1)

char MyBuf[ MY_BUF_SIZE ];
MyBuf[0] = 'a';
MyBuf[1] = 'b';
MyBuf[2] = 'c';
MyBuf[3] = 0;
4 bytes of storage, not 3!!! You need room for the terminator.
Since MY_BUF is 4, you really need a
MyBuf[ MY_BUF_SIZE -1 ] = 0;

You NEVER want to overrun the end of your buffer!

Writing into ...
MyBuf[ MY_BUF_SIZE ]
...is actually writing into the byte past the end of the buffer. In essence writing into the fifth slot because as an index it is index #4.

oh k now it makes sense. And with the methods a.find i can't pass in a variable?

You can but your string addressing was indicating a single character.
to pass a string you need a pointer.

char *pStr
And since I think you indicated you haven't covered pointers yet, then you can use the alternate pointer
char pStr[]

i am really confused now.

Not to confuse you but if you instructor actually teaches you,

it'll be const char *
as the string you are comparing to is not to be changed therefore it has a (read-only type) const prefix.

see i have a teacher but no lecture so basically i have to teach myself.

Sorry simultaneous postings.

A pointer is char *p;

The asterisk means a pointer of the type.
A char ary[] means an array of the type.

They are technically both pointers, though the normal method is to use the true pointer char * ptr;

Pass your ary[] like before. It's an empty array therefore it indicates a pointer to an array, not pushing the entire array on the stack!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.