954,168 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need some help with C string

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[]);
}
lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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. Try capitalizing your class from search to Search, or put it in a namespace.

Tom Gunn
Master Poster
733 posts since Jun 2009
Reputation Points: 1,446
Solved Threads: 135
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

>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
 

yeap i am compiling all of my files.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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?

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

it compiled.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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
 

nvm. It gives me some conversion error.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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
 

that's what i have the +1 for.

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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
 

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

lotrsimp12345
Posting Pro in Training
413 posts since Jun 2009
Reputation Points: 47
Solved Threads: 2
 

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
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You