i try to compile this programme and thasn't work :p
i think that the problem come from the string fonction (int r=strcmp( CL[j].nomC , cl);)

#include <stdio.h>
#include <conio.h>
#include <string.h>

struct etudiant{
    char nom[7];
    char prenom[7];
    float not1;
    float not2;
    float not3;
    float moy;
};

/*struct branche{
    char nom[10];
    int nbr_module;
};*/

struct classe{
    char nomC;
    int annC;

};



int m=0,nbrE;

void creClass(){ int n=0;
m=n+1;
      classe CL[m];
      for(int j=0;j<n+1;j++){

   printf("donnez le nom de la classe\n");
    scanf("%s",&CL[j].nomC);
   printf("donnez l'anne de la classe");
    scanf("%d",&CL[j].annC);
    printf("donnez le nombre d'etudiants\n");
    scanf("%d",&nbrE);
    etudiant etu[nbrE];
    for(int i=0;i<nbrE;i++){

 printf("donnez le nom de l'etudiant %d\n",i+1);
  gets(etu[i].nom);
 printf("donnez le prenom de l'etudiant %d\n",i+1);
  gets(etu[i].prenom);
 printf("donnez les trois notes de l'etudiant %d\n",i+1);
  scanf("%f%f%f",&etu[i].not1,&etu[i].not2,&etu[i].not3);
  etu[i].moy=(etu[i].not1+etu[i].not2+etu[i].not3)/3;
 printf("la moyenne est : %f\n",etu[i].moy);  

  }

}}

void chercherE(){
             char nom[7];
             char *cl;
             int an;
            classe CL[m]; 
            etudiant etu[nbrE]; 
    printf("donnez le nom de l'etudiant a chercher\n");
     gets(nom);
    printf("donnez la classe de l'etudiant a chercher\n");
     scanf("%s",&cl); 
    printf("donnez l'annee de l'etudiant a chercher\n");
     scanf("%d",&an);
    for(int j=0;j<m;j++){
        int r=strcmp( CL[j].nomC , cl);
        if(CL[j].annC==an&&r==0){
            for(int i=0;i<nbrE;i++){
        int r1=strcmp(etu[i].nom, nom ) ;
                if(r1==0){
                    printf(" l'etudiant %s %s a comme moyenne: %f",etu[i].nom,etu[i].prenom,etu[i].moy);
                }
            }
        }
    } 

}

void menu(){ int z;
    printf("1-creer une classe\n  2-chercher un etudiant\n");
  printf("faites votre chois");
  scanf("%d",&z);
  if(z==1){
    creClass();
  }else{ if(z==2){
    chercherE();
      }
  }
}

int main(){
    menu();
    getch();
    return 0;
}

i need help plz ;

