Encadre un texte en c/c++

Résolu
lil_adriano Messages postés 42 Date d'inscription vendredi 26 janvier 2007 Statut Membre Dernière intervention 16 juin 2008 - 18 nov. 2007 à 03:52
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007 - 19 nov. 2007 à 12:42
Slt tout le monde
je debute en C/C++ je voudrai faire cadre  pour un mot mais j'y arrive pas.
j'ai essayer de faire un mais on peu pas faire un mot corret.

voici mon code source

#include <stdio.h>

main()
{
      int i,tabl,j,larg;
     
tabl=37;
larg=9;

for (int i=0; i < tabl;i++)
{
    printf("#");
}
printf("\n");

for(int i=0; i<larg;i++)
{
        printf("|");
        for(int j = 0; j < i ; j++) //
    {
        printf( " ");
    }
   
   
    for(int j = 0; j < tabl - 2 - i; j++) //
    {
        printf( " ");
    }printf( "|\n");
}

for (int i=0; i < tabl;i++)
{
    printf("#");
}
printf("\n");
scanf("%d",&tabl);

return 0;
}

j'utilise devc++.
j'espere que vous apporterai une aide a mon probleme. merci

10 réponses

yann_lo_san Messages postés 1137 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 23 janvier 2016 26
18 nov. 2007 à 18:18
Voici une fonction permettant d'encadrer un texte de longueur variable sur la console.
Les constantes veulent dire
cadre haut gauche
cadre bas gauche
ect...



#include


<stdio.h>




#include



<tchar.h>




#include



<stdlib.h>






static





const
unsigned
char CHG =201;



static



const
unsigned
char CBG =200;



static



const
unsigned
char CHD =187;



static



const
unsigned
char CBD =188;



static



const
unsigned
char LHB =205;



static



const
unsigned
char LL =186;


// FONCTION ENCADRE TEXTE





void




EncadreTexte(char* texte,bool tab


)

{
   



int lg =strlen(texte);
   


char
* str1 =newchar[(lg+1)+12];
   

str1[lg+12]='\0';



    memset
(str1,'.', lg);
   


_strset
(str1, LHB);

   


if
(tab)printf("\t");
   


printf
(
"%c%s%c \n"
, CHG, str1, CHD);
   


if
(tab)printf("\t");
   


printf
(
"%c %s %c \n"
, LL, texte, LL);
   


if
(tab)printf("\t");
   


printf
(
"%c%s%c \n\n"
, CBG, str1, CBD);
   



    if
(str1)delete[] str1


;

}





// MAIN





int





_tmain
(
int argc,_TCHAR* argv


[])

{
   


EncadreTexte("UN TEXTE A ENCADRER", true);
   


return
0



;
}
3
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
18 nov. 2007 à 18:30
Salut

quelque petit pb sur ton prog

tu redeclare a chaque fois un nouvel int i dans les boucle for (inutile) et ne passe pas sur un compilateur gcc.

ci dessous un morceau de code qui passe en utilisant ta methede de boucle for mais tu devrait regarder les chaines de format  du printf ca allegerais le code

#include <stdio.h>

main()
{
int i,tabl,j,larg,len;
    
tabl=37;
larg=9;

char chaine[25];
printf ("entrez la chaine a ecrire (max 24 char) :");
scanf ("%24s",&chaine),
len=strlen(chaine);

printf ("entrer le nb de colonne : ");
scanf("%d",&tabl);
if (tabl <len+2)
    tabl=len+2;

for (i=0; i < tabl;i++)
   printf("#");
printf("\n");

for(i=0; i<larg/2;i++)
    {
        printf("|");
        for(j = 0; j < tabl-2 ; j++) //
            printf( " ");
    printf( "|\n");
    }

printf("|");
for (i=1;i<(tabl-len)/2;i++)
    printf (" ");
printf(chaine);
for (j=i+len;j<tabl-1;j++)
    printf(" ");
printf("|\n");

for(i=0; i<larg/2;i++)
    {
        printf("|");
        for(j = 0; j < tabl-2 ; j++) //
            printf( " ");
    printf( "|\n");
    }

for (i=0; i < tabl;i++)
    printf("#");
printf("\n");
//

return 0;
}
 
