Input reading using scanf hangs
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main()
int num;
printf("enter a number: ");
scanf("%in", &num);
for(int n = 1; n < num + 1; n++)
printf("%in", n);
return 0;
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
c scanf
add a comment |
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main()
int num;
printf("enter a number: ");
scanf("%in", &num);
for(int n = 1; n < num + 1; n++)
printf("%in", n);
return 0;
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
c scanf
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11
add a comment |
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main()
int num;
printf("enter a number: ");
scanf("%in", &num);
for(int n = 1; n < num + 1; n++)
printf("%in", n);
return 0;
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
c scanf
I am programming in C and i have a problem when i run a program in the cmd terminal. here is the code i use:
#include <stdio.h>
int main()
int num;
printf("enter a number: ");
scanf("%in", &num);
for(int n = 1; n < num + 1; n++)
printf("%in", n);
return 0;
Generally, everything works like it should, exept for one thing.
when I enter a number, nothing happens. there is no output, until I write anything and press Enter, and only then the number appear.
this is a screenshot of what it looks like.
here is enter the number (and press enter) but nothing happens: http://prntscr.com/deum9a
and this is how it looks like after i entered something random nad all the numbers popped up: http://prntscr.com/deumyn
if anyone knows how to fix this, please tell me (:
c scanf
c scanf
edited Nov 13 '18 at 23:08
melpomene
60.2k54693
60.2k54693
asked Dec 3 '16 at 14:07
pRog R. HammerpRog R. Hammer
91
91
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11
add a comment |
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11
add a comment |
1 Answer
1
active
oldest
votes
Remove the n
from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf()
will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf()
call.
Generally, scanf()
is considered bad for input reading. So, considering using fgets()
and parsing the input using sscanf()
.
See: Why does everyone say not to use scanf? What should I use instead?
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
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%2f40948635%2finput-reading-using-scanf-hangs%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
Remove the n
from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf()
will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf()
call.
Generally, scanf()
is considered bad for input reading. So, considering using fgets()
and parsing the input using sscanf()
.
See: Why does everyone say not to use scanf? What should I use instead?
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
add a comment |
Remove the n
from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf()
will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf()
call.
Generally, scanf()
is considered bad for input reading. So, considering using fgets()
and parsing the input using sscanf()
.
See: Why does everyone say not to use scanf? What should I use instead?
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
add a comment |
Remove the n
from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf()
will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf()
call.
Generally, scanf()
is considered bad for input reading. So, considering using fgets()
and parsing the input using sscanf()
.
See: Why does everyone say not to use scanf? What should I use instead?
Remove the n
from scanf()
scanf("%i", &num);
When you have a whitespace character in the format string, scanf()
will ignore any number of whtiespaces you input and thus the ENTER you do doesn't terminate the input reading. Basically, you'll be forced to input a non whitespace character again in order complete the scanf()
call.
Generally, scanf()
is considered bad for input reading. So, considering using fgets()
and parsing the input using sscanf()
.
See: Why does everyone say not to use scanf? What should I use instead?
answered Dec 3 '16 at 14:10
P.P.P.P.
75.1k11105155
75.1k11105155
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
add a comment |
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
thanks is worked! and ill look for some other input methods (:
– pRog R. Hammer
Dec 3 '16 at 14:38
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%2f40948635%2finput-reading-using-scanf-hangs%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
The scanf will be in the execution until enter key is pressed. Are you pressing the enter key right after you enter a number? If yes, what does console show? If no, then it works that way.
– Rehban Khatri
Dec 3 '16 at 14:11