Why does this code produce 1 on adding characters in C?












-1















I was trying to restrict user input from alphabets (Eg: repeat input until correct input is provided ) , in order to get only numbers to be added. Somehow, instead of able to do so, I was able to add alphabets, but with out being '1'



It increments 1 if a number with a character is given.



#include<stdio.h>

int main() {
int x,y;
while(1)
{
printf("Enter a number > ");
if(scanf("%d%d",&x,&y) != 1){
printf("%d",x+y);
break;
}
}
return 0;
}


What could be the reason behind it?










share|improve this question




















  • 2





    Sorry...what? Can you make it more clear?

    – Sourav Ghosh
    Nov 13 '18 at 14:30











  • What input are you using exactly, what output are you getting, and what output did you expect?

    – interjay
    Nov 13 '18 at 14:31






  • 1





    restrict user input? dont use scanf then. use fgets/sscanf

    – Anders
    Nov 13 '18 at 14:33











  • @SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:34











  • @interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:37
















-1















I was trying to restrict user input from alphabets (Eg: repeat input until correct input is provided ) , in order to get only numbers to be added. Somehow, instead of able to do so, I was able to add alphabets, but with out being '1'



It increments 1 if a number with a character is given.



#include<stdio.h>

int main() {
int x,y;
while(1)
{
printf("Enter a number > ");
if(scanf("%d%d",&x,&y) != 1){
printf("%d",x+y);
break;
}
}
return 0;
}


What could be the reason behind it?










share|improve this question




















  • 2





    Sorry...what? Can you make it more clear?

    – Sourav Ghosh
    Nov 13 '18 at 14:30











  • What input are you using exactly, what output are you getting, and what output did you expect?

    – interjay
    Nov 13 '18 at 14:31






  • 1





    restrict user input? dont use scanf then. use fgets/sscanf

    – Anders
    Nov 13 '18 at 14:33











  • @SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:34











  • @interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:37














-1












-1








-1








I was trying to restrict user input from alphabets (Eg: repeat input until correct input is provided ) , in order to get only numbers to be added. Somehow, instead of able to do so, I was able to add alphabets, but with out being '1'



It increments 1 if a number with a character is given.



#include<stdio.h>

int main() {
int x,y;
while(1)
{
printf("Enter a number > ");
if(scanf("%d%d",&x,&y) != 1){
printf("%d",x+y);
break;
}
}
return 0;
}


What could be the reason behind it?










share|improve this question
















I was trying to restrict user input from alphabets (Eg: repeat input until correct input is provided ) , in order to get only numbers to be added. Somehow, instead of able to do so, I was able to add alphabets, but with out being '1'



It increments 1 if a number with a character is given.



#include<stdio.h>

int main() {
int x,y;
while(1)
{
printf("Enter a number > ");
if(scanf("%d%d",&x,&y) != 1){
printf("%d",x+y);
break;
}
}
return 0;
}


What could be the reason behind it?







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 14:33









Loufi

15013




15013










asked Nov 13 '18 at 14:28









Sparsh_MishraSparsh_Mishra

67




67








  • 2





    Sorry...what? Can you make it more clear?

    – Sourav Ghosh
    Nov 13 '18 at 14:30











  • What input are you using exactly, what output are you getting, and what output did you expect?

    – interjay
    Nov 13 '18 at 14:31






  • 1





    restrict user input? dont use scanf then. use fgets/sscanf

    – Anders
    Nov 13 '18 at 14:33











  • @SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:34











  • @interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:37














  • 2





    Sorry...what? Can you make it more clear?

    – Sourav Ghosh
    Nov 13 '18 at 14:30











  • What input are you using exactly, what output are you getting, and what output did you expect?

    – interjay
    Nov 13 '18 at 14:31






  • 1





    restrict user input? dont use scanf then. use fgets/sscanf

    – Anders
    Nov 13 '18 at 14:33











  • @SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:34











  • @interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

    – Sparsh_Mishra
    Nov 13 '18 at 14:37








