-1

The program is in Italian but it's very simple. I ask how many numbers I have to insert in the vector and then you insert them one by one. I then do for loop to check every number that's been inserted but it doesn't seem work. Anyone know why?

#include <stdio.h>

int main(int argc, const char *argv[])
{

    int vett[30];
    int n, i;
    int conf;
    int n2;

    do
    {
        printf("quanti numeri");
        scanf("%d", &n);
    } while (n <= 0);

    for (i = 0; i < n; i++)
    {
        printf("inserire numero:");
        scanf("%d", &vett[i]);
    }
    printf("inserire secondo numero");
    scanf("%d, &n2");
    conf = 0;
    for (i = 0; i < n; i++)
    {
        if (vett[i] == n2)
        {
            conf = 1;
        }
    }
    if (conf == 1)
    {
        printf("il numero e' contenuto nel vettore");
    }
    else if (conf == 0)
    {
        printf("il numero non e' contenuto nel vettore");
    }

    return 0;
}
Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
Fish
  • 11
  • 4
  • 2
    You have `scanf("%d, &n2");` — you need `scanf("%d", &n2);`. Your compiler should be warning you about this. – Jonathan Leffler Feb 06 '20 at 15:27
  • You should always check return value of `scanf`. If the number of parsed values is not as expected, you can detect it. – Gerhardh Feb 06 '20 at 15:40
  • Besides, always check [what `scanf` returns](https://stackoverflow.com/questions/10469643/what-does-the-scanf-function-return). – Bob__ Feb 06 '20 at 15:40

1 Answers1

2
foo.c:17:9: warning: format ‘%d’ expects a matching ‘int *’ argument [-Wformat=]
   scanf("%d, &n2");

The compiler knows all, the compiler knows best.

Goswin von Brederlow
  • 7,800
  • 1
  • 18
  • 39