Home > Net >  Cannot figure out how to check a value in C code
Cannot figure out how to check a value in C code

Time:11-05

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):

int fRecherche2 (int ref[],int n,int nref)

    {
        int i;
        for (i=0;i<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("How many products would you like to add:");
            scanf("%d",& prods);
            fVerifVal(prods);
            n=NbArticle();
            nb=n prods;
            for(i=n;i<nb;i  )
            {
                printf("Reference: ");
                scanf("%d", &ref[i]);
                ref[i]=nref;
                code=fRecherche2(ref,n,nref);
                printf("%d\n",code);
                while(code!=-1)
                {
                    printf("The reference already exists.\n");
                    printf("Please try again: ");
                    scanf("%d", &ref[i]);
                    ref[i]=nref;
                    code=fRecherche2(ref,n,nref);
                    printf("%d",code);
                    if(code==-1)
                        break;
                }
                fVerif(ref,i); //Checks if the value is bigger than 0
                printf("Amount: ");
                scanf("%d", &qt[i]);
                fVerif(qt,i);
                printf("Price: ");
                scanf("%f", &prix[i]);
                fVerifReal(prix,i); //Checks if the value is bigger than 0.00
                printf("Security threshold: ");
                scanf("%d", &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("Combien d'articles : ");
    scanf("%d", &n );
    while(n<=1)
        {
            printf("Il faut au moins 1 article!\n");
            printf("Veuillez réesayer: ");
            scanf("%d",&n);
            if(n>0)
                break;
        }
        FILE * nombre;
        nombre=fopen("Nombre_d'article.txt","w");
        if (nombre == NULL)
        {
            printf("probleme d'ouverture du fichier \n");
            exit(1);
        }
            fprintf(nombre,"%d",n);

        fclose(nombre);
    int ref[n],qt [n], sds [n];
    float prix[n];
    for (i=0; i<n; i  )
    {
        printf("reference %d : ", i);
        scanf("%d", &ref[i]);
        fVerif(ref,i);
        printf("quantité %d : ", i);
        scanf("%d", &qt[i]);
        fVerif(qt,i);
        printf("Prix %d : ", i);
        scanf("%f", &prix[i]);
        fVerifReal(prix,i);
        printf("seuil de sécurité %d : ", i);
        scanf("%d", &sds[i]);
        fVerif(sds,i);

    }
    FILE * flot;
    flot=fopen("nom.txt","a");
    if (flot == NULL)
    {
        printf("Probleme d'ouverture du fichier \n");
        exit(1);
    }
    for (i=0; i<n; i  )
        fprintf(flot,"%d\t%d\t%.2f\t%d\n",ref[i],qt[i],prix[i],sds[i]);

    fclose(flot);
}

#include <stdio.h>
#include "sae.h"

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("\n\nBienvenue a l'application de gestion de stock 'Xtrachaus'\n\n");

    if (CodeErr== -1 || n == -1)
        printf("!! Vous n'avez pas encore créer de stock veuillez créer un stock avant de faire n'importe quel manipulation !! \n\n");

    printf("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");

    printf("Que voulez vous faire(votre choix): ");
    scanf("%c%*c", &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("Erreur, l'approvisionement ne peut pas s'effectuer.\n");
        else
            printf("Approvisionement éffectué. \n");
        printf("\n");
        fAfficherStock(ref,qt,prix,sds,n);
    }
    if (choix =='s')
    {
        CodeSup=fSuppression(ref,qt,prix,sds,n);
        if(CodeSup!=-1)
            printf("Suppression éffectué. \n");
        else
            printf("Erreur, la suppression ne peut pas s'effectuer.\n");
    }
    if (choix =='n')
        {
            code=fRecherche(ref,n);
            if (code!=-1)
            {
                printf("\nReference:\tQuantite:\tPrix:\tSeuil de securite:\n");
                printf("%d\t\t%d\t\t%.2f\t\t%d\n" ,ref[code], qt[code], prix[code], sds[code]);
            }           
            else 
                printf("La réference n'existe pas\n");
        }

    if (choix =='t')
    {
        code==fAjouter(ref,qt,prix,sds,n);
        if(code==-1)
            printf("Erreur, l'ajout ne peut pas s'effectuer.\n");
        else
            printf("Ajout éffectué. \n");
    }
    if (choix =='m')
    {
        code=fModifier(ref,qt,prix,sds,n);
        if(code==-1)
            printf("Erreur, la modification ne peut pas s'effectuer.\n");
        else
            printf("Modification éffectué. \n");
    }
}

    
int main (void)
{

    globale();
    return 0;
}

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).

edit3:

heres examples of where the program fails:

before adding a value: https://imgur.com/XRbXvVl After: https://i.imgur.com/mRI1aJq.png (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) https://imgur.com/HJk1PaW

CodePudding user response:

One major problem: In the fAjouter function you have code like this

n=NbArticle();
nb=n prods;
for(i=n 1;i<nb;i  )
{
    printf("Reference: ");
    scanf("%d", &ref[i]);
    ...
}

The array ref have n elements, and in the loop you start its indexing with n 1.

Unless prods is equal to zero then i will start out of bounds of the array, and using it as an index will lead to undefined behavior.

Remember that array-size in C is fixed once the array is defined. C doesn't have dynamic arrays. If you need dynamic arrays whose size can be extended you need to use dynamic allocation with e.g. malloc and realloc.

CodePudding user response:

I solved my problem by creating multiple arrays that will take will take the new size and will take the older values and ask the user to enter the new remaining ones.

  void fVerifRef(int ref[],int ref2[], int n, int pos)
    {
        int j,val;
        val=ref2[pos];
        j=fRecherche2(ref,n,val);
        while(j!=-1)
        {
            printf("Reference deja presente.\n");
            printf("Veuillez réessayer: ");
            scanf("%d",& ref2[pos]);
            val=ref2[pos];
            j=fRecherche2(ref,n,val);
                if(j==-1)
                    break;
        }
    }
    int fAjouter(int ref[],int qt[],float prix[],int sds[],int n)
    {
        int i,nb,prods,code;
        printf("Combien de produits voulez vous ajouter:");
        scanf("%d",& prods);
        fVerifVal(&prods);
        n=NbArticle();
        nb=n prods;
        int ref2[nb],qt2[nb],sds2[nb];
        float prix2[nb];
        for(i=0;i<n;i  )
        {
            ref2[i]=ref[i];
            qt2[i]=qt[i];
            prix2[i]=prix[i];
            sds2[i]=sds[i];
        }
        for (i=n; i<nb; i  )
        {
            printf("reference %d : ", i);
            scanf("%d", &ref2[i]);
            fVerifRef(ref,ref2,n,i);
            /*fVerif(ref2,i);*/
            printf("quantité %d : ", i);
            scanf("%d", &qt2[i]);
            fVerif(qt2,i);
            printf("Prix %d : ", i);
            scanf("%f", &prix2[i]);
            fVerifReal(prix2,i);
            printf("seuil de sécurité %d : ", i);
            scanf("%d", &sds2[i]);
            fVerif(sds2,i);
        }
        fEnregNombre(nb);
        code=fEnreg(ref2,qt2,prix2,sds2,nb);
        if(code==-1)
            return -1;
    }
  •  Tags:  
  • c
  • Related