C: prompt input one line at a time and check












0















Problem statement:

Loop 5 times. Each time, ask the user for an integer, check that the input received is that type, and then ask for another input until the user has given five correct inputs. The output of this program should look like this, assuming that the user gives five correct inputs of type int:



Hello! Please give me an integer: 0

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 2

Thanks! Please give me another integer: 3

Thanks! I am happy with five integers.



My attempts:



#include <stdio.h>

int invalid(x)
{
printf("nThat was not an integer, please give me an integer: ");
scanf("%d", &x);
}

int main()
{
int a, b, c, d, e, x;

printf("Hello! Please give me an integer: ");
scanf("%d", &a);
if(scanf("%d", &a) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &b);
if(scanf("%d", &b) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &c);
if(scanf("%d", &c) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &d);
if(scanf("%d", &d) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &e);
if(scanf("%d", &e) != 1)
{
invalid(x);
}
printf("nThanks! I am happy with five integers.n");
return 0;
}


// Failed attempt to use a loop //
for(i = 0; i < 4; i++)
{
printf("Thanks! Please give me another integer: ");
scanf("%d", &y);
if(scanf("%d", &y) != 1)
{
invalid(y);
}
}







do
{
printf ("Thanks! Please give me another integer: ", );
scanf("%d", &x);
for(scanf("%d", &x) != 1)
{
printf("That was not an integer, please give me an integer: ")
scanf("%d", &x);
}
i++;
} while (i < 4);


Common outputs that I get from the first input being a letter or non-integer:



Hello! Please give me an integer: d  
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! I am happy with five integers.









share|improve this question























  • I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

    – Viezevingertjes
    Nov 22 '18 at 5:57











  • Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

    – TonyB
    Nov 22 '18 at 6:00











  • When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

    – Jonathan Leffler
    Nov 22 '18 at 6:20








  • 1





    You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

    – Jonathan Leffler
    Nov 22 '18 at 6:21













  • Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

    – Basile Starynkevitch
    Nov 22 '18 at 6:28


















0















Problem statement:

Loop 5 times. Each time, ask the user for an integer, check that the input received is that type, and then ask for another input until the user has given five correct inputs. The output of this program should look like this, assuming that the user gives five correct inputs of type int:



Hello! Please give me an integer: 0

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 2

Thanks! Please give me another integer: 3

Thanks! I am happy with five integers.



My attempts:



#include <stdio.h>

int invalid(x)
{
printf("nThat was not an integer, please give me an integer: ");
scanf("%d", &x);
}

int main()
{
int a, b, c, d, e, x;

printf("Hello! Please give me an integer: ");
scanf("%d", &a);
if(scanf("%d", &a) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &b);
if(scanf("%d", &b) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &c);
if(scanf("%d", &c) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &d);
if(scanf("%d", &d) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &e);
if(scanf("%d", &e) != 1)
{
invalid(x);
}
printf("nThanks! I am happy with five integers.n");
return 0;
}


// Failed attempt to use a loop //
for(i = 0; i < 4; i++)
{
printf("Thanks! Please give me another integer: ");
scanf("%d", &y);
if(scanf("%d", &y) != 1)
{
invalid(y);
}
}







do
{
printf ("Thanks! Please give me another integer: ", );
scanf("%d", &x);
for(scanf("%d", &x) != 1)
{
printf("That was not an integer, please give me an integer: ")
scanf("%d", &x);
}
i++;
} while (i < 4);


Common outputs that I get from the first input being a letter or non-integer:



Hello! Please give me an integer: d  
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! I am happy with five integers.









share|improve this question























  • I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

    – Viezevingertjes
    Nov 22 '18 at 5:57











  • Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

    – TonyB
    Nov 22 '18 at 6:00











  • When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

    – Jonathan Leffler
    Nov 22 '18 at 6:20








  • 1





    You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

    – Jonathan Leffler
    Nov 22 '18 at 6:21













  • Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

    – Basile Starynkevitch
    Nov 22 '18 at 6:28
















0












0








0








Problem statement:

