String compare error handling

Posted on 2003-03-27
Medium Priority
Last Modified: 2010-04-01
I am working on a project and I have no idea how to handle a string compare result of 1. The program opens a data file and reads a quiz file. The first line is the key which is loaded into an array. The second line has 15 T/F answer followed by the students name. After reading the file, I am using a string compare to compare teh answers to the. If everything is correct it returns a value of 0.

The problem is how to handle a string the does not match and returns a value of one. When this value is returned, I want to compare the two strings element by element to find out whhcih one is wrong. ANy help would be greatly appreciated.

#include <fstream>
#include <iostream>
#include <string.h>

int main()
     using namespace std;

// Initialization
     char key[16];
     char answer[16];
     char firstname[5];
     char lastname[6];
     int result;
     ifstream inData;


     if (!inData)
               cout << "File was not found." << endl;
               return 1;

     inData >> key;

    int correct=0;
     int wrong=0;

     // Process data on file into array
     inData >> answer >> firstname >> lastname;

     // Results of processing
     result = strcmp(key,answer);
     if (result==0)
          cout << firstname << lastname << correct << wrong <<"\n";
// Not sure what to do here!!
          cout << firstname << lastname << correct << wrong <<"\n";
return 0;    

//*********   DATA FILE   *******
Question by:Chris2010
LVL 12

Accepted Solution

Salte earned 200 total points
ID: 8219708
Looks like strcmp is not the function you should use in the first place.

First off, strcmp is not the way you appear to think it is.

strcmp(a,b) compares two strings (a and b) and then return a value of 0, greater than 0 or less than 0 (not just 1 or 0):

The value is 0 if the two strings are identical.

The value is greater than 0 if the string a > b, i.e. a comes after b in a simple lexicographic ordering using the binary value of each char as the value for the compare. (It does not take locale or special locale specific collation rules into account).

The value is less than 0 if the string a < b, with a < b being the same as b > a using the description above.

However, in your case you are not interested in wether one string sort before or after another, you want to know which character is different and then return a pointer to the different value. If the two strings are equal you probably want a null pointer return or some such. So you need another function:

const char * my_special_strcmp(const char * a, const char * b);

I called it 'special_strcmp' because it really isn't comparable to strcmp in any way other than that this function will return a null pointer if and only if strcmp() return 0 value. Also, you have to make up your mind which pointer it returns if they are different, It can a pointer into the string a or b, either one but it must be the same each time.

Another way would simply be to return an integer value being the index where they differ, if they are the same you could then return either -1 or strlen(a) (0 return would mean they differ on the first).

If so the prototype would be:

int my_special_strcmp(const char * a, const char * b);

However, although the prototype is now similar to strcmp it is still very different from regular strcmp.

One way would therefore be to use a completely different name:

int find_longest_prefix(const char * a, const char * b);
return the length of the longest common prefix of a and b.

Same function but a different name. In this case you would deifnitely want equal strings to return strlen(a) (same as strlen(b)) since that would be the length of the common prefix in that case.

if I let flp(a,b) mean find_longest_prefix(a,b) (just so that I don't have to write so much here) we will have:

flp("","hello") == 0
flp("haha","hello") == 1 (only "h" is common)
flp("here","hello") == 2
flp("hello","hello") == 5
flp("he","hello") == 2
flp("hello","there") == 0

From this you see that even if return value is equal to strlen(a) it does not imply that a == b. However, if return value is equal to strlen(a) and also equal to strlen(b) then a == b.

The coding of that function is probably a big spoiler so I won't give out the details, the function is easy enough to code though and fairly similar to strcmp() in the body of the loop.


Author Comment

ID: 8220571
I found out quickly the strcmp is not the best implementation for this problem. Thanks for the help in finding a new direction. After modifying the program with your suggestion, I am able to compare the strings and get the results that I was looking for.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question