Hi,
I would need some help to resolve a problem.
I need to create a programm that contain 10 basic math questions.
After 3 fails in a row, the programm must stop. After each question the good answer should show if the student entered the wrong answer, and the result up to now will show.
I hope I explained myself well, english is not my fist language. :-)

So to start, I wrote a programm to ask the first question, then verify the answer, show the result and the good answer if needed. But it was really long and I was only repeating lots of commands.

So I am now trying something else. I would like create a loop, so each question will be asked one by one, the answer would be verified and the result would show.
I created two vector (not sure this is the correct word), one where I assigned the questions and another where I assigned the answer.

PROGRAM travail3q2version2;

{$APPTYPE CONSOLE}

uses Forms;

TYPE
//this is where the questions will be
tab_qsts= Array [1..10] of string[6];

//this is where the answers will be
tab_rep= Array [1..10] of integer;

CONST
//this is the quantity of questions
qst_max = 10;

VAR
reponse: char;
//will be use later on
cumulb, cumulm, cumulr, consb, consm, interup: integer;
a: tab_qsts;
b: tab_rep;
i,j,r: integer;


BEGIN

//will be use later on
cumulb:=0; cumulm:=0; cumulr:=0; consb:=0; consm:=0;

//here I assign the questions in the first vector
a[1]:='1+1'; a[2]:='1+2'; a[3]:='1+3'; a[4]:='1+4'; a[5]:='1+5';
a[6]:='2+1'; a[7]:='2+2'; a[8]:='2+3'; a[9]:='2+4'; a[10]:='2+5';

//here I assign the answers in the second vector
b[1]:=2; b[2]:=3; b[3]:=4; b[4]:=5; b[5]:=6;
b[6]:=3; b[7]:=4; b[8]:=5; b[9]:=6; b[10]:=7;


For i:= 1 to 10 do
For j:= 1 to 10 do

begin
writeln(a);
readln (r);

if (r)=(b[j]) then
begin
writeln('bravo');
end
end

END.


So, I'm only at the first step of the programm. I was expecting to see the first question, then the student to enter the first answer, if fine then "bravo' would had showed, if not the next question would had showed and so on. Then I was planning to add the other command I need to have the test calculate how many failure in a row and other stuf I need to do. But I cant even get the first part to work properly. The way the programm is now, each questions are asked 10 times, but each time with the following answer. So the first question is asked, then the first answer is expected, after the first question is asked again, and the second answer is expected, and so on.
If I erase the "For j:= 1 to 10 do" command, then each question is asked only once, but the answer is not verified.
Can someone tell me what is wrong?
zourha

Hi,

I compiled with the following code and it worked:

For i:= 1 to 10 do
begin
     writeln(a[i]);
     readln (r);

    if (r)=(b[i]) then  
        begin
           writeln('bravo');
       end
end

Thanks Lonelul,
you're awesome. This resolved my issue.
Now I will be able to continue.

(*my program is one alternative*)
(*
1,
create a program that contain 10 basic math questions.
*)
program main;

uses crt;

(*new types section*)
type questions = record
                       q:string;{the question}
                       a:string;{the answer}
                       r:boolean;{if answer is good then set to true,else false}
                 end;
     
     Aquestion = array[1..10]of questions;{array that contains the ten questions}


     
(*global variables section*)
var my_array:Aquestion;{one array}
    user:string;{the user answer}
    i:byte;{loop variable,counts only}
    wrong_answers:byte;{gathers the wrong answers}
    good_answers:byte;{stores the good answers}


(*with this function we'll check two strings
 if the two strings aren't equal then returns false else true,
 cause them equals*)
function Equal(v1,v2:string):boolean;
begin
     if v1 <> v2 then Equal:=false
     else Equal:=true;
end;


begin (*here begins the main program*)

      clrscr;

      wrong_answers:=0;{we set it to zero}
      good_answers:=0; {this one too}

      writeln('Fill the array.');
      (*ok,let's fill the array with questions*)
      for i:=1 to 10 do begin
          writeln('----------');
          write('Question ',i,' ',': '); {add your question }
          readln(my_array[i].q);         {store it}
          write('Answer   ',i,' ',': ');   {add the good answer too}
          readln(my_array[i].a);         {store the answer too}
          my_array[i].r:=false;          {later we'll treat it}
      end;

(*
2,
after 3 fails in a row,the program must stop
"so we have to listen the value of wrong_answers
and if it reaches 10 or not,if yes then game is over"
"here we can use a repeat..until loop"
*)
      clrscr;

      i:=0;  {set i to zero}

      repeat
            inc(i,1);{same as i:=i+1;}
            writeln('Question ',i,': ',my_array[i].q); {write the i.question}
            write('Answer ',i,': ');
            readln(user);    {catch the answer from the user}
            my_array[i].r:=Equal(my_array[i].a,user);{call our function to set result}
            if my_array[i].r then begin  {if the user's answer good then}
               inc(good_answers,1); {increase the good answers by one}
               write('Bravo! ',good_answers,' good answers.'); {write the screen}
               readln;
            end else begin
               inc(wrong_answers,1);
               write('Sorry...',wrong_answers,' wrong answer entered.');
               readln;
            end;
            clrscr;
      until (wrong_answers>=3)or(i=10);
      {write the final results to the screen...}
      clrscr;
      write('Results:');
      gotoxy(20,1);
      write('Good : ',good_answers);
      gotoxy(40,1);
      writeln('Wrong: ',wrong_answers);

      repeat
      until keypressed;

end.{of main}
{created by FlamingClaw 2010}

Thanks Lonelul,
you're awesome. This resolved my issue.
Now I will be able to continue.

If someone solved your problem then please mark as solved.Thank you, in the name of the community....

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.