tags:

views:

203

answers:

3

When I try to execute my program, I get an error. I can't read the last value in "ch":

int choi(char *Tvide[48])//permet de choisir la place selon les choix de l utilisateur
{
    char fum, classe, pos;

    printf("\n S.V.P choisissez votre Classe   (A:1 ere classe )/(B: 2 eme classe): ");
    classe = getche();
    printf("\n Est ce que vous etes fumeur   (O:fumeur)/(N:non fumeur):");
    fum = getche();
    printf("\n S.V.P vous preferez s''assoir pres de la fenetre ou du couloir(C:couloir )/(F:fenetre):");
    pos=getche();

    int Tfum[24] = {3,4,7,8,11,12,15,16,19,20,23,24,27,28,31,32,35,36,39,40,43,44,47,48};
    int Tnfum[24] = {1,2,5,6,9,10,13,14,17,18,21,22,25,26,29,30,33,34,37,38,41,42,45,46};
    int Tfen[24] = {1,4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33,36,37,40,41,44,45,48};
    int Tcol[24] = {2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35,38,39,42,43,46,47};
    int k;

    char Tdispo[48];

    for(k=1; k<=48; k++) { Tdispo[k]='o'; } // on met içi le tableau des places vides

    if (classe=='A')
    {
     for(k=9;k<=48;k++) { Tdispo[k]='n'; }
    }
    if (classe=='B')
    {
     for(k=1;k<=9;k++) { Tdispo[k]='n'; } 
    }
    if (fum=='O')
    {
     for(k=1;k<=24;k++) { Tdispo[Tnfum[k]]='n'; }
    }
    if (fum=='N')
    {
     for(k=1;k<=24;k++) { Tdispo[Tfum[k]]='n'; } 
    }
    if (pos=='C')
    {
     for(k=1;k<=24;k++) { Tdispo[Tfen[k]]='n'; }
    }
    if (pos=='F')
    {
     for(k=1; k<=24; k++) { Tdispo[Tcol[k]]='n'; }
    }

    int s;

    printf("Les places disponibles sont:");

    for(s=1; s<=48; s++)
    {
     if(Tdispo[s] == 'o') { printf("%d",s,"~"); }
    }

    int ch;
    printf("\n S.V.P introduire votre choix :");
    scanf("%d",ch);

    Tvide[ch]=='n';
    int ch1 = ch;
    return ch1;
}

What could be causing this behavior?

+6  A: 

You should have

scanf("%d",&ch);

not

scanf("%d",ch);

Because the result of scanf gets stored in the variable named ch.

Mark Rushakoff
A: 

yes yes , I did wot u told me , ... but it does n t work yet ... try if u didn t trust me !

T_Z
So why did you select his answer as "accepted"?
Jason
Also, this should be posted as a comment ('add comment') not an answer. Also, build with warnings so your compiler (at least, if you have a good compiler) will tell you about errors like the one @Mark pointed out
bdonlan
A: 

Your main problem is that C uses 0-based arrays. A array of size 24 iss accessed 0 to 23) but you access them from 1 to 24, thus the last access is outside the array.

peje