Loop 5 times. Each time, ask the user for an integer, check that the input received is that type, and then ask for another input until the user has given five correct inputs. The output of this program should look like this, assuming that the user gives five correct inputs of type int:



Hello! Please give me an integer: 0

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 2

Thanks! Please give me another integer: 3

Thanks! I am happy with five integers.



My attempts:



#include <stdio.h>

int invalid(x)
{
printf("nThat was not an integer, please give me an integer: ");
scanf("%d", &x);
}

int main()
{
int a, b, c, d, e, x;

printf("Hello! Please give me an integer: ");
scanf("%d", &a);
if(scanf("%d", &a) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &b);
if(scanf("%d", &b) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &c);
if(scanf("%d", &c) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &d);
if(scanf("%d", &d) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &e);
if(scanf("%d", &e) != 1)
{
invalid(x);
}
printf("nThanks! I am happy with five integers.n");
return 0;
}


// Failed attempt to use a loop //
for(i = 0; i < 4; i++)
{
printf("Thanks! Please give me another integer: ");
scanf("%d", &y);
if(scanf("%d", &y) != 1)
{
invalid(y);
}
}







do
{
printf ("Thanks! Please give me another integer: ", );
scanf("%d", &x);
for(scanf("%d", &x) != 1)
{
printf("That was not an integer, please give me an integer: ")
scanf("%d", &x);
}
i++;
} while (i < 4);


Common outputs that I get from the first input being a letter or non-integer:



Hello! Please give me an integer: d  
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! I am happy with five integers.









share|improve this question














Problem statement:

Loop 5 times. Each time, ask the user for an integer, check that the input received is that type, and then ask for another input until the user has given five correct inputs. The output of this program should look like this, assuming that the user gives five correct inputs of type int:



Hello! Please give me an integer: 0

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 1

Thanks! Please give me another integer: 2

Thanks! Please give me another integer: 3

Thanks! I am happy with five integers.



My attempts:



#include <stdio.h>

int invalid(x)
{
printf("nThat was not an integer, please give me an integer: ");
scanf("%d", &x);
}

int main()
{
int a, b, c, d, e, x;

printf("Hello! Please give me an integer: ");
scanf("%d", &a);
if(scanf("%d", &a) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &b);
if(scanf("%d", &b) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &c);
if(scanf("%d", &c) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &d);
if(scanf("%d", &d) != 1)
{
invalid(x);
}
printf("nThanks! Please give me another integer: ");
scanf("%d", &e);
if(scanf("%d", &e) != 1)
{
invalid(x);
}
printf("nThanks! I am happy with five integers.n");
return 0;
}


// Failed attempt to use a loop //
for(i = 0; i < 4; i++)
{
printf("Thanks! Please give me another integer: ");
scanf("%d", &y);
if(scanf("%d", &y) != 1)
{
invalid(y);
}
}







do
{
printf ("Thanks! Please give me another integer: ", );
scanf("%d", &x);
for(scanf("%d", &x) != 1)
{
printf("That was not an integer, please give me an integer: ")
scanf("%d", &x);
}
i++;
} while (i < 4);


Common outputs that I get from the first input being a letter or non-integer:



Hello! Please give me an integer: d  
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! Please give me another integer:
That was not an integer, please give me an integer: Thanks! I am happy with five integers.






c loops printf scanf user-input






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 5:54









AlexisAlexis

1




1













  • I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

    – Viezevingertjes
    Nov 22 '18 at 5:57











  • Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

    – TonyB
    Nov 22 '18 at 6:00











  • When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

    – Jonathan Leffler
    Nov 22 '18 at 6:20








  • 1





    You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

    – Jonathan Leffler
    Nov 22 '18 at 6:21













  • Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

    – Basile Starynkevitch
    Nov 22 '18 at 6:28





















  • I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

    – Viezevingertjes
    Nov 22 '18 at 5:57











  • Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

    – TonyB
    Nov 22 '18 at 6:00











  • When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

    – Jonathan Leffler
    Nov 22 '18 at 6:20








  • 1





    You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

    – Jonathan Leffler
    Nov 22 '18 at 6:21













  • Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

    – Basile Starynkevitch
    Nov 22 '18 at 6:28



















