Thread: Help needed

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    1

    Help needed

    Hello im pretty new to programming in C and i can figure out what is wrong with my code.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    typedef struct voiture
     {
         char immat;
         char marque;
         char modele;
         int vitesseMax;
         char type;
     } t_voiture;
    
    
     typedef struct maillon
     {
         t_voiture voiture;
         struct maillon *suivant;
     }t_maillon;
    
    
    t_maillon* creerM ()
    {
        t_maillon *nouv;
        nouv = malloc(sizeof(t_maillon));
    
    
        printf("quelle immatriculation ?");
        scanf("%s",&nouv->voiture.immat);
    
    
        printf("quelle marque ?");
        scanf("%s",&nouv->voiture.marque);
    
    
        printf("quelle modele ?");
        scanf("%s",&nouv->voiture.modele);
    
    
        printf("quelle vitesse max ?");
        scanf("%d",&nouv->voiture.vitesseMax);
    
    
        printf("quel type ?");
        scanf("%s",&nouv->voiture.type);
    
    
        nouv->suivant = NULL;
    }
    
    
    void afficherImmat (t_maillon *ancre, char* marqueChoisie)
    {
        if (ancre!=NULL)
        {
            afficherImmat(ancre->suivant,marqueChoisie);
            if (strcmp(marqueChoisie,ancre->voiture.marque)==0)
            {
                afficherM(ancre);
            }
        }
        else
        {
            printf("Erreur, la liste est vide");
        }
    }
    
    
    void afficherVoit (t_maillon *ancre, t_maillon voiture)
    {
        printf("%c",ancre->voiture.immat);
        printf("%c",ancre->voiture.marque);
        printf("%c",ancre->voiture.modele);
        printf("%d",ancre->voiture.vitesseMax);
        printf("%c",ancre->voiture.type);
    }
    
    
    void supprimerImmat (t_maillon **pancre, char* immat)
    {
        int temp;
        t_maillon prec;
        t_maillon courant;
    
    
        if(*pancre==NULL)
        {
            printf("liste vide !");
        }
    
    
        else
        {
            if(strcmp((*pancre)->voiture.immat,immat)==0)   /// on supprime le premier maillon
            {
                temp=*pancre;   /// on mémorise l'adresse du premier
                *pancre=(*pancre)->suivant; /// l'ancre pointe sur le deuxieme
                free(temp); /// on libère le premier
            }
            else    /// il faut rechercher le maillon à la fin de la liste
            {
                prec = *pancre;
                courant = courant.suiv;
                while ((courant!=NULL)&&strcmp(courant->immat,immat)!=0))   /// on décale d'un le maillon
                {
                    prec = courant;
                    courant = courant->suiv;
                    /// quand on sort de la boucle
                    /// soit on est arrivés à la fin de la liste sans trouver
                    /// soit rien à faire
                }
                if (courant!=NULL)
                {
                    prec->suiv=courant->suiv;
                    free(courant);
                }
            }
        }
    }
    
    
    void sauvegarderLC(t_maillon *ancre, char* ficname)
    {
        FILE*fp;
        fp=fopen(ficname,"w");
        if(fp==NULL)
        {
            printf("erreur");
        }
        else
        {
            while(ancre!=NULL)
            {
                fprintf(fp,"%c",ancre->immat);
                fprintf(fp,"%c",ancre->marque);
                fprintf(fp,"%c",ancre->modele);
                fprintf(fp,"%d",ancre->vitesseMax);
                fprintf(fp,"%c",ancre->type);
                ancre=ancre->suiv;
            }
            fclose(fp);
        }
    }
    
    
    t_maillon *charger(char*ficname)
    {
        t_maillon *nouv;
        t_maillon *liste=NULL;
        FILE*fp;
        fp=fopen(ficname,"r");
        if(fp!=NULL)
        {
            while(!foef(fp))
            {
                nouv=(t_maillon*)malloc(sizeof(t_maillon));
                fscanf(fp,"%s",nouv->immat);
                fscanf(fp,"%s",nouv->marque);
                fscanf(fp,"%s",nouv->modele);
                fscanf(fp,"%d",nouv->vitesseMax);
                fscanf(fp,"%s",nouv->type);
                nouv->suiv=NULL;
                inserTete(&liste,nouv);
            }
            fclose(fp);
        }
        return liste;
    }
    
    
    int main()
    {
        int nbMaillon;
        int choix;
        int i;
        t_maillon *maillon;
    
    
        printf("Combien de maillon souhaitez-vous ?");
        scanf("%d",&nbMaillon);
    
    
        creerM ();
    
    
        do
        {
            printf("Que voulez-vous faire ?\n1 pour afficher les immatriculations à partir d'une marque\n");
            printf("2 pour afficher toute les voitures de la liste\n");
            printf("3 pour supprimer une voiture de la liste\n4 pour sauvegarder la liste chainee\n");
            printf("5 pour charger un fichier");
            scanf("%d",&choix)
        }
        while(choix!=1 || choix!=2 || choix!=3 || choix!=4 || choix!=5);
    
    
        switch(choix)
        {
        case 1: afficherImmat (ancre,marqueChoisie);
            break;
        case 2: afficherVoit (ancre,voiture);
            break;
        case 3: supprimerImmat (&pancre,immat);
            break;
        case 4: sauvegarderLC(ancre,ficname);
            break;
        case 5 : charger(ficname)
        }
    
    
        return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your code supposed to do, and how does it not work? For example, what is your test input, expected output, and actual output?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is what I get when I try to compile your code...
    Code:
    ||=== Build: Debug in AlgorithmTest (compiler: GNU GCC Compiler) ===|
    D:\Programming\AlgorithmTest\main.c||In function 'afficherImmat':|
    D:\Programming\AlgorithmTest\main.c|58|warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\string.h|43|note: expected 'const char *' but argument is of type 'char'|
    D:\Programming\AlgorithmTest\main.c|60|warning: implicit declaration of function 'afficherM' [-Wimplicit-function-declaration]|
    D:\Programming\AlgorithmTest\main.c||In function 'afficherVoit':|
    D:\Programming\AlgorithmTest\main.c|70|warning: unused parameter 'voiture' [-Wunused-parameter]|
    D:\Programming\AlgorithmTest\main.c||In function 'supprimerImmat':|
    D:\Programming\AlgorithmTest\main.c|95|warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\string.h|43|note: expected 'const char *' but argument is of type 'char'|
    D:\Programming\AlgorithmTest\main.c|97|warning: assignment makes integer from pointer without a cast [-Wint-conversion]|
    D:\Programming\AlgorithmTest\main.c|99|warning: passing argument 1 of 'free' makes pointer from integer without a cast [-Wint-conversion]|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|358|note: expected 'void *' but argument is of type 'int'|
    D:\Programming\AlgorithmTest\main.c|103|error: incompatible types when assigning to type 't_maillon {aka struct maillon}' from type 't_maillon * {aka struct maillon *}'|
    D:\Programming\AlgorithmTest\main.c|104|error: 't_maillon {aka struct maillon}' has no member named 'suiv'|
    D:\Programming\AlgorithmTest\main.c|105|error: invalid operands to binary != (have 't_maillon {aka struct maillon}' and 'void *')|
    D:\Programming\AlgorithmTest\main.c|105|error: invalid type argument of '->' (have 't_maillon {aka struct maillon}')|
    D:\Programming\AlgorithmTest\main.c|105|error: expected statement before ')' token|
    D:\Programming\AlgorithmTest\main.c|108|error: invalid type argument of '->' (have 't_maillon {aka struct maillon}')|
    D:\Programming\AlgorithmTest\main.c|113|error: invalid operands to binary != (have 't_maillon {aka struct maillon}' and 'void *')|
    D:\Programming\AlgorithmTest\main.c|115|error: invalid type argument of '->' (have 't_maillon {aka struct maillon}')|
    D:\Programming\AlgorithmTest\main.c|115|error: invalid type argument of '->' (have 't_maillon {aka struct maillon}')|
    D:\Programming\AlgorithmTest\main.c|116|error: incompatible type for argument 1 of 'free'|
    C:\Program Files (x86)\CodeBlocks\MinGW\include\stdlib.h|358|note: expected 'void *' but argument is of type 't_maillon {aka struct maillon}'|
    D:\Programming\AlgorithmTest\main.c||In function 'sauvegarderLC':|
    D:\Programming\AlgorithmTest\main.c|135|error: 't_maillon {aka struct maillon}' has no member named 'immat'|
    D:\Programming\AlgorithmTest\main.c|136|error: 't_maillon {aka struct maillon}' has no member named 'marque'|
    D:\Programming\AlgorithmTest\main.c|137|error: 't_maillon {aka struct maillon}' has no member named 'modele'|
    D:\Programming\AlgorithmTest\main.c|138|error: 't_maillon {aka struct maillon}' has no member named 'vitesseMax'|
    D:\Programming\AlgorithmTest\main.c|139|error: 't_maillon {aka struct maillon}' has no member named 'type'|
    D:\Programming\AlgorithmTest\main.c|140|error: 't_maillon {aka struct maillon}' has no member named 'suiv'|
    D:\Programming\AlgorithmTest\main.c||In function 'charger':|
    D:\Programming\AlgorithmTest\main.c|155|warning: implicit declaration of function 'foef' [-Wimplicit-function-declaration]|
    D:\Programming\AlgorithmTest\main.c|158|error: 't_maillon {aka struct maillon}' has no member named 'immat'|
    D:\Programming\AlgorithmTest\main.c|159|error: 't_maillon {aka struct maillon}' has no member named 'marque'|
    D:\Programming\AlgorithmTest\main.c|160|error: 't_maillon {aka struct maillon}' has no member named 'modele'|
    D:\Programming\AlgorithmTest\main.c|161|error: 't_maillon {aka struct maillon}' has no member named 'vitesseMax'|
    D:\Programming\AlgorithmTest\main.c|162|error: 't_maillon {aka struct maillon}' has no member named 'type'|
    D:\Programming\AlgorithmTest\main.c|163|error: 't_maillon {aka struct maillon}' has no member named 'suiv'|
    D:\Programming\AlgorithmTest\main.c|164|warning: implicit declaration of function 'inserTete' [-Wimplicit-function-declaration]|
    D:\Programming\AlgorithmTest\main.c||In function 'main':|
    D:\Programming\AlgorithmTest\main.c|194|error: expected ';' before '}' token|
    D:\Programming\AlgorithmTest\main.c|200|error: 'ancre' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|200|note: each undeclared identifier is reported only once for each function it appears in|
    D:\Programming\AlgorithmTest\main.c|200|error: 'marqueChoisie' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|202|error: 'voiture' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|204|error: 'pancre' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|204|error: 'immat' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|206|error: 'ficname' undeclared (first use in this function)|
    D:\Programming\AlgorithmTest\main.c|209|error: expected ';' before '}' token|
    D:\Programming\AlgorithmTest\main.c|177|warning: unused variable 'maillon' [-Wunused-variable]|
    D:\Programming\AlgorithmTest\main.c|176|warning: unused variable 'i' [-Wunused-variable]|
    D:\Programming\AlgorithmTest\main.c||In function 'creerM':|
    D:\Programming\AlgorithmTest\main.c|50|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build failed: 30 error(s), 11 warning(s) (0 minute(s), 1 second(s)) ===|
    Fact - Beethoven wrote his first symphony in C

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    "liste chainee" - I'm guessing that you are making a linked list, right?


    Let's look at some of the errors to get you started...

    Code:
    printf("quelle immatriculation ?");
        scanf("%s",&nouv->voiture.immat);
    "immat" is a char. For %s to work it will need to be an array of chars.

    It might be worth doing something like this...
    Code:
    #define MAX_STRING_SIZE 0xFF
    
    typedef struct voiture
     {
         char immat[MAX_STRING_SIZE];
         char marque[MAX_STRING_SIZE];
         char modele[MAX_STRING_SIZE];
         int vitesseMax;
         char type[MAX_STRING_SIZE];
     } t_voiture;
    "temp" is declared as an int instead of a pointer to a t_maillon
    Code:
    int temp;
    ...
    
    temp=*pancre;
    One of these are an instance of "t_maillon", one of them is a pointer to "t_maillon"
    Code:
    prec = *pancre;



    It seems that you typed all this code out in one go and then found that it had problems. That in itself is your biggest problem. The better way is to add one thing at a time and not move on until you have it working.

    I'd suggest commenting out most of your code (or making a new project) and starting with creating a list and getting that right. It looks like you are loading it from a file in charger, right?

    You may want to read FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com and FAQ > Casting malloc - Cprogramming.com
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c/c++ help needed
    By zensaaa in forum C Programming
    Replies: 2
    Last Post: 03-06-2011, 07:34 AM
  2. Needed help.
    By Denisius in forum C Programming
    Replies: 3
    Last Post: 01-19-2007, 08:05 PM
  3. Help needed.
    By Denisius in forum C Programming
    Replies: 10
    Last Post: 01-11-2007, 05:39 AM
  4. some help needed.
    By epidemic in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2007, 02:56 PM
  5. C++ help needed?
    By atif in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2002, 06:43 PM

Tags for this Thread