Fonction str et val en pascal

oh9007 Messages postés 41 Date d'inscription   Statut Membre Dernière intervention   -  
 oh9007 -
Bonjour,Aidez moi svp !!

je trouve pas l'erreur !!!(val)!
svp comment utiliser la fonction val ?
program exe;
uses wincrt;
var ch,chi,chp:string; i,nb,x:integer;
begin
write('donner un nombre entier svp :');
read(nb);
str (nb,ch);
for i:=1 to length(ch) do
begin
x:=val(ch[i]);
if x mod 2 = 0 do
chp:=chp+ch[i] else
chi:=chi+ch[i];
end;
writeln('les nombre paires ',chp);
writeln('les nombres impaires ',chi);
readln;readln;
end.
A voir également:

1 réponse

KX Messages postés 16761 Date d'inscription   Statut Modérateur Dernière intervention   3 020
 
Tu ne peux pas écrire x:=val(ch[i]); la bonne syntaxe est en fait val(ch[i],x);
Après tu auras également une erreur avec le if, quand tu écris do en fait c'est un then qu'il faut...
Sinon ton programme marche...

Petite astuce au passage, pour tester si un nombre est impair il existe la fonction odd :
if odd(x) then chi:=chi+ch[i]+' '
          else chp:=chp+ch[i]+' ';
0
oh9007
 
merci mais la bonne repense est :

program exe;
uses wincrt;
var ch,chi,chp:string; i,nb,x:integer;
begin
write(chr(7));{BIP!}
write(#7);{Re-BIP!}
write('donner un nombre entier svp :');
read(nb);
str (nb,ch);
chp:=''; chi:='';
for i:=1 to length(ch) do
begin
val(ch[i],nb,x);
if nb mod 2 = 0 then
chp:=chp+ch[i] else
chi:=chi+ch[i];
end;
writeln('les nombre paires ',chp);
writeln('les nombres impaires ',chi);
write(chr(7));{BIP!}
write(#7);{Re-BIP!}
write(chr(7));{BIP!}
write(#7);{Re-BIP!}
readln;readln;
end.
0