We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,624 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Having trouble comparing elements of a string

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

3
Contributors
5
Replies
4 Days
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
TarkiB
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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)

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13

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.

TarkiB
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Adak
Posting Virtuoso
1,640 posts since Jun 2008
Reputation Points: 456
Solved Threads: 196
Skill Endorsements: 6

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 :)

zeroliken
Nearly a Posting Virtuoso
1,346 posts since Nov 2011
Reputation Points: 214
Solved Threads: 205
Skill Endorsements: 13

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.

TarkiB
Newbie Poster
9 posts since Mar 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by zeroliken and Adak

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0995 seconds using 2.74MB