title: Cannot figure out how to check a value in C code
body: <p>Hello I'm fairly new to programming so here's my problem, I have multiple arrays of the size x,
  I want to add b values to my array, what I did is x + b then ask the user to type his value then call a function that looks if the value is already in the array, if it is, I ask the user to type it again, but the problem is that the search function returns random numbers. heres my code (the fRecherche2 works fine outside of fAjouter):</p>
  <pre><code>int fRecherche2 (int ref[],int n,int nref)
  
      {
          int i;
          for (i=0;i&lt;n;i++)
              if (ref[i]==nref)
                  return i;
          return -1;
      }
      
  int fAjouter(int ref[],int qt[],float prix[],int sds[],int n)
          {
              int i,prods,nb,code,nref;
              printf(&quot;How many products would you like to add:&quot;);
              scanf(&quot;%d&quot;,&amp; prods);
              fVerifVal(prods);
              n=NbArticle();
              nb=n+prods;
              for(i=n;i&lt;nb;i++)
              {
                  printf(&quot;Reference: &quot;);
                  scanf(&quot;%d&quot;, &amp;ref[i]);
                  ref[i]=nref;
                  code=fRecherche2(ref,n,nref);
                  printf(&quot;%d\n&quot;,code);
                  while(code!=-1)
                  {
                      printf(&quot;The reference already exists.\n&quot;);
                      printf(&quot;Please try again: &quot;);
                      scanf(&quot;%d&quot;, &amp;ref[i]);
                      ref[i]=nref;
                      code=fRecherche2(ref,n,nref);
                      printf(&quot;%d&quot;,code);
                      if(code==-1)
                          break;
                  }
                  fVerif(ref,i); //Checks if the value is bigger than 0
                  printf(&quot;Amount: &quot;);
                  scanf(&quot;%d&quot;, &amp;qt[i]);
                  fVerif(qt,i);
                  printf(&quot;Price: &quot;);
                  scanf(&quot;%f&quot;, &amp;prix[i]);
                  fVerifReal(prix,i); //Checks if the value is bigger than 0.00
                  printf(&quot;Security threshold: &quot;);
                  scanf(&quot;%d&quot;, &amp;sds[i]);
                  fVerif(sds,i);
          
              }
              fEnregNombre(nb); //saves my new size in a file
              code=fEnreg(ref,qt,prix,sds,nb); // saves all my new arrays in a file
              if(code==-1)
                  return -1;
      }
  void fCreerStock (void)
  {
      int i,n;
      printf(&quot;Combien d'articles : &quot;);
      scanf(&quot;%d&quot;, &amp;n );
      while(n&lt;=1)
          {
              printf(&quot;Il faut au moins 1 article!\n&quot;);
              printf(&quot;Veuillez réesayer: &quot;);
              scanf(&quot;%d&quot;,&amp;n);
              if(n&gt;0)
                  break;
          }
          FILE * nombre;
          nombre=fopen(&quot;Nombre_d'article.txt&quot;,&quot;w&quot;);
          if (nombre == NULL)
          {
              printf(&quot;probleme d'ouverture du fichier \n&quot;);
              exit(1);
          }
              fprintf(nombre,&quot;%d&quot;,n);
  
          fclose(nombre);
      int ref[n],qt [n], sds [n];
      float prix[n];
      for (i=0; i&lt;n; i++)
      {
          printf(&quot;reference %d : &quot;, i);
          scanf(&quot;%d&quot;, &amp;ref[i]);
          fVerif(ref,i);
          printf(&quot;quantité %d : &quot;, i);
          scanf(&quot;%d&quot;, &amp;qt[i]);
          fVerif(qt,i);
          printf(&quot;Prix %d : &quot;, i);
          scanf(&quot;%f&quot;, &amp;prix[i]);
          fVerifReal(prix,i);
          printf(&quot;seuil de sécurité %d : &quot;, i);
          scanf(&quot;%d&quot;, &amp;sds[i]);
          fVerif(sds,i);
  
      }
      FILE * flot;
      flot=fopen(&quot;nom.txt&quot;,&quot;a&quot;);
      if (flot == NULL)
      {
          printf(&quot;Probleme d'ouverture du fichier \n&quot;);
          exit(1);
      }
      for (i=0; i&lt;n; i++)
          fprintf(flot,&quot;%d\t%d\t%.2f\t%d\n&quot;,ref[i],qt[i],prix[i],sds[i]);
  
      fclose(flot);
  }
  
  #include &lt;stdio.h&gt;
  #include &quot;sae.h&quot;
  
  void globale (void)
  {
      int n;
      n=NbArticle();
      int ref[n],qt[n],sds[n],CodeErr,code,CodeSup;
      float prix[n];
      char choix;
      CodeErr=fConsulterStock(ref,qt,prix,sds,n);
      printf(&quot;\n\nBienvenue a l'application de gestion de stock 'Xtrachaus'\n\n&quot;);
  
      if (CodeErr== -1 || n == -1)
          printf(&quot;!! Vous n'avez pas encore créer de stock veuillez créer un stock avant de faire n'importe quel manipulation !! \n\n&quot;);
  
      printf(&quot;Créer un stock: 'c'| Afficher le stock: 'o'| Etat du stock: 'e'| Faire le devis d'une commade: 'd'| Récapitulatif des ventes: 'r'| Approvisionnement: 'a'| \nSupprimer un article:'s'| Rechercher un article avec n° ref: 'n'| Modifier l'article :'m'| Ajouter un article: 't'\n\n&quot;);
  
      printf(&quot;Que voulez vous faire(votre choix): &quot;);
      scanf(&quot;%c%*c&quot;, &amp;choix);
      if (choix =='c')
          fCreerStock();
      if (choix =='o')
          fAfficherStock(ref,qt,prix,sds,n);
      if (choix =='e')
          fEtatStock(ref,qt,prix,sds,n);
      /*if (choix =='d')
          fDevis();
      if (choix =='r')
          fVentes();*/
      if (choix =='a')
      {
          fAfficherStock(ref,qt,prix,sds,n);
          code=fAppro(ref,qt,prix,sds,n);
          if(code==-1)
              printf(&quot;Erreur, l'approvisionement ne peut pas s'effectuer.\n&quot;);
          else
              printf(&quot;Approvisionement éffectué. \n&quot;);
          printf(&quot;\n&quot;);
          fAfficherStock(ref,qt,prix,sds,n);
      }
      if (choix =='s')
      {
          CodeSup=fSuppression(ref,qt,prix,sds,n);
          if(CodeSup!=-1)
              printf(&quot;Suppression éffectué. \n&quot;);
          else
              printf(&quot;Erreur, la suppression ne peut pas s'effectuer.\n&quot;);
      }
      if (choix =='n')
          {
              code=fRecherche(ref,n);
              if (code!=-1)
              {
                  printf(&quot;\nReference:\tQuantite:\tPrix:\tSeuil de securite:\n&quot;);
                  printf(&quot;%d\t\t%d\t\t%.2f\t\t%d\n&quot; ,ref[code], qt[code], prix[code], sds[code]);
              }           
              else 
                  printf(&quot;La réference n'existe pas\n&quot;);
          }
  
      if (choix =='t')
      {
          code==fAjouter(ref,qt,prix,sds,n);
          if(code==-1)
              printf(&quot;Erreur, l'ajout ne peut pas s'effectuer.\n&quot;);
          else
              printf(&quot;Ajout éffectué. \n&quot;);
      }
      if (choix =='m')
      {
          code=fModifier(ref,qt,prix,sds,n);
          if(code==-1)
              printf(&quot;Erreur, la modification ne peut pas s'effectuer.\n&quot;);
          else
              printf(&quot;Modification éffectué. \n&quot;);
      }
  }
  
      
  int main (void)
  {
  
      globale();
      return 0;
  }
  </code></pre>
  <p>If theres anything else I could provide please tell me! Thanks.
  (sorry most of the code and variables are in french i translated the part im stuck at).</p>
  <p>edit3:</p>
  <p>heres examples of where the program fails:</p>
  <p>before adding a value:
  <a href="https://imgur.com/XRbXvVl" rel="nofollow noreferrer">https://imgur.com/XRbXvVl</a>
  After:
  <a href="https://i.imgur.com/mRI1aJq.png" rel="nofollow noreferrer">https://i.imgur.com/mRI1aJq.png</a> (i choose 't' to add a value then type the how many i want to add, in this picture i wanted 1 more value, but i don't get to type my information, it immeadiatly adds random values)
  <a href="https://imgur.com/HJk1PaW" rel="nofollow noreferrer">https://imgur.com/HJk1PaW</a></p>