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.
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 ofdata(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 thatword[i]is probably defined correctly, butdata[i]is not. This goes back to the usage of the equivalence test above.If you set initialize
datacorrectly, (correctly meaning allocating the memory correctly, I don’t know wheredatais instantiated), then the test would likely return -1, as it would get a non NULL pointer from thestrstr()call (assuming thatdatais the correct type),iwould be0onbreaking, and the ternary operator would yieldi-1, =-1.So fix the initialization / assignment of the
datavariableAnd 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