2




2





Sorry...what? Can you make it more clear?

– Sourav Ghosh
Nov 13 '18 at 14:30





Sorry...what? Can you make it more clear?

– Sourav Ghosh
Nov 13 '18 at 14:30













What input are you using exactly, what output are you getting, and what output did you expect?

– interjay
Nov 13 '18 at 14:31





What input are you using exactly, what output are you getting, and what output did you expect?

– interjay
Nov 13 '18 at 14:31




1




1





restrict user input? dont use scanf then. use fgets/sscanf

– Anders
Nov 13 '18 at 14:33





restrict user input? dont use scanf then. use fgets/sscanf

– Anders
Nov 13 '18 at 14:33













@SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

– Sparsh_Mishra
Nov 13 '18 at 14:34





@SouravGhosh I am using number as inputs, and trying to restrict the input to numbers. Basically trying to repeat the input process until i get 2 numbers. In case i give a character as input, the result is incremented by one (example input: a b) output : 1

– Sparsh_Mishra
Nov 13 '18 at 14:34













@interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

– Sparsh_Mishra
Nov 13 '18 at 14:37





@interjay i used inputs : a b, i expected me to ask again for input, but the output produced was 1

– Sparsh_Mishra
Nov 13 '18 at 14:37












1 Answer
1






active

oldest

votes


















1














scanf() returns the number of successful conversions it has performed for the input given.



With




scanf("%d%d", &x, &y)



you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.



If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:



#include <stdio.h>

int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%dn", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != 'n');
}
}
return 0;
}


The more ideomatic way:



int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(nn", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != 'n');
}

printf("%dn", x + y);





share|improve this answer


























  • I think the check was for != 1....so I guess OP knows this.

    – Sourav Ghosh
    Nov 13 '18 at 14:36






  • 1





    The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

    – Sourav Ghosh
    Nov 13 '18 at 14:37











  • thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

    – Sparsh_Mishra
    Nov 13 '18 at 14:39













  • If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

    – Swordfish
    Nov 13 '18 at 14:42













  • @Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

    – Sourav Ghosh
    Nov 13 '18 at 14:51











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%2f53283230%2fwhy-does-this-code-produce-1-on-adding-characters-in-c%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









1














scanf() returns the number of successful conversions it has performed for the input given.



With




scanf("%d%d", &x, &y)



you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.



If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:



#include <stdio.h>

int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%dn", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != 'n');
}
}
return 0;
}


The more ideomatic way:



int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(nn", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != 'n');
}

printf("%dn", x + y);





share|improve this answer


























  • I think the check was for != 1....so I guess OP knows this.

    – Sourav Ghosh
    Nov 13 '18 at 14:36






  • 1





    The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

    – Sourav Ghosh
    Nov 13 '18 at 14:37











  • thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

    – Sparsh_Mishra
    Nov 13 '18 at 14:39













  • If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

    – Swordfish
    Nov 13 '18 at 14:42













  • @Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

    – Sourav Ghosh
    Nov 13 '18 at 14:51
















1














scanf() returns the number of successful conversions it has performed for the input given.



With




scanf("%d%d", &x, &y)



you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.



If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:



#include <stdio.h>

int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%dn", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != 'n');
}
}
return 0;
}


The more ideomatic way:



int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(nn", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != 'n');
}

printf("%dn", x + y);





share|improve this answer


























  • I think the check was for != 1....so I guess OP knows this.

    – Sourav Ghosh
    Nov 13 '18 at 14:36






  • 1





    The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

    – Sourav Ghosh
    Nov 13 '18 at 14:37











  • thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

    – Sparsh_Mishra
    Nov 13 '18 at 14:39













  • If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

    – Swordfish
    Nov 13 '18 at 14:42













  • @Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

    – Sourav Ghosh
    Nov 13 '18 at 14:51














1












1








1







scanf() returns the number of successful conversions it has performed for the input given.



