Link to home
Start Free TrialLog in
Avatar of Chris2010
Chris2010

asked on

String compare error handling

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;

     inData.open("c:quiz.dat");

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

     inData >> key;

    while(!inData.eof())
    {
    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)
     {
          correct=strlen(answer);
          cout << firstname << lastname << correct << wrong <<"\n";
     }
     else
     {
// Not sure what to do here!!
          cout << firstname << lastname << correct << wrong <<"\n";
     }
}
return 0;    
}


//*********   DATA FILE   *******
TFTFTFTFTTTFFFT
TFTFTFTFTTTFFFT Jim John
TFTFTFTFTTTFFFF Jerry Sanchez
 
ASKER CERTIFIED SOLUTION
Avatar of Salte
Salte

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Chris2010
Chris2010

ASKER

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.