?
Solved

How would I modify this program

Posted on 2000-03-30
3
Medium Priority
?
243 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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