Recent

Author Topic: How to make this code repeat?  (Read 2766 times)

PascalUserInNeedOfHelp

  • Newbie
  • Posts: 1
How to make this code repeat?
« on: December 02, 2015, 01:32:14 am »
Hi I'm tasked to make a converter I got the conversions and I need to repeat the menu once again instead of killing it.
So I need to repeat the codes starting from the repeat to the, writeln('Tempature Conversion Program'); , up  to
writeln ('Converted temperature is: ', tconverted: 8:2, ' degrees ', scale);
readln;

How do I do it?
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   crt,
  7.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  8.   cthreads,
  9.   {$ENDIF}{$ENDIF}
  10.   Classes
  11.   { you can add units after this };
  12. var a, tconverted: real;
  13.       f, scale: char;
  14.  
  15.  
  16.  
  17.  
  18. begin
  19.   Repeat
  20.    clrscr;
  21.   writeln('Tempature Conversion Program');
  22.   writeln;
  23.   writeln('1. Convert Celsius to Fahrenheit');
  24.   writeln('2. Convert Fahrenheit to Celsius');
  25.   writeln('3. Convert Celsius to Kelvin');
  26.   writeln('4. Convert Kelvin to Celsius');
  27.   writeln('5. Convert Kelvin to Fahrenheit');
  28.   writeln('6. Convert Fahrenheit to Kelvin');
  29.   writeln;
  30.   writeln('7. Press 7 to exit');
  31.   writeln;
  32.   writeln ('Enter a number for temperature conversion ');
  33.   readln (a);
  34.   writeln ('Enter a number to convert from 1-6 or press 7 to exit');
  35.   readln (f);
  36.   if f in ['1'] then
  37.     begin;
  38.       tconverted := a * 9 / 5 + 32;
  39.       scale:='F';
  40.     end;
  41.  
  42.     if f in ['2'] then
  43.     begin;
  44.         tconverted:= (a - 32) * 5 / 9;
  45.         scale:='C';
  46.     end;
  47.  
  48.     if f in ['3'] then
  49.     begin
  50.       tconverted := a + 273.15;
  51.       scale:='K';
  52.     end;
  53.  
  54.     if f in ['4'] then
  55.     begin
  56.       tconverted := a - 273.15;
  57.       scale:='C';
  58.     end;
  59.  
  60.     if f in ['5'] then
  61.     begin
  62.       tconverted := a *  9/5 - 459.67;
  63.       scale:='F';
  64.     end;
  65.  
  66.     if f in ['6'] then
  67.     begin
  68.       tconverted :=  (a + 459.67) * 5 / 9;
  69.       scale:='K';
  70.     end;
  71.  
  72.  
  73.  
  74.   writeln ('Converted temperature is: ', tconverted: 8:2, ' degrees ', scale);
  75.   readln;
  76.  
  77.   until ;
  78.    clrscr;
  79.      repeat;
  80.  
  81.  
  82.  
  83.  
  84.  
  85. end.
« Last Edit: December 02, 2015, 01:37:56 am by PascalUserInNeedOfHelp »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to make this code repeat?
« Reply #1 on: December 02, 2015, 05:03:43 am »

userx-bw

  • Full Member
  • ***
  • Posts: 178
Re: How to make this code repeat?
« Reply #2 on: December 22, 2015, 03:12:11 am »

I'd change this into  a procedure or two, maybe a function that passes a val through the prams and a result,

Code: [Select]
  writeln('Tempature Conversion Program');
  writeln;

 
  writeln('1. Convert Celsius to Fahrenheit');
  writeln('2. Convert Fahrenheit to Celsius');
  writeln('3. Convert Celsius to Kelvin');
  writeln('4. Convert Kelvin to Celsius');
  writeln('5. Convert Kelvin to Fahrenheit');
  writeln('6. Convert Fahrenheit to Kelvin');
  writeln;
  writeln('7. Press 7 to exit');
  writeln;
  writeln ('Enter a number for temperature conversion ');
  readln (a);
  writeln ('Enter a number to convert from 1-6 or press 7 to exit');
  readln (f);

below:  case statment here, maybe inside of a procedure even.

Code: [Select]
  if f in ['1'] then
    begin;
      tconverted := a * 9 / 5 + 32;
      scale:='F';
    end;

    if f in ['2'] then
    begin;
        tconverted:= (a - 32) * 5 / 9;
        scale:='C';
    end;

    if f in ['3'] then
    begin
      tconverted := a + 273.15;
      scale:='K';
    end;

    if f in ['4'] then
    begin
      tconverted := a - 273.15;
      scale:='C';
    end;

    if f in ['5'] then
    begin
      tconverted := a *  9/5 - 459.67;
      scale:='F';
    end;

    if f in ['6'] then
    begin
      tconverted :=  (a + 459.67) * 5 / 9;
      scale:='K';
    end;


procedure here, or keep it a writeln, or put this into that procedure above too , there is a few different things you could do with this line of code.
Code: [Select]

  writeln ('Converted temperature is: ', tconverted: 8:2, ' degrees ', scale);
  readln;



then I'd wrap it inside of a loop of some type that you can break out of when user tells it to exit,

then only need to write about two or three lines of code in the body of program, inpress your teacher and get to sit at the head of the class ;) hehe
« Last Edit: December 22, 2015, 04:03:24 am by userx-bw »
My Finished Projects
https://sourceforge.net/projects/mhsetroot/
https://sourceforge.net/projects/gmhsetrootfreepascalfrontend/

HP Elitetbook 6930p Dual Core Intel vPro 2.66 gHz
VOID (Linux) 64bit

 

TinyPortal © 2005-2018