Thread: strcmp problem, whats the problem, i cant figure it out!

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    54

    Question strcmp problem, whats the problem, i cant figure it out!

    I'm reading data from a text file, it reads it perfectly well and the data is being stored in a structure. Im using it for instance as a multiple choice test, i have on the text file the final letter of the correct answer. so i ask the user to input his answer and store it in a variable in main, the input goes perfectly well and so to compare the answer with the one from the structure i use strcmp but everytime i get the result that its the wrong answer even if i input the correct data, whats wrong, here is the piece of the code:


    Code:
    if(strcmp(answer,quest.ans)==0){
                              printf("You are correct\n\n");
                              }
                              else{
                                    printf("Your Answer Is Wrong!!\n\n");}

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Be sure there is no excess white space or hidden chars. Other then that if they are both strings and do indeed match, that code should work.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by valaris View Post
    Be sure there is no excess white space or hidden chars. Other then that if they are both strings and do indeed match, that code should work.

    I know it should work but by gosh i can seem to get what is the problem: the text file its reading info from looks like this:

    1.What is a lizard?;A.Reptile;B.Mammal;C.Bird;D.Amphibiab;A

    The A at the end is supposed to be the quest.ans to cross check the info from the scanf> the code im using is this to retrieve info from text file:

    Code:
    
    printf("\nPlease Enter Your Answer:");
    	         scanf("%s",answer);
    Last edited by AvaGodess; 10-18-2008 at 06:46 PM.

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    *answer=toupper(*answer);
    should be
    toupper(answer);

    bear in mind though your next file is not all uppercase so no it wont be the same as toupper(answer)

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by valaris View Post
    *answer=toupper(*answer);
    should be
    toupper(answer);

    bear in mind though your next file is not all uppercase so no it wont be the same as toupper(answer)

    Still even like that, even if i enter the letter manually as a cap it still doesnt work.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    How big is answer? It needs to be at least 2 bytes in order to store the null terminator in this case if you are only inputting one character.

    char answer[2];

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Probably quest.ans is "A\n", while answer is "A".

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by valaris View Post
    How big is answer? It needs to be at least 2 bytes in order to store the null terminator in this case if you are only inputting one character.

    char answer[2];
    Yup i did that, u know the funny thing is that i used strlen to determine the size of both of em and for quest.ans i got 2 and for the one in scanf answer i got 1. I believe there is a prblem, probably like tabstop said a newline, but how would i correct that?

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    Probably quest.ans is "A\n", while answer is "A".
    Hmm ya thats an interesting point, i thnk the one from the text file is including a newline, since i wanted more questions like question 2,3 ... and so on. How could i correct this though

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    ptr=strtok(NULL,";\n");
    strcpy(quest.ans,ptr);

  11. #11
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    strlen returns the strings length excluding the null terminator. Could you just post the entire code?

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by valaris View Post
    strlen returns the strings length excluding the null terminator. Could you just post the entire code?
    That would be a big help, hey could i mail it to u or something, its cuz im starting like the core of a lil test multiple choice thingy and i got three other ppl in my class. wouldnt like them to have similar codes to mine if they find it on the net which probably they would. Let me know if i can, or else ill simply have to post it

  13. #13
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    Code:
    ptr=strtok(NULL,";\n");
    strcpy(quest.ans,ptr);

    Thank im downloading a compiler here at the moment to try it, since im currently on windows and no compiler not my pc, the problems u have when u tryin to do some programming without net access is a bummer. Hopefully i hav permissions to install on this machine.

    I thnk that might be the problem though, cuz the codes everything seem fine, except for that part which seems a very reasonable problem

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by valaris View Post
    strlen returns the strings length excluding the null terminator. Could you just post the entire code?
    Hey Thank u Valaris, the problem was what tabstop said, i entered the newline character and now it runs perfectly well. Thank u very much appriciate the help.

    lots of love!

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    54
    Quote Originally Posted by tabstop View Post
    Code:
    ptr=strtok(NULL,";\n");
    strcpy(quest.ans,ptr);
    Tabstop, thanks in a million that little thingy was the problem i could never seem to find it, lol, its amazing how one little thing messes the whole code, im new to C by the way, first language its awesome though. That helped a lot, it works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. Can't figure out problem with code
    By Beast() in forum C Programming
    Replies: 4
    Last Post: 04-16-2005, 05:27 PM
  4. Multithreading problem
    By Bacardi34 in forum C Programming
    Replies: 5
    Last Post: 09-02-2004, 02:26 PM
  5. Can't figure out strcmp error
    By Fyodorox in forum C++ Programming
    Replies: 10
    Last Post: 05-08-2002, 10:18 AM