2016-07-28 3 views
0

Je rencontre un problème avec mon code. Cela commence bien mais quand vous demandez à l'utilisateur de saisir l'entrée au début, il ne passera pas sur l'instruction if. Comment puis-je réparer cela. J'ai essayé des tas pour contourner ceci et finalement l'ai eu pour lire des données mais continuellement dire que c'est invalide.Code ne bouge pas Lire

PROGRAM AT3 (input, output); 
uses crt, math; 
CONST 
    band6 = 90; 
    band5 = 80; 
    band4 = 70; 
    band3 = 60; 
    band2 = 50; 
VAR 
    Studname : array of string; 
    studmark : array of integer; 
    flag : boolean; 
    studinfo : text; 
    input : string; 
    count : integer; 
    num : integer; 
     input2: integer; 
    highmark, lowmark : integer; 
    median, average : integer; 

BEGIN 
    lowmark := 100; 
    highmark := 0; 
    median := 0; 
    ASSIGN (Studinfo, 'ExamResults.txt'); 
    flag := false; 
    WRITELN('welcome to the Band generator.'); 
     WRITELN('To enter student results, please enter the number of students. To see class statistics, please type zzz. To clear the screen type clear screen. And to exit the program, type exit'); 
    While flag = false DO 
     ReadLN (input); 
     IF input = 'zzz' THEN 
      WHILE not EOF(Studinfo) DO 
      BEGIN 
       WRITELN(studinfo); 
      END; 
     IF input = 'exit' THEN 
      Flag := true; 
       IF input = 'clear screen' THEN 
         CLRSCR 
       ELSE 
        if input2 <> 0 THEN 
         num := input2 
       ELSE 
       WRITELN('Please enter a valid number.'); 
     FOR count := 0 to num-1 DO 
      BEGIN 
      WRITELN('Please enter name of student', count); 
      read(studname[count]); 
      WRITE(studinfo, studname[count]); 
      WRITELN('Please enter mark of student', count, 'out of 100 (nearest whole number)'); 
      read(studmark[count]);    write(studinfo, studmark[count]); 
      IF studmark[count] >=band6 THEN 
       WRITELN(studinfo, 'band6'); 
      IF studmark[count] >=band5 THEN 
       WRITELN(studinfo, 'band5'); 
      IF studmark[count] >=band4 THEN 
       WRITELN(studinfo, 'band4'); 
      IF studmark[count] >=band3 THEN 
       WRITELN(studinfo, 'band3'); 
      IF studmark[count] >=band2 THEN 
       WRITELN(studinfo, 'band2'); 
      IF studmark[count] <band2 THEN 
       WRITELN(studinfo, 'band1'); 
      IF studmark[count] >= highmark THEN 
       highmark := studmark[count]; 
      IF studmark[count] <= lowmark THEN 
       lowmark := studmark[count]; 
      END; 
     median := highmark MOD 2; 

    CLOSE(studinfo); 
END. 
+0

"il ne bougera pas sur l'instruction if" qui 'déclaration if' voulez-vous dire exactement? BTW, il existe plusieurs autres problèmes avec votre code, par exemple. votre "Assign (Studinfo, 'ExamResults.txt')" doit être suivi immédiatement de "Rewrite (Studinfo)". Aussi, quelle version de Pascal utilisez-vous? – MartynA

+0

Hmmm ... ce code ressemble astucieusement au code écrit par l'utilisateur ** LeCarloVC ** (puis supprimé à nouveau), hier. Il a également eu les mêmes problèmes et cette chose «si marque/groupe». Aussi utilisé les mots-clés majuscules (la plupart du temps) et même cette '' zzz '' = fin de l'idée de programme. –

+0

L'indentation seule ne définit pas les blocs en Pascal. Vous avez besoin de 'begin' et' end' pour les blocs multi-instructions. – lurker

Répondre

2

