0

This is a group assignment and it's become rather difficult to the point our professor has extended the project by 1 week. there are 50 stages/tests, we've only been able to reach up to stage 11 and then the function fails.

this function is in our .cpp file (we're positive it's this function causing the problem's because when we change parts of it, it affects stage 11 which we've passed).

int segment::match(const char word[]) {
    int i;
    cout << data[0];
    data[0] == "OOP";
    cout << data[0];
    for(i=0;i<NUM_MAX;i++) {
        cout << "word = " << &word[i] << " data[i] = " << data[i];
        if(strstr(&word[i],data[i])!= NULL)
        break;
    }
       return i==NUM_MAX ? 1 : i-1;

and from the main.cpp (provided to us as the assignment) this is the what we are trying to accomplish

Passed test 11...

Your match( ) return value ----> -1
Actual match( ) return value --> -1 
Press the ENTER key to continue... 
word = OOP data[i] = 

Failed while testing the match( ) 
function... Failed on test 12... 
Your match( ) return value ----> -1 
Actual match( ) return value --> 1 
Press the ENTER key to continue...

You passed 11/50 tests... 
Your program is 22.00% complete! 
Your program still needs some work! 
Keep at it!

what the function is suppose to do is check for "oop" and if it isn't there it exits with -1, and if it is there it should return true with 1.

I guess what I'm asking is how do I make that function that it returns both -1 and 1 in the proper order?

If you would like access to the main.cpp and segement.cpp i can upload that as files somewhere because they're very long and I did not want to cram the post.

Any help is appreciated, thank you.

EDIT* Here is the full code that we have http://jsfiddle.net/h5aKN/

The "html" section has the segement.cpp which is what we built. and the jscript section has the a2main.cpp which is what our professor built.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
SorryEh
  • 900
  • 2
  • 15
  • 47
  • 7
    `data[0] == "OOP";` That smells. – chris Jun 16 '12 at 17:57
  • Im still trying to better understand the problem, because this smells a lot like a simple string matching problem!!! – trumpetlicks Jun 16 '12 at 18:00
  • 2
    `if(strstr(&word[i],data[i])!= NULL)` probably isn't the right thing to do. – dirkgently Jun 16 '12 at 18:01
  • Do you want to detect "oop" anywhere in the string, or only at the beginning? – Beta Jun 16 '12 at 18:12
  • Missing lots of information about types. C++ is about the types. Show us the types. – Martin York Jun 16 '12 at 18:20
  • lol that smells, well each section is separated by a "," and it's calculating the binary into ascii and from there into the letter (we have that part down) and what we get is OOP from the test however the main I don't see oop, I only see OP1440'/0'....and according to the requirement's we are not allowed to change that. And since a few people have finished the assignment we figured it's what it should be. ill just show you the main so you understand what i'm doing, fiddle seems like a fine place to put it so you don't have to download anything http://jsfiddle.net/xpTTJ/ – SorryEh Jun 16 '12 at 18:20

2 Answers2

1

data[0] == "OOP"; is probably not what you want to do. The double = (==) tests for equality, so here you're testing if the item at the first index of data (data[0]) and the character string "OOP" are equal.

In the running of the test, you are cout'ing: word = OOP data[i] =, which means that word[i] is probably defined correctly, but data[i] is not. This goes back to the usage of the equivalence test above.

If you set initialize data correctly, (correctly meaning allocating the memory correctly, I don't know where data is instantiated), then the test would likely return -1, as it would get a non NULL pointer from the strstr() call (assuming that data is the correct type), i would be 0 on breaking, and the ternary operator would yield i-1, = -1.

So fix the initialization / assignment of the data variable

And if you're not limited to c style strings (char arrays), I'd use the std::string type and its associated methods (see the c++ string reference if you haven't already). Usually much nicer to work with

eqzx
  • 5,323
  • 4
  • 37
  • 54
  • so I changed it to data[0] = "OOP" and i got the error segment.cpp:90: error: incompatible types in assignment of 'const char [4]' to 'char [2000]'. I'm so confused. I did afterwards change match to string and then declare it in my segment class in the .h file and that's the error I got after compiling. – SorryEh Jun 16 '12 at 19:17
  • 1
    try using C++ style strings, or look at the pages for sprintf http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/ and this SO question on array initialization http://stackoverflow.com/questions/201101/how-to-initialize-an-array-in-c – eqzx Jun 16 '12 at 23:58
  • thanks! however i did change the (strstr(&word[i], data[i]) to (strstr(data[i], word)!=NULL and now it passes stage 12...but I don't know why that would work. – SorryEh Jun 17 '12 at 02:48
0

If you are passing a list of words to the function:

As the use of (strstr(&word[i],data[i])) suggests you are looking for a string inside another string. Thus you are looping over a list of strings (words).

Then this looks wrong:

int segment::match(const char word[]) {

Here you are passing one word.
Its impossable to tell what it should be, but a guess would be:

int segment::match(const char* word[]) {
                   //      ^^^^^

But to be honest the whole thing is rather ugly C++. If you were writting C I would say fine, but if you had been writing C++ correctly the type system would have saved you from all these problems. Use std::string to represent words.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • 1
    it seems that this is an assignment, the prof is probably using C++ to teach OOP (how did I guess?), instead of best programming practices with regards to char arrays and strings in c/c++ – eqzx Jun 16 '12 at 18:26
  • @nrhine1: You can't claim to teach OOP- or C++- and then use `const char*`. – Puppy Jun 16 '12 at 18:40