3
lil_adriano Messages postés 42 Date d'inscription vendredi 26 janvier 2007 Statut Membre Dernière intervention 16 juin 2008
19 nov. 2007 à 01:55
Merci de m'avoir repondu aussi vite. mais dans vos codes je suis confronter a un probleme c'est strlen ca me dit:
   `strlen' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)

je tenterai d'arranger ce petit probleme sinon je fais appel a vous. Merci encore.
0
lil_adriano Messages postés 42 Date d'inscription vendredi 26 janvier 2007 Statut Membre Dernière intervention 16 juin 2008
19 nov. 2007 à 02:03
Heu re salut c'est encore moi yann_lo_san la  j'ai un probleme avec ton code sources ca me dis:


  In function `void EncadreTexte(char*, bool)':
`strlen' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
`newchar' undeclared (first use this function) 
`memset' undeclared (first use this function) 
`_strset' undeclared (first use this function)

donc tu pourra me dire comment resoudre se probleme?  je vais essayer de mon cote mais ta lumiere ne serai pas de refus.

logiciel utilisé: devc++ peut etre ca pourra aide.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
19 nov. 2007 à 08:45
Salut lil_adriano

Jái essaye le code ussi mais ce n'est pas de l'ANSI C Je lái modifie mais la je suis au boulot ;
Je te renvoie ca a midi ou  ce soir .
 Le fait que tu ne vois pas strlen est etrange ca fait partie sdu C standard.
( táurais pas des option de compilation C++ ??? ....... )
0
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
19 nov. 2007 à 09:03
pour le probleme de strlen ca devrait etre resolu en rajoutant un

#include < string.h>  ou
#include < Cstring.h>  

good luck
0
yann_lo_san Messages postés 1137 Date d'inscription lundi 17 novembre 2003 Statut Membre Dernière intervention 23 janvier 2016 26
19 nov. 2007 à 09:12
#include <string> devrait résoudre le problème.

Pour newchar c'est un espace manquant, c'est bien sur :
new char
0
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
19 nov. 2007 à 12:14
Salut lil la version de yann passee en C ANSI tu devrais pouvoir la compiler sans trop de pb.
NB : j'ai rajoute 2 parametre a la fonction pour choisir le nb d'espace qui apparaisse entre le text et l'encadrement ( sp et li)

-----------------------------

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

static const unsigned char LL ='|';

// FONCTION ENCADRE TEXTE
// Encadre le text texte avec sp espace sur la ligne et li ligne en haut et en bas

void
EncadreTexte(char* texte,int sp,int li )
{
int lg =strlen(texte)+2*sp;
int i;
char * str1 =( char*)malloc(lg+3);
char *str2=( char*)malloc(sp);
char * str3 =( char*)malloc(lg+3);
str1[lg]='\0';
str2[sp]='\0';
memset (str1,'#', lg+2);
memset (str2,' ', sp);
memset (str3,' ', lg+2);

str3[0]=LL;str3[lg+1]=LL;str3[lg+2]='\n';
printf ( "%s \n" , str1);
for (i=0;i</string.h></stdio.h>
0
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
19 nov. 2007 à 12:40
je renvoie c'est passe bizar

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

static const unsigned char LL ='|';

// FONCTION ENCADRE TEXTE
// Encadre le text texte avec sp espace sur la ligne et li ligne en haut et en bas

void
EncadreTexte(char* texte,int sp,int li )
{
int lg =strlen(texte)+2*sp;
int i;
char * str1 =( char*)malloc(lg+3);
char *str2=( char*)malloc(sp);
char * str3 =( char*)malloc(lg+3);
str1[lg]='\0';
str2[sp]='\0';
memset (str1,'#', lg+2);
memset (str2,' ', sp);
memset (str3,' ', lg+2);

str3[0]=LL;str3[lg+1]=LL;str3[lg+2]='\n';
printf ( "%s \n" , str1);
for (i=0;i<li;i++)
printf(str3);
printf ( "%c%s%s%s%c \n" , LL,str2, texte,str2, LL);
for (i=0;i<li;i++)
printf(str3);
printf ( "%s \n\n" , str1);

free(str1) ;
}

// MAIN

int
main ( int argc,char* argv [])
{

EncadreTexte("BINGO",5,2);
return 0 ;
}
0
titof3812 Messages postés 18 Date d'inscription lundi 12 novembre 2007 Statut Membre Dernière intervention 12 décembre 2007
19 nov. 2007 à 12:42
oups pour que ca soit propre
rajouter
free (str2);
free (str3);
dans la fonction
0
Rejoignez-nous