How do I?? Please Read this!!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

How do I?? Please Read this!!!!

Hi



I think I have pretty much finished my source code for my second assignment. It is a menu driven program. I need a little help with one thing.

In module #2 the user is to type in a number.



If the user does not type in a number the program stops.

I need to constuct a invalid portion and I am not sure what I am doing wrong .



This is what I am typing in



program Primenumber;

uses crt;





var

n, trialdiv : integer;

prime : boolean;





begin

clrscr;

write ('enter a number:');

readln (n);

If (n < integer) THEN

begin

writeln('You have not selected a number');

writeln('Please try again');

PRIMENUMBER



this comes out as expected "(" at the THEN



Please if anyone can look this this over it would be great as it is due this afternoon.



Robyn

[email protected]



Thank you very much




Comments

  • Try reading in a string and then test whether the string is an integer


  • : Try reading in a string and then test whether the string is an integer

    :



    Steve,

    Thank you for replying.



    If I am reading in a string what would I do different in my readln.



    writeln('type in a number');

    readln(N)



    would it be that I would declare the variable (n) as a string instead of a integer?



    And how would I test a string to see if it is a integer?



    Thanks again

    Robyn


  • You can declare N as a string type.



    Note that there is a pascal function called "val" which converts a string type to its numeric value. The function is something like this



    val(str : string, var int, code : integer);



    the code returns zero if the operation is successful, so maybe you can do something like this:



    var

    N : string;

    number, code : integer;



    begin

    writeln('Enter a number');

    readln(N);

    val(N, number, code);



    if code <> 0 then writeln('Invalid integer')

    else writeln('You have entered an integer');



    end;


Sign In or Register to comment.