strcmp() not returning what it should return












-3















Basically I want to create a program that would have potential questions that might be in my upcoming exam in Digital Systems.



#include <stdio.h>
#include <string.h>
int main() {
char input1[600];
printf("What is the set of available registers?");
scanf("%s", &input1);

if(strcmp(input1, "registers, memory, hard disc") == 0){
printf("Good job! You got it right");
}
else {
printf("Wrong answer!");
}


So whenever I type "registers, memory, hard disc" when I'm asked it returns 1 instead of 0. I cannot see the problem. I am kind of new to C so sorry if it is a silly question.










share|improve this question

























  • use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

    – xing
    Nov 25 '18 at 17:41






  • 3





    In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

    – Broman
    Nov 25 '18 at 17:42








  • 1





    scanf famously (yes; it is by design) separates input on spaces.

    – usr2564301
    Nov 25 '18 at 17:43






  • 5





    "Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

    – John Bollinger
    Nov 25 '18 at 17:46






  • 1





    ericlippert.com/2014/03/05/how-to-debug-small-programs

    – Broman
    Nov 25 '18 at 17:51
















-3















Basically I want to create a program that would have potential questions that might be in my upcoming exam in Digital Systems.



#include <stdio.h>
#include <string.h>
int main() {
char input1[600];
printf("What is the set of available registers?");
scanf("%s", &input1);

if(strcmp(input1, "registers, memory, hard disc") == 0){
printf("Good job! You got it right");
}
else {
printf("Wrong answer!");
}


So whenever I type "registers, memory, hard disc" when I'm asked it returns 1 instead of 0. I cannot see the problem. I am kind of new to C so sorry if it is a silly question.










share|improve this question

























  • use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

    – xing
    Nov 25 '18 at 17:41






  • 3





    In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

    – Broman
    Nov 25 '18 at 17:42








  • 1





    scanf famously (yes; it is by design) separates input on spaces.

    – usr2564301
    Nov 25 '18 at 17:43






  • 5





    "Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

    – John Bollinger
    Nov 25 '18 at 17:46






  • 1





    ericlippert.com/2014/03/05/how-to-debug-small-programs

    – Broman
    Nov 25 '18 at 17:51














-3












-3








-3








Basically I want to create a program that would have potential questions that might be in my upcoming exam in Digital Systems.



#include <stdio.h>
#include <string.h>
int main() {
char input1[600];
printf("What is the set of available registers?");
scanf("%s", &input1);

if(strcmp(input1, "registers, memory, hard disc") == 0){
printf("Good job! You got it right");
}
else {
printf("Wrong answer!");
}


So whenever I type "registers, memory, hard disc" when I'm asked it returns 1 instead of 0. I cannot see the problem. I am kind of new to C so sorry if it is a silly question.










share|improve this question
















Basically I want to create a program that would have potential questions that might be in my upcoming exam in Digital Systems.



#include <stdio.h>
#include <string.h>
int main() {
char input1[600];
printf("What is the set of available registers?");
scanf("%s", &input1);

if(strcmp(input1, "registers, memory, hard disc") == 0){
printf("Good job! You got it right");
}
else {
printf("Wrong answer!");
}


So whenever I type "registers, memory, hard disc" when I'm asked it returns 1 instead of 0. I cannot see the problem. I am kind of new to C so sorry if it is a silly question.







c string string-comparison strcmp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 18:29









alk

59.1k765179




59.1k765179










asked Nov 25 '18 at 17:39









dangerardangerar

112




112













  • use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

    – xing
    Nov 25 '18 at 17:41






  • 3





    In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

    – Broman
    Nov 25 '18 at 17:42








  • 1





    scanf famously (yes; it is by design) separates input on spaces.

    – usr2564301
    Nov 25 '18 at 17:43






  • 5





    "Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

    – John Bollinger
    Nov 25 '18 at 17:46






  • 1





    ericlippert.com/2014/03/05/how-to-debug-small-programs

    – Broman
    Nov 25 '18 at 17:51



















  • use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

    – xing
    Nov 25 '18 at 17:41






  • 3





    In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

    – Broman
    Nov 25 '18 at 17:42








  • 1





    scanf famously (yes; it is by design) separates input on spaces.

    – usr2564301
    Nov 25 '18 at 17:43






  • 5





    "Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

    – John Bollinger
    Nov 25 '18 at 17:46






  • 1





    ericlippert.com/2014/03/05/how-to-debug-small-programs

    – Broman
    Nov 25 '18 at 17:51

















use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

– xing
Nov 25 '18 at 17:41





use fgets to read a line that contains spaces. strcspn can be used to remove the trailing newline.

– xing
Nov 25 '18 at 17:41




3




3





In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

– Broman
Nov 25 '18 at 17:42







In a situation like this, you should print the string that you have read to see what it contains. That's basic debugging.

– Broman
Nov 25 '18 at 17:42






1




1





scanf famously (yes; it is by design) separates input on spaces.

– usr2564301
Nov 25 '18 at 17:43





scanf famously (yes; it is by design) separates input on spaces.

– usr2564301
Nov 25 '18 at 17:43




5




5





"Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

– John Bollinger
Nov 25 '18 at 17:46





"Standard library function X does not behave according to its documentation" is almost always a misdiagnosis. It is far more likely that the inputs are not what you think they are (as here), or even that you've misunderstood the docs.

– John Bollinger
Nov 25 '18 at 17:46




1




1





ericlippert.com/2014/03/05/how-to-debug-small-programs

– Broman
Nov 25 '18 at 17:51





ericlippert.com/2014/03/05/how-to-debug-small-programs

– Broman
Nov 25 '18 at 17:51












3 Answers
3






active

oldest

votes


















2














As already said in the comments, scanf() with "%s" stops conversion at the first whitespace character. To read a whole line of text, use fgets():



#include <stddef.h>
#include <stdio.h>
#include <string.h>

// ...

char foo[100];
if(!fgets(foo, sizeof(foo), stdin)) // don't use sizeof on pointers
; // handle error // for that purpose!

size_t length = strlen(foo);
if(length && foo[length - 1] == 'n') // fgets also reads the newline character
foo[--length] = ''; // at the end of the line, this removes it.





share|improve this answer


























  • Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

    – chux
    Nov 25 '18 at 20:52











  • Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

    – Broman
    Nov 25 '18 at 22:36



















1














Swordfish has already given a good answer, and fgets is preferable to scanf. However, I want to show how you would use scanf in this case:



if(scanf("%599[^n]", input1) != 1) {
// Handle error
}


So what is different?





  1. scanf returns the number of successfully assignments, so if this returns 1, then input1 has been assigned. If not, an error has occured.

  2. Changed s to [^n] to read until newline

  3. Inserted 599 (one less than 600) so ensure that we don't write outside the array.

  4. Removed & from input1. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.






share|improve this answer

































    -2














    Try change your scanf line from:



    scanf("%s", &input1);


    to:



    scanf("%[^n]", input1);



    It worked out for me.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470139%2fstrcmp-not-returning-what-it-should-return%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      As already said in the comments, scanf() with "%s" stops conversion at the first whitespace character. To read a whole line of text, use fgets():



      #include <stddef.h>
      #include <stdio.h>
      #include <string.h>

      // ...

      char foo[100];
      if(!fgets(foo, sizeof(foo), stdin)) // don't use sizeof on pointers
      ; // handle error // for that purpose!

      size_t length = strlen(foo);
      if(length && foo[length - 1] == 'n') // fgets also reads the newline character
      foo[--length] = ''; // at the end of the line, this removes it.





      share|improve this answer


























      • Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

        – chux
        Nov 25 '18 at 20:52











      • Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

        – Broman
        Nov 25 '18 at 22:36
















      2














      As already said in the comments, scanf() with "%s" stops conversion at the first whitespace character. To read a whole line of text, use fgets():



      #include <stddef.h>
      #include <stdio.h>
      #include <string.h>

      // ...

      char foo[100];
      if(!fgets(foo, sizeof(foo), stdin)) // don't use sizeof on pointers
      ; // handle error // for that purpose!

      size_t length = strlen(foo);
      if(length && foo[length - 1] == 'n') // fgets also reads the newline character
      foo[--length] = ''; // at the end of the line, this removes it.





      share|improve this answer


























      • Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

        – chux
        Nov 25 '18 at 20:52











      • Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

        – Broman
        Nov 25 '18 at 22:36














      2












      2








      2







      As already said in the comments, scanf() with "%s" stops conversion at the first whitespace character. To read a whole line of text, use fgets():



      #include <stddef.h>
      #include <stdio.h>
      #include <string.h>

      // ...

      char foo[100];
      if(!fgets(foo, sizeof(foo), stdin)) // don't use sizeof on pointers
      ; // handle error // for that purpose!

      size_t length = strlen(foo);
      if(length && foo[length - 1] == 'n') // fgets also reads the newline character
      foo[--length] = ''; // at the end of the line, this removes it.





      share|improve this answer















      As already said in the comments, scanf() with "%s" stops conversion at the first whitespace character. To read a whole line of text, use fgets():



      #include <stddef.h>
      #include <stdio.h>
      #include <string.h>

      // ...

      char foo[100];
      if(!fgets(foo, sizeof(foo), stdin)) // don't use sizeof on pointers
      ; // handle error // for that purpose!

      size_t length = strlen(foo);
      if(length && foo[length - 1] == 'n') // fgets also reads the newline character
      foo[--length] = ''; // at the end of the line, this removes it.






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 25 '18 at 21:11

























      answered Nov 25 '18 at 17:53









      SwordfishSwordfish

      1




      1













      • Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

        – chux
        Nov 25 '18 at 20:52











      • Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

        – Broman
        Nov 25 '18 at 22:36



















      • Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

        – chux
        Nov 25 '18 at 20:52











      • Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

        – Broman
        Nov 25 '18 at 22:36

















      Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

      – chux
      Nov 25 '18 at 20:52





      Good use of fgets() and good test to prevent hacker exploit of entering a null character as the first character.

      – chux
      Nov 25 '18 at 20:52













      Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

      – Broman
      Nov 25 '18 at 22:36





      Nice one. fgets is of course preferable, but I added an answer on how to do with scanf

      – Broman
      Nov 25 '18 at 22:36













      1














      Swordfish has already given a good answer, and fgets is preferable to scanf. However, I want to show how you would use scanf in this case:



      if(scanf("%599[^n]", input1) != 1) {
      // Handle error
      }


      So what is different?





      1. scanf returns the number of successfully assignments, so if this returns 1, then input1 has been assigned. If not, an error has occured.

      2. Changed s to [^n] to read until newline

      3. Inserted 599 (one less than 600) so ensure that we don't write outside the array.

      4. Removed & from input1. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.






      share|improve this answer






























        1














        Swordfish has already given a good answer, and fgets is preferable to scanf. However, I want to show how you would use scanf in this case:



        if(scanf("%599[^n]", input1) != 1) {
        // Handle error
        }


        So what is different?





        1. scanf returns the number of successfully assignments, so if this returns 1, then input1 has been assigned. If not, an error has occured.

        2. Changed s to [^n] to read until newline

        3. Inserted 599 (one less than 600) so ensure that we don't write outside the array.

        4. Removed & from input1. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.






        share|improve this answer




























          1












          1








          1







          Swordfish has already given a good answer, and fgets is preferable to scanf. However, I want to show how you would use scanf in this case:



          if(scanf("%599[^n]", input1) != 1) {
          // Handle error
          }


          So what is different?





          1. scanf returns the number of successfully assignments, so if this returns 1, then input1 has been assigned. If not, an error has occured.

          2. Changed s to [^n] to read until newline

          3. Inserted 599 (one less than 600) so ensure that we don't write outside the array.

          4. Removed & from input1. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.






          share|improve this answer















          Swordfish has already given a good answer, and fgets is preferable to scanf. However, I want to show how you would use scanf in this case:



          if(scanf("%599[^n]", input1) != 1) {
          // Handle error
          }


          So what is different?





          1. scanf returns the number of successfully assignments, so if this returns 1, then input1 has been assigned. If not, an error has occured.

          2. Changed s to [^n] to read until newline

          3. Inserted 599 (one less than 600) so ensure that we don't write outside the array.

          4. Removed & from input1. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 25 '18 at 23:09

























          answered Nov 25 '18 at 22:14









          BromanBroman

          7,249112644




          7,249112644























              -2














              Try change your scanf line from:



              scanf("%s", &input1);


              to:



              scanf("%[^n]", input1);



              It worked out for me.






              share|improve this answer






























                -2














                Try change your scanf line from:



                scanf("%s", &input1);


                to:



                scanf("%[^n]", input1);



                It worked out for me.






                share|improve this answer




























                  -2












                  -2








                  -2







                  Try change your scanf line from:



                  scanf("%s", &input1);


                  to:



                  scanf("%[^n]", input1);



                  It worked out for me.






                  share|improve this answer















                  Try change your scanf line from:



                  scanf("%s", &input1);


                  to:



                  scanf("%[^n]", input1);



                  It worked out for me.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 25 '18 at 23:03

























                  answered Nov 25 '18 at 17:57









                  Anderson OliveiraAnderson Oliveira

                  876




                  876






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470139%2fstrcmp-not-returning-what-it-should-return%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Wiesbaden

                      Marschland

                      Dieringhausen