Comparing each letter in two strings

Apr 25, 2013 at 11:31pm
So I'm writing a function to compare two strings of letters from a scantron. Example: aaaaaaaAbbbBaccCdaaaaaaaa would be the teachers answer key and aaaaaaaabBBBacccdabaacaaa would be a students answers. Capitals and Lower Cases are equal.

At the moment all its returning are zeros, so I'm guessing something I've done isn't allowed.

1
2
3
4
5
6
7
8
9
10
int GetGrade(string TeacherKey, string StudentKey)
{
  int Correct=0, Grade;
  for (int i=0; i<25; i++)
    if (TeacherKey[i] == StudentKey[i] || TeacherKey[i] == StudentKey[i] + 32){
      Correct++;
    }
  Grade = Correct/25*100;
  return Grade;
}
Apr 25, 2013 at 11:39pm
The type of Correct is an int, which doesn't support floating point numbers (decimals). If correct is below 25, after the division with 25 the prog truncates the numbers following the decimal, leaving you with 0.

Try changing Correct type to double.
Apr 25, 2013 at 11:41pm

1
2
3
4
5
// or...
Grade = Correct*4;

// or...
Grade = Correct*100/25;
Apr 25, 2013 at 11:52pm
That worked and got me closer, but now all the numbers are very slightly off =/. I'm guessing it has something to do with how I calculated lower cases and capitals being the same because when I tested It I used
aaaaaaaAbbbBaccCdaaaaaaaa Answer key
aaaaaaaAbbbBaccCdaaaaaaaa Copy and Pasted answer key 100% score
AAAAAAAAbbbbacccdaaaaaaaa Variations in capitals 92% score
They were both supposed to be 100%.
Apr 25, 2013 at 11:53pm
Any suggestions beyond just adding 32?
Apr 25, 2013 at 11:58pm
1
2
3
4
5
6
7
8
9
10
11
string s1 = "aaaaaaaAbbbBaccCdaaaaaaaa";
string s2 = "aaaaaaaabBBBacccdabaacaaa";

unsigned correct = 0;
for (unsigned i = 0; i < s1.length(); ++i) {
 if (tolower(s1[i]) == tolower(s2[i]))
  correct++;
}

cout << "Number of correct = " << correct << endl;
cout << "Percentage: "<< correct/s1.length() * 100 << "%" << endl;
Apr 26, 2013 at 12:03am
I forgot to do TeacherKey[i] == StudentKey[i] - 32 in my code.
Topic archived. No new replies allowed.