Des questions comme celles-ci attirent souvent un commentaire du type "SO n'est pas un service de devoirs-faire ". Ce q est un peu exceptionnel car il n'y a pas de manière polie que je puisse penser à pour le décrire. Il a tous les signes d'avoir été écrit par quelqu'un qui ne sait pas vraiment ce qu'ils font - mais ne désespérez pas, nous étions tous débutants une fois! Je ne vais pas réécrire tout votre code pour vous - parce que vous n'apprendrez rien de tout cela - mais ce qui suit devrait au moins donner une boucle principale fonctionnelle que vous pouvez ensuite compléter et embellir si nécessaire .

La principale chose que j'ai fixée est la séquence d'actions dans la boucle - le original était une confusion complète, pour le dire légèrement. J'ai ajouté plusieurs commentaires entre accolades {}. Il y a un style de commentaires plus récent qui utilise // mais je ne sais pas quelle est la saveur de Pascal que vous utilisez.

code:

const 
    band6 = 90; 
    band5 = 80; 
    band4 = 70; 
    band3 = 60; 
    band2 = 50; 
    ArraySize = 20; { to set explicit array sizes } 
var 
    studname : array[1..ArraySize] of string; 
    studmark : array[1..ArraySize] of integer; 
    flag : boolean; 
    studinfo : text; 
    kbdinput : string; { to avoid name clash with program Input param} 
    count : integer; 
    num : integer; 
    {input2: integer; not used} 
    highmark, 
    lowmark : integer; 
    median, average : integer; 

begin 
    lowmark := 100; 
    highmark := 0; 
    median := 0; 
    assign (studinfo, 'c:\temp\examresults.txt'); { always use full path for file name } 
    Rewrite(studinfo); { set studinfo in the state to allow writing to the file} 

    flag := false; 
    writeln('welcome to the band generator.'); 
     writeln('to enter student results, please enter the number of students when prompted.'); 
    while flag = false do 
    begin 
     write('please enter a valid number of students. '); 
     readln(num); 
     for count := 1 to num do { not change of start and stop values} 
     begin 
      write('please enter name of student #', count, ' followed by [Enter] '); 
      readln(studname[count]); 
      write(studinfo, studname[count]); 

      write('please enter mark of student #', count, ' out of 100 (nearest whole number) followed by [Enter] '); 
      readln(studmark[count]); 
      write(studinfo, studmark[count]); 
     end; 
{ 
      if studmark[count] >=band6 then 
       writeln(studinfo, 'band6'); 
      if studmark[count] >=band5 then 
       writeln(studinfo, 'band5'); 
      if studmark[count] >=band4 then 
       writeln(studinfo, 'band4'); 
      if studmark[count] >=band3 then 
       writeln(studinfo, 'band3'); 
      if studmark[count] >=band2 then 
       writeln(studinfo, 'band2'); 
      if studmark[count] <band2 then 
       writeln(studinfo, 'band1'); 
      if studmark[count] >= highmark then 
       highmark := studmark[count]; 
      if studmark[count] <= lowmark then 
       lowmark := studmark[count]; 
      end; 
     median := highmark mod 2; 
} 

     writeln('to see class statistics, please type zzz. to clear the screen type zzz and to exit the program, type exit'); 
     readln (kbdinput); 
     if kbdinput = 'zzz' then 
     { The following does nothing useful 
      while not eof(studinfo) do 
      begin 
       writeln(studinfo); 
      end; 
     } 
     ; 
     if kbdinput = 'exit' then 
      flag := true 
     else 
     if kbdinput = 'clear screen' then 
      {clrscr;'} 

     close(studinfo); 
    end; 
end. 
1

Regardez votre ligne:

While flag = false DO 
     ReadLN (input); 

Flag est jamais faux il lit donc pour toujours. Vous rencontrez des problèmes avec vos blocs de début/fin.

+0

'Flag' est toujours' false', vous voulez dire. Mais 'if input = 'exit'' est défini sur true. Cela devrait arrêter la boucle. –

+0

@RudyVelthuis Dans le code affiché de l'OP, le 'si exit = 'exit'' n'est pas dans la portée du corps' while'. Le corps 'while' manque le' begin'/'end'. – lurker

+1

@lurker: Ah oui, une autre erreur dans le code. Trop de choses pour que le code ait du sens. –