Getting “Segmentation fault (core dumped)” after prompting user for input
Switch statement results in "Segmentation fault (core dumped)" regardless of the case.
I tried changing the data type of "command" but can't get any other result.
char command;
int temp;
while(1) {
printf("Enter command ('d'/'m'/'s'/'r'): ");
scanf("%c", command);
printf("n");
switch(command) {
case 'd' :
printf("display which employee (0-19)?n");
scanf("%i", temp);
//display(temp);
printf("displayed");
break;
case 'm' :
printf("modify which employee (0-19)?n");
scanf("%i", temp);
//modify(temp);
printf("modified");
break;
case 's' :
//save();
printf("saved");
break;
case 'r' :
//retrieve();
printf("retrieved");
break;
default :
printf("Command not recognizedn");
}
}
Expected to print the action according to the relevant case. Instead it just prints the "Segmentation fault (core dumped)" message regardless.
c
add a comment |
Switch statement results in "Segmentation fault (core dumped)" regardless of the case.
I tried changing the data type of "command" but can't get any other result.
char command;
int temp;
while(1) {
printf("Enter command ('d'/'m'/'s'/'r'): ");
scanf("%c", command);
printf("n");
switch(command) {
case 'd' :
printf("display which employee (0-19)?n");
scanf("%i", temp);
//display(temp);
printf("displayed");
break;
case 'm' :
printf("modify which employee (0-19)?n");
scanf("%i", temp);
//modify(temp);
printf("modified");
break;
case 's' :
//save();
printf("saved");
break;
case 'r' :
//retrieve();
printf("retrieved");
break;
default :
printf("Command not recognizedn");
}
}
Expected to print the action according to the relevant case. Instead it just prints the "Segmentation fault (core dumped)" message regardless.
c
3
scanf("%i", temp);
-->scanf("%i", &temp);
Sincetemp
is anint
variable, you need to provide&
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.
– Achal
Dec 27 '18 at 20:18
2
also add a space before % and an ampersand before commandscanf(" %c", &command);
– xing
Dec 27 '18 at 20:19
add a comment |
Switch statement results in "Segmentation fault (core dumped)" regardless of the case.
I tried changing the data type of "command" but can't get any other result.
char command;
int temp;
while(1) {
printf("Enter command ('d'/'m'/'s'/'r'): ");
scanf("%c", command);
printf("n");
switch(command) {
case 'd' :
printf("display which employee (0-19)?n");
scanf("%i", temp);
//display(temp);
printf("displayed");
break;
case 'm' :
printf("modify which employee (0-19)?n");
scanf("%i", temp);
//modify(temp);
printf("modified");
break;
case 's' :
//save();
printf("saved");
break;
case 'r' :
//retrieve();
printf("retrieved");
break;
default :
printf("Command not recognizedn");
}
}
Expected to print the action according to the relevant case. Instead it just prints the "Segmentation fault (core dumped)" message regardless.
c
Switch statement results in "Segmentation fault (core dumped)" regardless of the case.
I tried changing the data type of "command" but can't get any other result.
char command;
int temp;
while(1) {
printf("Enter command ('d'/'m'/'s'/'r'): ");
scanf("%c", command);
printf("n");
switch(command) {
case 'd' :
printf("display which employee (0-19)?n");
scanf("%i", temp);
//display(temp);
printf("displayed");
break;
case 'm' :
printf("modify which employee (0-19)?n");
scanf("%i", temp);
//modify(temp);
printf("modified");
break;
case 's' :
//save();
printf("saved");
break;
case 'r' :
//retrieve();
printf("retrieved");
break;
default :
printf("Command not recognizedn");
}
}
Expected to print the action according to the relevant case. Instead it just prints the "Segmentation fault (core dumped)" message regardless.
c
c
asked Dec 27 '18 at 20:17
user2324350
83
83
3
scanf("%i", temp);
-->scanf("%i", &temp);
Sincetemp
is anint
variable, you need to provide&
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.
– Achal
Dec 27 '18 at 20:18
2
also add a space before % and an ampersand before commandscanf(" %c", &command);
– xing
Dec 27 '18 at 20:19
add a comment |
3
scanf("%i", temp);
-->scanf("%i", &temp);
Sincetemp
is anint
variable, you need to provide&
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.
– Achal
Dec 27 '18 at 20:18
2
also add a space before % and an ampersand before commandscanf(" %c", &command);
– xing
Dec 27 '18 at 20:19
3
3
scanf("%i", temp);
--> scanf("%i", &temp);
Since temp
is an int
variable, you need to provide &
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.– Achal
Dec 27 '18 at 20:18
scanf("%i", temp);
--> scanf("%i", &temp);
Since temp
is an int
variable, you need to provide &
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.– Achal
Dec 27 '18 at 20:18
2
2
also add a space before % and an ampersand before command
scanf(" %c", &command);
Dec 27 '18 at 20:19
also add a space before % and an ampersand before command
scanf(" %c", &command);
Dec 27 '18 at 20:19
add a comment |
1 Answer
1
active
oldest
votes
The %c
format specifier expects the address of a char
, i.e. a char *
to be passed in. You're passing in a char
instead. The same goes for %i
and int
further down. Using the wrong format specifier invokes undefined behaivor, which is in this case manifests as a crash.
You need to pass the address of the variables in question so that scanf
can modify them. Also, for %c
you should have a space in the format string before it to consume any whitespace left in the input buffer.
So you want:
scanf(" %c", &command);
And:
scanf("%i", &temp);
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%2f53950409%2fgetting-segmentation-fault-core-dumped-after-prompting-user-for-input%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The %c
format specifier expects the address of a char
, i.e. a char *
to be passed in. You're passing in a char
instead. The same goes for %i
and int
further down. Using the wrong format specifier invokes undefined behaivor, which is in this case manifests as a crash.
You need to pass the address of the variables in question so that scanf
can modify them. Also, for %c
you should have a space in the format string before it to consume any whitespace left in the input buffer.
So you want:
scanf(" %c", &command);
And:
scanf("%i", &temp);
add a comment |
The %c
format specifier expects the address of a char
, i.e. a char *
to be passed in. You're passing in a char
instead. The same goes for %i
and int
further down. Using the wrong format specifier invokes undefined behaivor, which is in this case manifests as a crash.
You need to pass the address of the variables in question so that scanf
can modify them. Also, for %c
you should have a space in the format string before it to consume any whitespace left in the input buffer.
So you want:
scanf(" %c", &command);
And:
scanf("%i", &temp);
add a comment |
The %c
format specifier expects the address of a char
, i.e. a char *
to be passed in. You're passing in a char
instead. The same goes for %i
and int
further down. Using the wrong format specifier invokes undefined behaivor, which is in this case manifests as a crash.
You need to pass the address of the variables in question so that scanf
can modify them. Also, for %c
you should have a space in the format string before it to consume any whitespace left in the input buffer.
So you want:
scanf(" %c", &command);
And:
scanf("%i", &temp);
The %c
format specifier expects the address of a char
, i.e. a char *
to be passed in. You're passing in a char
instead. The same goes for %i
and int
further down. Using the wrong format specifier invokes undefined behaivor, which is in this case manifests as a crash.
You need to pass the address of the variables in question so that scanf
can modify them. Also, for %c
you should have a space in the format string before it to consume any whitespace left in the input buffer.
So you want:
scanf(" %c", &command);
And:
scanf("%i", &temp);
answered Dec 27 '18 at 20:20
dbush
93.1k12101134
93.1k12101134
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53950409%2fgetting-segmentation-fault-core-dumped-after-prompting-user-for-input%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
3
scanf("%i", temp);
-->scanf("%i", &temp);
Sincetemp
is anint
variable, you need to provide&
to store user input into it. Also read compiler warning carefully & solve them, don't ignore them.– Achal
Dec 27 '18 at 20:18
2
also add a space before % and an ampersand before command
scanf(" %c", &command);
– xing
Dec 27 '18 at 20:19