Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How would I modify this program

Posted on 2000-03-30
3
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 49

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 30 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
Are you looking for the options available for exporting EDB files to PST? You may be confused as they are different in different Exchange versions. Here, I will discuss some options available.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview

610 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