why input isn't being taken as i wanted?












-1















In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.



But here, in the 1st case if i use a string with number,then there is 2 output.



Example input:




4



donate 100



report



donate 500




Example output:




0



100



100



0




But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?



#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];

scanf("%d",&T);

while(T--)
{
scanf(" %s",input1);
val=-1;

val=atoi(input1);
total+=val;

printf("nInput="%s" val="%d"n", input1, val);

if(val != -1)
printf("%dn",total);

}
}









share|improve this question




















  • 2





    scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

    – Osiris
    Nov 21 '18 at 16:16













  • Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

    – David Schwartz
    Nov 21 '18 at 16:19






  • 1





    @mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

    – Osiris
    Nov 21 '18 at 16:30






  • 1





    @mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

    – David Schwartz
    Nov 21 '18 at 16:40






  • 1





    @mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

    – David Schwartz
    Nov 21 '18 at 17:22
















-1















In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.



But here, in the 1st case if i use a string with number,then there is 2 output.



Example input:




4



donate 100



report



donate 500




Example output:




0



100



100



0




But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?



#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];

scanf("%d",&T);

while(T--)
{
scanf(" %s",input1);
val=-1;

val=atoi(input1);
total+=val;

printf("nInput="%s" val="%d"n", input1, val);

if(val != -1)
printf("%dn",total);

}
}









share|improve this question




















  • 2





    scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

    – Osiris
    Nov 21 '18 at 16:16













  • Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

    – David Schwartz
    Nov 21 '18 at 16:19






  • 1





    @mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

    – Osiris
    Nov 21 '18 at 16:30






  • 1





    @mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

    – David Schwartz
    Nov 21 '18 at 16:40






  • 1





    @mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

    – David Schwartz
    Nov 21 '18 at 17:22














-1












-1








-1








In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.



But here, in the 1st case if i use a string with number,then there is 2 output.



Example input:




4



donate 100



report



donate 500




Example output:




0



100



100



0




But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?



#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];

scanf("%d",&T);

while(T--)
{
scanf(" %s",input1);
val=-1;

val=atoi(input1);
total+=val;

printf("nInput="%s" val="%d"n", input1, val);

if(val != -1)
printf("%dn",total);

}
}









share|improve this question
















In this program i want to get a input where will be a string with some int.if no values were not given then, the program will just show the total amount.



But here, in the 1st case if i use a string with number,then there is 2 output.



Example input:




4



donate 100



report



donate 500




Example output:




0



100



100



0




But notice here i got chance of 3 times to input string,but output is 4 time.
So, can anyone tell me why this program acting weird?



#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=0;
char input1[20];

scanf("%d",&T);

while(T--)
{
scanf(" %s",input1);
val=-1;

val=atoi(input1);
total+=val;

printf("nInput="%s" val="%d"n", input1, val);

if(val != -1)
printf("%dn",total);

}
}






c string input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 5 '18 at 14:21









gsamaras

51.9k24107191




51.9k24107191










asked Nov 21 '18 at 16:12









mahin hossenmahin hossen

958




958








  • 2





    scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

    – Osiris
    Nov 21 '18 at 16:16













  • Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

    – David Schwartz
    Nov 21 '18 at 16:19






  • 1





    @mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

    – Osiris
    Nov 21 '18 at 16:30






  • 1





    @mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

    – David Schwartz
    Nov 21 '18 at 16:40






  • 1





    @mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

    – David Schwartz
    Nov 21 '18 at 17:22














  • 2





    scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

    – Osiris
    Nov 21 '18 at 16:16













  • Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

    – David Schwartz
    Nov 21 '18 at 16:19






  • 1





    @mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

    – Osiris
    Nov 21 '18 at 16:30






  • 1





    @mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

    – David Schwartz
    Nov 21 '18 at 16:40






  • 1





    @mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

    – David Schwartz
    Nov 21 '18 at 17:22








2




2





scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

– Osiris
Nov 21 '18 at 16:16







scanf(" %s", input1); reads till the next whitespace. first scanf will read donate and the second 100.

– Osiris
Nov 21 '18 at 16:16















Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

– David Schwartz
Nov 21 '18 at 16:19





Why not just add a line of code to log what's in input1 and what's in val right before the if and figure it out yourself?

– David Schwartz
Nov 21 '18 at 16:19




1




1





@mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

– Osiris
Nov 21 '18 at 16:30





@mahinhossen If you want to read a string until new line you should use scanf(" %[^n]s",input1);

– Osiris
Nov 21 '18 at 16:30




1




1





