Link to home
Start Free TrialLog in
Avatar of kazooie21
kazooie21

asked on

How would I modify this program

How would I change this program so it will output BOTH the number of capitals gotten in 1 guess and the number gotten in 4 guesses?

Here it is:

var state: string;
    capital : string;
    Statefile: text;
    tries: integer;
    guess: string;
    correct_guess: integer;


procedure GETDATA (var localStatefile: text;
                   var localstate: string;
                   var localcapital: string);
   begin
      readln (localStatefile, localstate);
      readln (localStatefile, localcapital);
   end; {GETDATA}


procedure GUESS_CAPITAL (var tries: integer;
                         guess: string;
                         var localcorrect_guess: integer);

   begin{GUESS_CAPITAL}
      tries := 0;
     repeat
       tries := tries + 1;
       write ('Give capital of ', state, ' ' );
       readln (guess)
     until (guess = capital) or (tries = 4);
      if guess = capital then
         writeln ('Nice work. You got it on try ', tries);
         inc(localcorrect_guess);
      else
          begin{until}
           writeln ('You did not get it in 4 tries or less');
           writeln ('The correct answer is ' , capital)
          end;{until}
   end; {GUESS_CAPITAL}

procedure GUESSES (localcorrect_guess: integer);
   begin
     writeln ('You have guessed ' ,localcorrect_guess, ' states in 4 or fewer tries ');
   end;{GUESSES}


begin {main}
     assign (Statefile, 'A:\state2.dat');
     reset (Statefile);
     correct_guess:= 0;
     while not seekeof (Statefile) do
       begin
         GETDATA (Statefile, state, capital);
         GUESS_CAPITAL (tries, guess, correct_guess);
         GUESSES (correct_guess)
       end;
     close (Statefile)
end.
Avatar of dbrunton
dbrunton
Flag of New Zealand image

Can you give some examples of the output you require because it is not clear enough?   Once you do that the rest is easy.
ASKER CERTIFIED SOLUTION
Avatar of My name is Mud
My name is Mud

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of My name is Mud
My name is Mud

By The WAY, Kazzoie... REMEMBER THIS IS WRONG

    if guess = capital then
      writeln ('Nice work. You got it on try ', tries);
      inc(localcorrect_guess);
    else
      begin{until}
        writeln ('You did not get it in 4 tries or less');
        writeln ('The correct answer is ' , capital)
      end;{until}

First in the
    if guess = capital then
      writeln ('Nice work. You got it on try ', tries);
      inc(localcorrect_guess);
The first Statement <writeln ('Nice work. You got it on try ', tries);> will only be executed, and in the statement <inc(localcorrect_guess);> you use a semicolon, those TWO statements are more than ONE statement, and when you want to execute more than ONE statement, that is TWO, THREE, ONE MILLION, ETC. You USE a Blocked statement, or whatever the name, like...

    BEGIN
       <First statement>; (* <--- Whatch the semicolon?? *)
       <Second Statement>; (* <--- Whatch the semicolon?? *)
       <...>; (* <--- Whatch the semicolon?? *)
       <One Millionth Statement> (* <-- Now here you use or Not use a semicolon, why 'cos is the END of the statement*)
    END;