3

everyone! I hope someone can help me figure out something in C language. This is my first seriously homework in IT, I have no experience and I'm learning in e-studies, so teacher help isn't very available.

I need to develop console application in C language. User need to input 10 integer numbers, if insert number isn't integer, need to output error and again re-enter new number until all 10 integer numbers will be inserted.

Everything works in case if I say that these 10 numbers can't be 0 (I make this to be sure that my if-else statement working), but won't work when I want that every input number will be check if it is integer or not.

How can I do it right.

Please help

so far my code look like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i;
    float f;
    int numbers[10];

    for (i = 0; i < 10; i++)
    {
        scanf ("%d", &numbers[i]);
        if (numbers[i] != 0)
        {
            scanf ("*%d", &numbers[i]);
        }
        else
        {
            printf ("\nError!Entered number is't integer \n");
            printf ("\nPlease insert number again \n");
            scanf("%*d", &numbers[i]);
        }

    }

}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
effeja
  • 65
  • 4
  • 1
    see http://stackoverflow.com/questions/11139092/what-happens-when-you-input-things-like-12ab-to-scanfd-argu – Lesto Mar 20 '14 at 16:59

5 Answers5

1
#include <stdio.h>

int main(void) {
    int i = 0;
    int val;
    char ch;
    int numbers[10];

    while(i < 10) {
        val = scanf("%d", numbers + i);  // read the integer into a[i]
        if(val != 1) {
            while((ch = getchar()) != '\n')  // discard the invalid input
                ;  // the null statement
            printf("Error! Entered number is not an integer.\n");
            printf("Please enter an integer again.\n");
            val = scanf("%d", numbers + i);
            continue;
        }
        ++i;
    }
    // process the numbers array
    return 0;
}

I write this line again

val = scanf("%d", numbers + i);

Now it works how I need. Great - thanks a lot

effeja
  • 65
  • 4
0

There are several techniques you might use:

  1. Read the number as a string and reject if it contains characters not suitable for an integer. The use sscanf() to convert the string to integer.

  2. Read the number as a float and reject if it is out of integer range or it has a non-integer value.

  3. Read the input character by character and build up an integer value. If invalid characters appear, reject the value.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • Thank you for reply so fast. Can you please write me an example how it looks in code for each one of option? Today I was already trying the sscanf thig, but for there somethnig was wrong – effeja Mar 20 '14 at 17:07
0

scanf returns the number of input items successfully matched and assigned. You can check this value for 1 for each call of scanf. If the value is 0, then you should discard the input to clear the stdin buffer and read input again.

#include <stdio.h>
#include <ctype.h>

int main(void) {
    int i = 0; 
    int val;
    char ch;
    int numbers[10];

    while(i < 10) {
        // read an integer and the first non-numeric character

        val = scanf("%d%c", numbers + i, &ch);  

        // if the number of items assigned by scanf is not 2 or if 
        // the first non-numeric character is not a whitespace, then
        // discard the input and call read input again.
        // for example input of type 32ws are completely discarded

        if(val != 2 || !isspace(ch)) {
            while((ch = getchar()) != '\n')  // discard the invalid input
                ;  // the null statement
            printf("Error! Entered number is not an integer.\n");
            printf("Please enter an integer again.\n");
            continue;
        }
        ++i;
    }
    // process the numbers array
    return 0;
}
ajay
  • 9,402
  • 8
  • 44
  • 71
  • Thank you. This is working, but Is there a way that numbers who program eliminated because isn't integer won't counting in array. For example - user input 1 2 3 0,5 (error) 4 (instead of 0,5) 5 6 7 8 9 10 = total input numbers (integer) are 10, because with your code he would not let enter last number because he counts there number who is not integer. – effeja Mar 20 '14 at 17:29
  • @effeja Then your best bet is to read the integer as a string and then check if the string contains any non-numeric character. – ajay Mar 20 '14 at 17:35
  • @effeja please the updated answer. Now it does what you want. – ajay Mar 20 '14 at 17:53
0

Although I am not entirely clear on the details of your question, here is an outline of code similar to what you want:

int main(void)
{
    int i;
    int numbers[10];
    int sum = 0;

    for(i=0; i<10; ++i)
    {
        printf("Enter #%d:\n", i+1);
        scanf("%d", numbers+i);

        if (numbers[i] % 2 == 0) // Then Number is even
        {
            sum += numbers[i];
        }
    }

    printf("The sum of only the even numbers is %d\n", sum);

    getch();
    return 0;
}
abelenky
  • 63,815
  • 23
  • 109
  • 159
0

To read an int, suggest fgets() then sscanf() or strtol()

#include <stdio.h>
#include <stdlib.h>

int main(void) {
  int i;
  int numbers[10];

  for (i = 0; i < 10; ) {
    char buffer[50];
    if (fgets(buffer, sizeof buffer, stdin) == NULL) break; 
    int n;  // number of `char` parsed
    if (sscanf(buffer, "%d %n", &numbers[i], &n) != 1 || buffer[n] != '\0') {
      printf("Error! Entered number is not an integer.\n");
      printf("Please enter an integer again.\n");
      continue;
    }
    i++;
  }
  return 0;
}

The strtol() approach. This detects overflow issues:

    if (fgets(buffer, sizeof buffer, stdin) == NULL) break; 
    char *endptr;
    errno = 0;  
    long num = strtol(buffer, &endptr, 10);
    if (errno || num < INT_MIN || num > INT_MAX) Handle_RangeError();
    if (buffer == endptr || *endptr != '\n') Handle_SyntaxError();
    numbers[i] = (int) num;        

Recommend making a int GetInt(const char *prompt) function that can be used repeatedly.
User input is evil. Do not trust it until well vetted.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256