Solved

How would I modify this program

Posted on 2000-03-30
3
235 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:kazooie21
  • 2
3 Comments
 
LVL 48

Expert Comment

by:dbrunton
ID: 2672005
Can you give some examples of the output you require because it is not clear enough?   Once you do that the rest is easy.
0
 
LVL 6

Accepted Solution

by:
My name is Mud earned 10 total points
ID: 2672206
------------------------ Try this: -----------------------------
var
  wrongguesses: Array[1..4] Of String;
  state: string;
  Capital : string;
  Statefile: text;
  I,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);
    wrongguesses[tries]:=guess;
  until (guess = capital) or (tries = 4);
  if guess = capital then
    Begin
      writeln ('Nice work. You got it on try ', tries);
      inc(localcorrect_guess);
    End
  else
    begin{until}
      writeln ('You did not get it in 4 tries or less');
      WriteLn('Your wrong guesses were: ');
      For I:=1 to 4 do
        WriteLn(WrongGuesses[Tries]);
      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, 'C: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.
0
 
LVL 6

Expert Comment

by:My name is Mud
ID: 2672218
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;
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
This problem is more common than not and I will show you some things to check to solve this problem.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question