@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

– David Schwartz
Nov 21 '18 at 16:40





@mahinhossen Right, so you do want them separate. You want to be able to ignore the "donate" and process the "100". That's most easily done if they're separate. If they're together, you'd just have to separate them because you want to handle the two things differently.

– David Schwartz
Nov 21 '18 at 16:40




1




1





@mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

– David Schwartz
Nov 21 '18 at 17:22





@mahinhossen Your scanf is fine. You just need to add code to ignore the input if it's not a number. Your choice of atoi was not a good one, since it has no error detection.

– David Schwartz
Nov 21 '18 at 17:22












1 Answer
1






active

oldest

votes


















1














Simply add logging and the answer will be obvious:



#include<stdio.h>
#include<stdlib.h>
int main()
{
int T, total=0, val=-1;
char input1[20];

scanf("%d",&T);

while(T--)
{
scanf(" %s",input1);
val=-1;

val=atoi(input1);
total+=val;

printf("nInput='%s' val='%d'n", input1, val);

if(val != -1)
printf("%dn",total);

}
}


Then run it and give it your input:



4
donate 100

Input='donate' val='0'
0

Input='100' val='100'
100
report

Input='report' val='0'
100
donate 500

Input='donate' val='0'
100


As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.






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%2f53416201%2fwhy-input-isnt-being-taken-as-i-wanted%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














    Simply add logging and the answer will be obvious:



    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
    int T, total=0, val=-1;
    char input1[20];

    scanf("%d",&T);

    while(T--)
    {
    scanf(" %s",input1);
    val=-1;

    val=atoi(input1);
    total+=val;

    printf("nInput='%s' val='%d'n", input1, val);

    if(val != -1)
    printf("%dn",total);

    }
    }


    Then run it and give it your input:



    4
    donate 100

    Input='donate' val='0'
    0

    Input='100' val='100'
    100
    report

    Input='report' val='0'
    100
    donate 500

    Input='donate' val='0'
    100


    As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.






    share|improve this answer




























      1














      Simply add logging and the answer will be obvious:



      #include<stdio.h>
      #include<stdlib.h>
      int main()
      {
      int T, total=0, val=-1;
      char input1[20];

      scanf("%d",&T);

      while(T--)
      {
      scanf(" %s",input1);
      val=-1;

      val=atoi(input1);
      total+=val;

      printf("nInput='%s' val='%d'n", input1, val);

      if(val != -1)
      printf("%dn",total);

      }
      }


      Then run it and give it your input:



      4
      donate 100

      Input='donate' val='0'
      0

      Input='100' val='100'
      100
      report

      Input='report' val='0'
      100
      donate 500

      Input='donate' val='0'
      100


      As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.






      share|improve this answer


























        1












        1








        1







        Simply add logging and the answer will be obvious:



        #include<stdio.h>
        #include<stdlib.h>
        int main()
        {
        int T, total=0, val=-1;
        char input1[20];

        scanf("%d",&T);

        while(T--)
        {
        scanf(" %s",input1);
        val=-1;

        val=atoi(input1);
        total+=val;

        printf("nInput='%s' val='%d'n", input1, val);

        if(val != -1)
        printf("%dn",total);

        }
        }


        Then run it and give it your input:



        4
        donate 100

        Input='donate' val='0'
        0

        Input='100' val='100'
        100
        report

        Input='report' val='0'
        100
        donate 500

        Input='donate' val='0'
        100


        As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.






        share|improve this answer













        Simply add logging and the answer will be obvious:



        #include<stdio.h>
        #include<stdlib.h>
        int main()
        {
        int T, total=0, val=-1;
        char input1[20];

        scanf("%d",&T);

        while(T--)
        {
        scanf(" %s",input1);
        val=-1;

        val=atoi(input1);
        total+=val;

        printf("nInput='%s' val='%d'n", input1, val);

        if(val != -1)
        printf("%dn",total);

        }
        }


        Then run it and give it your input:



        4
        donate 100

        Input='donate' val='0'
        0

        Input='100' val='100'
        100
        report

        Input='report' val='0'
        100
        donate 500

        Input='donate' val='0'
        100


        As you can see, your expectation that you'd somehow get a -1 value in val if atoi wasn't given a number is incorrect.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 16:22









        David SchwartzDavid Schwartz

        138k14145230




        138k14145230
































            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%2f53416201%2fwhy-input-isnt-being-taken-as-i-wanted%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

            How to change which sound is reproduced for terminal bell?

            Can I use Tabulator js library in my java Spring + Thymeleaf project?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents