1
Program q2;

Var input: integer ;
Var repeatvar: char ;

Procedure display(input : integer) ;

Begin
If (input = 9) then 
    Write ('********************************************************') ;
    Write ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
    Write ('********************************************************') ;
If (input = 8) then
    Write ('********************************************************') ;
    Write ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
    Write ('********************************************************') ;
If (input = 7) then
    Write ('********************************************************') ;
    Write ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
    Write ('********************************************************') ;
If (input = 6) then
    Write ('********************************************************') ;
    Write ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
    Write ('********************************************************') ;
If (input = 5) then
    Write ('********************************************************') ;
    Write ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
    Write ('********************************************************') ;
If (input = 4) then
    Write ('********************************************************') ;
    Write ('4! = 4 x 3 x 2 x 1 = 24 ');
    Write ('********************************************************') ;
If (input = 3) then
    Write ('********************************************************') ;
    Write ('3! = 3 x 2 x 1 = 6 ');
    Write ('********************************************************') ;
If (input = 2) then
    Write ('********************************************************') ;
    Write ('2! = 2 x 1 = 2 ');
    Write ('********************************************************') ;
If (input = 1) then
    Write ('********************************************************') ;
    Write ('1! = 1 = 1 ');
    Write ('********************************************************') ;
If (input = 0) then
    Write ('********************************************************') ;
    Write ('0! = 1 = 1 ');
    Write ('********************************************************') ;

End ;

Begin
While (repeatvar = 'y') do
Begin
Write('Enter an integer between 0 and 9 or -1 to quit');
readln(input);

If (input = -1) then
    begin
    Write('Sentinel value - Exiting program !');
    Readln;
    end
Else
    display(input);

End

End.

This is the warning I get when compiling it:

Free Pascal Compiler version 2.4.0 [2010/05/18] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling q2.pas
q2.pas(61,8) Warning: Variable "repeatvar" does not seem to be initialized
Linking q2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
78 lines compiled, 0.2 sec
1 warning(s) issued

I am typing in q2 but cannot see any output, it’s not doing anything. I have tried a number of things; also I am totally new to Pascal, and FPC – the FreePascal Compiler. Can anyone tell me what’s the problem?

In the part of the repeatvar, I wanted to implement this; I wanted to implement this C++ code:

do
{
    cout << "Enter an integer between 0 and 9 or -1 to quit => ";
    cin >> input ; // get number
    if(input == -1)
    {
        cout << "Sentinel value - Exiting program !" << endl;
        exit;
        system("pause");
        return 0;
    }
    else
    {
        // display function
        display(input);
    }
    
    cout << endl;
    cout << " Press y to repeat , any other key and enter to terminate: ";
    cin >> repeat;
    if(repeat != 'y')
        exit;
}while( repeat == 'y');
7
  • 2
    ASk your self this : What is the Value of repeatvar at this line the very first time you get to the line : While (repeatvar = 'y') do. Ask yourself what the test is checking for and what you're giving it. ie when is this line FIRST executed and when do you ask for input! Commented Jan 28, 2013 at 2:30
  • i wanted to implement this c++ code !! , the one i edited . please have a look @PreetSangha . I am relatively new to programming , so the logic is a bit wayward. Commented Jan 28, 2013 at 2:33
  • possible duplicate of c++ to pascal conversion of a beginner program. Do not post the same question again as a new one when your question is closed. Instead, improve the closed question and ask for it to be reopened. Reposting it as a duplicate is a violation of this site's guidelines. Commented Jan 28, 2013 at 2:36
  • ok , will keep that in mind . Am sorry about that. Am new to this site. thnx ! @KenWhite - how should I ask to reopen my previous question ? Commented Jan 28, 2013 at 2:40
  • 2
    Please do not edit your questions like that, because that makes existing comments/answers/suggestions simply nonsensical. If you have another question, please post it as a new question. Commented Jan 28, 2013 at 17:01

3 Answers 3

2

Because the repeatvar is never initialized, your program never enters the while loop, because there is no way for repeatvar to be 'y' to begin with.

Sign up to request clarification or add additional context in comments.

Comments

0

You forgot to initialize the variable

repeatvar := 'y'

insert the above line before the lines

While (repeatvar = 'y') do 
Begin

...

End

Comments

-1

i hope that the correction of code

program Project1;


Var input: integer ;
Var repeatvar: char ;



Procedure display(input : integer) ;
Begin
If (input = 9) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
  Writeln ('********************************************************') ;
  end;
If (input = 8) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
  Writeln ('********************************************************') ;
  end;
If (input = 7) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
  Writeln ('********************************************************') ;
  end;
If (input = 6) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
  Writeln ('********************************************************') ;
  end;
If (input = 5) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
  Writeln ('********************************************************') ;
  end;
If (input = 4) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('4! = 4 x 3 x 2 x 1 = 24 ');
  Writeln ('********************************************************') ;
  end;
If (input = 3) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('3! = 3 x 2 x 1 = 6 ');
  Writeln ('********************************************************') ;
  end;
If (input = 2) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('2! = 2 x 1 = 2 ');
  Writeln ('********************************************************') ;
  end;
If (input = 1) then
  begin
  Writeln ('********************************************************') ;
  Writeln ('1! = 1 = 1 ');
  Writeln ('********************************************************') ;
  end;
If (input = 0) then
begin
  Writeln ('********************************************************') ;
  Writeln ('0! = 1 = 1 ');
  Writeln ('********************************************************') ;
  end;
End ;


Begin
repeat
   writeln('enter number between 1-9 or -1 to quit: ');
   readln(input);
   display(input);
until input=-1;


End.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.