strcmp() not returning what it should return
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
|
show 1 more comment
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
usefgets
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
|
show 1 more comment
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
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
c string string-comparison strcmp
edited Nov 25 '18 at 18:29
alk
59.1k765179
59.1k765179
asked Nov 25 '18 at 17:39
dangerardangerar
112
112
usefgets
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
|
show 1 more comment
usefgets
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.Nov 25 '18 at 17:41
use
fgets
to read a line that contains spaces. strcspn
can be used to remove the trailing newline.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
|
show 1 more comment
3 Answers
3
active
oldest
votes
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.
Good use offgets()
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 withscanf
– Broman
Nov 25 '18 at 22:36
add a comment |
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?
scanf
returns the number of successfully assignments, so if this returns 1, theninput1
has been assigned. If not, an error has occured.- Changed
s
to[^n]
to read until newline - Inserted
599
(one less than 600) so ensure that we don't write outside the array. - Removed
&
frominput1
. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.
add a comment |
Try change your scanf line from:
scanf("%s", &input1);
to:
scanf("%[^n]", input1);
It worked out for me.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Good use offgets()
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 withscanf
– Broman
Nov 25 '18 at 22:36
add a comment |
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.
Good use offgets()
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 withscanf
– Broman
Nov 25 '18 at 22:36
add a comment |
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.
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.
edited Nov 25 '18 at 21:11
answered Nov 25 '18 at 17:53
SwordfishSwordfish
1
1
Good use offgets()
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 withscanf
– Broman
Nov 25 '18 at 22:36
add a comment |
Good use offgets()
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 withscanf
– 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
add a comment |
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?
scanf
returns the number of successfully assignments, so if this returns 1, theninput1
has been assigned. If not, an error has occured.- Changed
s
to[^n]
to read until newline - Inserted
599
(one less than 600) so ensure that we don't write outside the array. - Removed
&
frominput1
. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.
add a comment |
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?
scanf
returns the number of successfully assignments, so if this returns 1, theninput1
has been assigned. If not, an error has occured.- Changed
s
to[^n]
to read until newline - Inserted
599
(one less than 600) so ensure that we don't write outside the array. - Removed
&
frominput1
. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.
add a comment |
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?
scanf
returns the number of successfully assignments, so if this returns 1, theninput1
has been assigned. If not, an error has occured.- Changed
s
to[^n]
to read until newline - Inserted
599
(one less than 600) so ensure that we don't write outside the array. - Removed
&
frominput1
. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.
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?
scanf
returns the number of successfully assignments, so if this returns 1, theninput1
has been assigned. If not, an error has occured.- Changed
s
to[^n]
to read until newline - Inserted
599
(one less than 600) so ensure that we don't write outside the array. - Removed
&
frominput1
. In this case, it would probably work anyway, but it is undefined behavior, which should be avoided at all costs.
edited Nov 25 '18 at 23:09
answered Nov 25 '18 at 22:14
BromanBroman
7,249112644
7,249112644
add a comment |
add a comment |
Try change your scanf line from:
scanf("%s", &input1);
to:
scanf("%[^n]", input1);
It worked out for me.
add a comment |
Try change your scanf line from:
scanf("%s", &input1);
to:
scanf("%[^n]", input1);
It worked out for me.
add a comment |
Try change your scanf line from:
scanf("%s", &input1);
to:
scanf("%[^n]", input1);
It worked out for me.
Try change your scanf line from:
scanf("%s", &input1);
to:
scanf("%[^n]", input1);
It worked out for me.
edited Nov 25 '18 at 23:03
answered Nov 25 '18 at 17:57
Anderson OliveiraAnderson Oliveira
876
876
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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