With




scanf("%d%d", &x, &y)



you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.



If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:



#include <stdio.h>

int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%dn", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != 'n');
}
}
return 0;
}


The more ideomatic way:



int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(nn", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != 'n');
}

printf("%dn", x + y);





share|improve this answer















scanf() returns the number of successful conversions it has performed for the input given.



With




scanf("%d%d", &x, &y)



you ask for two integers so scanf() will return 2 if it was successful. Your code however checks for != 1 which will also be true if scanf() returns 0 becuause you entered "a b" and no conversation could be performed.



If not all conversions were successful, all characters not being part of a successful conversion remain in stdin and the next scanf() will try to interpret them again and fail. To prevent that from happening you have to "clear" them:



#include <stdio.h>

int main()
{
while(1)
{
printf("Enter two numbers: ");
int x, y; // define variables as close to where they're used as possible
if (scanf("%d%d", &x, &y) == 2) {
printf("%dn", x + y);
break;
}
else {
int ch; // discard all characters until EOF or a newline:
while ((ch = getchar()) != EOF && ch != 'n');
}
}
return 0;
}


The more ideomatic way:



int x, y;
while (printf("Enter two numbers: "),
scanf("%d%d", &x, &y) != 2)
{
fputs("Input error :(nn", stderr);
int ch;
while ((ch = getchar()) != EOF && ch != 'n');
}

printf("%dn", x + y);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 14:56

























answered Nov 13 '18 at 14:31









SwordfishSwordfish

9,06811335




9,06811335













  • I think the check was for != 1....so I guess OP knows this.

    – Sourav Ghosh
    Nov 13 '18 at 14:36






  • 1





    The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

    – Sourav Ghosh
    Nov 13 '18 at 14:37











  • thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

    – Sparsh_Mishra
    Nov 13 '18 at 14:39













  • If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

    – Swordfish
    Nov 13 '18 at 14:42













  • @Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

    – Sourav Ghosh
    Nov 13 '18 at 14:51



















  • I think the check was for != 1....so I guess OP knows this.

    – Sourav Ghosh
    Nov 13 '18 at 14:36






  • 1





    The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

    – Sourav Ghosh
    Nov 13 '18 at 14:37











  • thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

    – Sparsh_Mishra
    Nov 13 '18 at 14:39













  • If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

    – Swordfish
    Nov 13 '18 at 14:42













  • @Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

    – Sourav Ghosh
    Nov 13 '18 at 14:51

















I think the check was for != 1....so I guess OP knows this.

– Sourav Ghosh
Nov 13 '18 at 14:36





I think the check was for != 1....so I guess OP knows this.

– Sourav Ghosh
Nov 13 '18 at 14:36




1




1





The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

– Sourav Ghosh
Nov 13 '18 at 14:37





The problem may be elsewhere....for a non-numeric input it'll keep failing as the input is not consumed, and would likely cause problem.

– Sourav Ghosh
Nov 13 '18 at 14:37













thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

– Sparsh_Mishra
Nov 13 '18 at 14:39







thanks for that one, but I'd like to learn more about the output produced, example: a+b results in 1 and 12+a results in 13

– Sparsh_Mishra
Nov 13 '18 at 14:39















If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

– Swordfish
Nov 13 '18 at 14:42







If you enter a b your code will execute printf("%dn", x + y); using garbage values for x and y since they were never written to and contain indeterminate values. For 12 a the condition of your if won't be true and the next scanf() will also fail returnin 0 your condition getting true and printf("%dn", x + y); printing garbage.

– Swordfish
Nov 13 '18 at 14:42















@Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

– Sourav Ghosh
Nov 13 '18 at 14:51





@Swordfish 1) I never said the code was correct, it was referring to the then state of your answer, 2)the comment was made just before your expansion. :)

– Sourav Ghosh
Nov 13 '18 at 14:51


















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%2f53283230%2fwhy-does-this-code-produce-1-on-adding-characters-in-c%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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python