Hi there,

I've been learning C lately, and in one of the exercises I've encountered a bit of a frustrating issue. The exercise is to accept a string of characters that are answers to a multiple choice quiz, and then compare them to the correct answers which are in another string.

It's mostly working fine, but I'm having an issue when comparing the first element in the string. No matter what the letter is, the program always says it's the same in both strings, when it's definitely not. The strange thing is that otherwise the program works fine - it will correctly determine if an answer is correct or not for all other elements in the string. Just the first one is having the issue. My code is below:

int main()
{
    char correct[]= "ABBDCEACBEDBBCC";
    char exam[15];
    char grade[1];
    int count, score, length = (strlen(correct)+1);

    printf("Enter an answer string or q to quit\n");
    gets(exam);
    score = 0;
    while(exam[0] != 'q')
    {
        for(count = 0; count <= length; count++)
        {
            exam[count] = toupper(exam[count]);
            //printf("count is at %d\n %s", count, exam);
            if (exam[count] == correct[count]) score += 1;
            
        }
        switch(score)
        {
            [cases to determine a letter grade by checking the score]
        }
                printf("Score = %d, grade = %c", score, grade[0]);
        printf("\n\nEnter an answer string or q to quit\n");
        scanf("%s", exam);
        score = 0;
    }

    return 0;

If I put an if statement in to look for the first element, it fixes the issue:

if (count == 0 && exam[0] != 'A') score -=1;

But that's not really an elegant solution. If any one could give me a hint as to why the first element isn't being compared, that would be great.

Cheers

The problem isn't the first letter of the string
your comparing the null terminator of both strings which is equal

@line 6 remove the +1 from strlen
@line 13 change the condition to count<length (compare only the last letter value)

Hi, thanks for the help.
I've made those changes, and unfortunately I'm still having an issue. This time, it's giving me one less than it should be. So I enter the correct answers, and it gives me a score of 14, when it should be 15. By putting in some print statements, I can see that the first time it goes through the for loop, it's not incrementing the score for some reason.

It seems that , when the loop is comparing exam[0] and correct[0], it's seeing them as different, when they are in fact the same. Here's the updated for loop, with the printf statements I mentioned:

for(count = 0; count < length; count++)
        {
            exam[count] = toupper(exam[count]);
            printf("count is at %d %s -", count, exam);
            if (exam[count] == correct[count]) score += 1;
            printf(" SCORE IS %d\n", score);
        }

The output of this is in a screenshot, since I can't seem to copy from the terminal.

http://imgur.com/dwCld

Cheers for the help.

Why not add a print statement with two getchar()'s afterward (to pause the screen) the first time through the loop, and print out exam[0] and correct[0] and see what's going on?

It looks right, but sometimes the errors are just too simple to catch with a look through.

Are you compiling or running this program in Debug mode? If so, try a release mode. Sometimes debug mode gets twisted 'round.

weird I tried the whole code myself with the corrections I mentioned and got the 15 with all the correct answers...

I think you should try what Adak suggested :)

Sorry for the delay in posting again, when I originally tried to the site was being migrated and was in read only mode. It seems both of you are correct in that the issue does seem to be when I compile it. When I tried compiling it elsewhere (on a Linux VM) the code ran as expected. I'm using Code Blocks on Windows 7. When switching the 'Build target' from Debug to Release, the problem goes away. Really odd. Thanks very much to the two of you, without your help I think I would still be tearing my hair out!

Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.