I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

– Viezevingertjes
Nov 22 '18 at 5:57





I dont know C very well, but it looks like you are executing scanf() twice. I think you should store the first one in a variable and then use it in the if().

– Viezevingertjes
Nov 22 '18 at 5:57













Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

– TonyB
Nov 22 '18 at 6:00





Problem statement said to create a "loop"... you didn't... you used simple sequence. Perhaps use a "while", or a "do while", or maybe even a "for"...

– TonyB
Nov 22 '18 at 6:00













When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

– Jonathan Leffler
Nov 22 '18 at 6:20







When scanf() fails to convert d into an integer, it leaves d in the input for the next input operation. Since that's another attempt to convert to d to an integer, it fails again, and the process gets boring. If the input fails to convert, you probably need to gobble to the end of the line: static inline void gobble(void) { int c; while ((c = getchar()) != EOF && c != 'n') ; } and call gobble(); after you detect a problem.

– Jonathan Leffler
Nov 22 '18 at 6:20






1




1





You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

– Jonathan Leffler
Nov 22 '18 at 6:21







You're also using a K&R style function where the parameter x is implicitly typed int. That's been invalid according to standard C for the whole of this millennium. Time to upgrade to 21st Century C; leave it to old fogies like me to deal with super-antiquated code (and even I don't write in that style — but I do get to fix code that was written like that). Note that you alter the argument x, but that never affects anything in the main() function.

– Jonathan Leffler
Nov 22 '18 at 6:21















Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

– Basile Starynkevitch
Nov 22 '18 at 6:28







Read how to debug small programs and the documentation of standard IO functions that you are using. Be aware that stdio is buffering. Consider using fflush or put n at end of printf control format strings. Compile with all warnings and debug info gcc -Wall -Wextra -g

– Basile Starynkevitch
Nov 22 '18 at 6:28














1 Answer
1






active

oldest

votes


















1















  1. You have to use array to store 5 numbers (or 4, or any other amount greater than 1)

  2. In any case you should use loop to ask 5 (or any other series of numbers) and here for loop will be good

  3. You should analyse value returned by scanf to check if the input was correct, and re ask if value is wrong (this can be also loop, but do..while is prehereble) after incorrect characters removed from input buffer.


UPDATE



My version of the prog:



#include <stdio.h>

#define NUM_CNT 5

int main(void)
{
int i, res, c;
int num[NUM_CNT]; // array for all your numbers
printf("Hello!n");
for (i = 0; i < NUM_CNT; i++) {
printf("Please give me an integer: ");
do {
res = scanf("%d", &num[i]);
if ( res ) {
printf("Thanks!n");
} else {
printf("That was not an integer, please give me an integer: ");
while ((c = getchar()) != 'n' && c != EOF); // clean input buffer
}
} while(res != 1);
}
printf("I am happy with five integers.n");
// just to see all the numbers
for (i = 0; i < NUM_CNT; i++) {
printf("%d ", num[i]);
}
return 0;
}


You are free to remake your code using my ideas.






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%2f53424663%2fc-prompt-input-one-line-at-a-time-and-check%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















    1. You have to use array to store 5 numbers (or 4, or any other amount greater than 1)

    2. In any case you should use loop to ask 5 (or any other series of numbers) and here for loop will be good

    3. You should analyse value returned by scanf to check if the input was correct, and re ask if value is wrong (this can be also loop, but do..while is prehereble) after incorrect characters removed from input buffer.


    UPDATE



    My version of the prog:



    #include <stdio.h>

    #define NUM_CNT 5

    int main(void)
    {
    int i, res, c;
    int num[NUM_CNT]; // array for all your numbers
    printf("Hello!n");
    for (i = 0; i < NUM_CNT; i++) {
    printf("Please give me an integer: ");
    do {
    res = scanf("%d", &num[i]);
    if ( res ) {
    printf("Thanks!n");
    } else {
    printf("That was not an integer, please give me an integer: ");
    while ((c = getchar()) != 'n' && c != EOF); // clean input buffer
    }
    } while(res != 1);
    }
    printf("I am happy with five integers.n");
    // just to see all the numbers
    for (i = 0; i < NUM_CNT; i++) {
    printf("%d ", num[i]);
    }
    return 0;
    }


    You are free to remake your code using my ideas.






    share|improve this answer






























      1















      1. You have to use array to store 5 numbers (or 4, or any other amount greater than 1)

      2. In any case you should use loop to ask 5 (or any other series of numbers) and here for loop will be good

      3. You should analyse value returned by scanf to check if the input was correct, and re ask if value is wrong (this can be also loop, but do..while is prehereble) after incorrect characters removed from input buffer.


      UPDATE



      My version of the prog:



      #include <stdio.h>

      #define NUM_CNT 5

      int main(void)
      {
      int i, res, c;
      int num[NUM_CNT]; // array for all your numbers
      printf("Hello!n");
      for (i = 0; i < NUM_CNT; i++) {
      printf("Please give me an integer: ");
      do {
      res = scanf("%d", &num[i]);
      if ( res ) {
      printf("Thanks!n");
      } else {
      printf("That was not an integer, please give me an integer: ");
      while ((c = getchar()) != 'n' && c != EOF); // clean input buffer
      }
      } while(res != 1);
      }
      printf("I am happy with five integers.n");
      // just to see all the numbers
      for (i = 0; i < NUM_CNT; i++) {
      printf("%d ", num[i]);
      }
      return 0;
      }


      You are free to remake your code using my ideas.






      share|improve this answer




























        1












        1








        1








        1. You have to use array to store 5 numbers (or 4, or any other amount greater than 1)

        2. In any case you should use loop to ask 5 (or any other series of numbers) and here for loop will be good

        3. You should analyse value returned by scanf to check if the input was correct, and re ask if value is wrong (this can be also loop, but do..while is prehereble) after incorrect characters removed from input buffer.


        UPDATE



        My version of the prog:



        #include <stdio.h>

        #define NUM_CNT 5

        int main(void)
        {
        int i, res, c;
        int num[NUM_CNT]; // array for all your numbers
        printf("Hello!n");
        for (i = 0; i < NUM_CNT; i++) {
        printf("Please give me an integer: ");
        do {
        res = scanf("%d", &num[i]);
        if ( res ) {
        printf("Thanks!n");
        } else {
        printf("That was not an integer, please give me an integer: ");
        while ((c = getchar()) != 'n' && c != EOF); // clean input buffer
        }
        } while(res != 1);
        }
        printf("I am happy with five integers.n");
        // just to see all the numbers
        for (i = 0; i < NUM_CNT; i++) {
        printf("%d ", num[i]);
        }
        return 0;
        }


        You are free to remake your code using my ideas.






        share|improve this answer
















        1. You have to use array to store 5 numbers (or 4, or any other amount greater than 1)

        2. In any case you should use loop to ask 5 (or any other series of numbers) and here for loop will be good

        3. You should analyse value returned by scanf to check if the input was correct, and re ask if value is wrong (this can be also loop, but do..while is prehereble) after incorrect characters removed from input buffer.


        UPDATE



        My version of the prog:



        #include <stdio.h>

        #define NUM_CNT 5

        int main(void)
        {
        int i, res, c;
        int num[NUM_CNT]; // array for all your numbers
        printf("Hello!n");
        for (i = 0; i < NUM_CNT; i++) {
        printf("Please give me an integer: ");
        do {
        res = scanf("%d", &num[i]);
        if ( res ) {
        printf("Thanks!n");
        } else {
        printf("That was not an integer, please give me an integer: ");
        while ((c = getchar()) != 'n' && c != EOF); // clean input buffer
        }
        } while(res != 1);
        }
        printf("I am happy with five integers.n");
        // just to see all the numbers
        for (i = 0; i < NUM_CNT; i++) {
        printf("%d ", num[i]);
        }
        return 0;
        }


        You are free to remake your code using my ideas.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 '18 at 6:31

























        answered Nov 22 '18 at 6:19









        VolAndVolAnd

        5,53831537




        5,53831537
































            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%2f53424663%2fc-prompt-input-one-line-at-a-time-and-check%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







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()