// compiling error;
  C:\Users\kira\Desktop\Projet 1.cpp    In function 'void chercherE()':
  70    32  C:\Users\kira\Desktop\Projet 1.cpp  [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
  4     0   C:\Users\kira\Desktop\Projet 1.cpp   In file included from C:\Users\kira\Desktop\Projet 1.cpp
  53    15  C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h  [Note] initializing argument 1 of 'int strcmp(const char*, const char*)'

Recommended Answers

All 2 Replies

The function strcmp takes two parameters. Both of them have to be char*. Pointers to char. In this case:
strcmp( CL[j].nomC , cl),
cl is a char*, so that's fine, but CL[j].nomC is not a char*. Is it a single char.

What you're trying to do here could never work. Never. Because the function strcmp compares c-style strings. A c-style string is an array of char, with a zero on the end. A single char is not an array of char with a zero on the end.

If you have a single char (such as CL[j].nomC), and you want to check if is the same as another single char, just use ==.

Also, see this code?

char *cl;
... 
printf("donnez la classe de l'etudiant a chercher\n");
scanf("%s",&cl); 

This is very wrong indeed. Completely wrong. cl is a pointer, pointing to some random memory somewhere. Because you never set cl to anything, it's random. Then, you're trying to write to that random memory with scanf. You're trying to write over random memory. Writing over some random piece of memory somewhere is a very, very bad idea. It is wrong. Do not write over random memory.

This is in the C++ forum; if you're coding in C++, just use proper C++ strings.

If you can handle more advice, don't use conio.h, don't use gets, don't use Dev-Cpp unless you're using version 5 or later

If you can handle more ...

and if your spec's are to compile with a C++ compiler,

(but to restrict the 'io' to C style),

then ...

this example ...

may give you some more ideas to get you started with a working shell.

// entreEtudiant.cpp //

/*
    The presumption used here ...
    is that you wish to compile with a C++ compiler
    BUT ...
    restrict the io here to the C style ...
*/


#include <cstdio>
#include <cstring>


const int NOM_BUF_LEN = 8;
const int NOTS_NUM = 3;
const int NOM_NUM_MAX = 2; // keep small while testing //

const char* MENU = "1-Entre un etudiant  2-Print  3-Chercher un etudiant  4-Quit\n"
                   "Faites votre chois:  ";

// 2 utilities to ease input here ... //
char* fixedFgets( char* s, size_t bufSize, FILE* fin )
{
    if( fgets( s, bufSize, fin ) )
    {
        char *p = strchr( s, '\n' ),
             c ;
        if( p ) *p = 0; /* strip off '\n' at end ... IF it exists */
        else while( (c = fgetc( fin )) != '\n'  &&  c != EOF ) ; /* flush... */
        return s;
    }
    /*  else ... if reach here ... */
    return NULL;
}
int entreChr( const char* msg )
{
    printf( msg ); fflush( stdout );
    char chr = getchar();
    if( chr != '\n' ) while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}


// a C++ struct or class with all public //
struct Etudiant
{
    char nom[NOM_BUF_LEN];
    char prenom[NOM_BUF_LEN];
    float nots[NOTS_NUM];

    float moy() const
    {
        float sum = 0.0;
        for( int i = 0; i < NOTS_NUM; ++ i ) sum += nots[i];
        return sum/NOTS_NUM;
    }

    void print( FILE* fp ) const
    {
        fprintf( fp, "%s %s", nom, prenom );
        for( int i = 0; i < NOTS_NUM; ++ i )
            fprintf( fp, " %f", nots[i] );
        fprintf( fp, ", la moyenne est: %f\n", moy() );
    }

    // i is the passed in size //
    int entre( int i )
    {
        char buf[100];
        if( i < NOM_NUM_MAX )
        {
            printf( "Donnez le nom de l'etudiant (%d)    : ", i+1 );
            fixedFgets( nom, NOM_BUF_LEN, stdin );

            printf( "Donnez le prenom de l'etudiant (%d) : ", i+1 );
            fixedFgets( prenom, NOM_BUF_LEN, stdin );

            while( true )
            {
                printf( "Donnez les trois notes de l'etudiant (%d) : ", i+1 );
                fixedFgets( buf, sizeof buf, stdin );
                if( sscanf( buf, "%f %f %f", &nots[0], &nots[1], &nots[2] ) == 3 )
                    break;
                // if reach here ...
                printf( "Encore ... mais seulment 3 int's ... \n" );
            }
            return i+1;
        }
        // else ... if reach here ... //
        printf( "Full ... enlarge array and recompile and rerun ... \n" );
        return i;
    }
} ;



void  menuChoisEtc( Etudiant aryEtuds[] )
{
    Etudiant et;
    bool done = false;
    int size = 0, newsize = 0;
    while( !done )
    {
        switch( entreChr( MENU ) )
        {
            case '1': newsize = et.entre( size );
            if( newsize == size+1 ) // add at end of ary //
            {
                aryEtuds[size] = et;
                size = newsize;
            }
            break;

            case '2':
            for( int i = 0; i < size; ++ i )
                 aryEtuds[i].print( stdout );
            break;

            //case '3': chercher(); break; :
            case '4' : done = true; break;

            default: printf( "Not implemented yet ...\n" );
        }
    }
}



int main()
{
    Etudiant myEtuds[NOM_NUM_MAX];

    menuChoisEtc( myEtuds );
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.