Howdy, Stranger!

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

Categories

whats wrong with this?

francisstokesfrancisstokes Member Posts: 43
I only started programming with pascal yesterday, and was trying to make a simple password protected program. The program checks to see if a password file has been made, and if it hasn't, makes the user register a password. I there is already a password file, it asks the user for the password and should run the main program. If the password is incorrect, it displays an 'incorrect password' message and ends the program. I've been de-bugging it for about an hour, but the program only runs for a second and then ends. help!

[code]
program average;

uses crt;

const

noofints = 5;
s = 'pw.txt';

var

F : text; {F & s variables for reading the password file}


passinput : string;
sum : integer;
a,b,c,d,e : integer;
avr : real;
setpass : string;

begin {main program code}

assign(f, s);
reset(f);
if IOResult = 0 then
begin
writeln ('Please enter the password...'); readln(passinput);

if ('passinput' = 'F') then
begin
clrscr;
writeln ('This is a program to work out the average of five numbers.');
writeln ('please enter a value for number 1.'); readln (a);
writeln ('please enter a value for number 2.'); readln (b);
writeln ('please enter a value for number 3.'); readln (c);
writeln ('please enter a value for number 4.'); readln (d);
writeln ('please enter a value for number 5.'); readln (e);

sum := a + b + c + d + e;
avr := sum / noofints;

writeln ('No.1 is: ', a);
writeln ('No.2 is: ', b);
writeln ('No.3 is: ', c);
writeln ('No.4 is: ', d);
writeln ('No.5 is: ', e);

writeln ('The sum of the numbers is ', sum);
writeln ('The average of the numbers is ', avr);

readln;


end

else

writeln ('incorrect password');
readln;


end

else

writeln ('You have yet to set a password. Please correctly type a password of');
writeln ('your choice...'); readln (setpass);
assign (f, s);
rewrite (f);
writeln (setpass);
close (f);


end. {end of program code}
[/code]

Comments

  • PP2005PP2005 Member Posts: 111
    : [code]
    : if ('passinput' = 'F') then
    : [/code]

    As far as I can figure out, the above snippet will always return false. You're comparing two strings - not two variables. Anyhow, you need to first read the password from the password file into a variable, and then compare that variable with the password the user entered.
  • francisstokesfrancisstokes Member Posts: 43
    : : [code]
    : : if ('passinput' = 'F') then
    : : [/code]
    :
    : As far as I can figure out, the above snippet will always return false. You're comparing two strings - not two variables. Anyhow, you need to first read the password from the password file into a variable, and then compare that variable with the password the user entered.
    :

    Im sorry, im still very new to this, but isnt a string a variable?
  • zibadianzibadian Member Posts: 6,349
    : : : [code]
    : : : if ('passinput' = 'F') then
    : : : [/code]
    : :
    : : As far as I can figure out, the above snippet will always return false. You're comparing two strings - not two variables. Anyhow, you need to first read the password from the password file into a variable, and then compare that variable with the password the user entered.
    : :
    :
    : Im sorry, im still very new to this, but isnt a string a variable?
    :
    If you place something between '', then it is called a ard-coded string. It cannot change except when you recompile the program. A hard-coded string value is located directly into the code, where it appears. Identifiers (such as variable names) are never surrounded by ''.
  • francisstokesfrancisstokes Member Posts: 43
    I actualy thought that would probably happen, but it was returning an error, something about an operator (equals to be exact), so i tried with the quotations.

  • Phat NatPhat Nat Member Posts: 757
    [code]
    program average;

    uses crt;

    const

    noofints = 5;
    s = 'pw.txt';

    var

    F : text; {F & s variables for reading the password file}


    passinput : string;
    sum : integer;
    a,b,c,d,e : integer;
    avr : real;
    setpass : string;

    begin {main program code}
    assign(f, s);
    reset(f);
    if IOResult = 0 then
    begin
    writeln ('Please enter the password...'); readln(passinput);

    if ('passinput' = 'F') then
    begin
    clrscr;
    writeln ('This is a program to work out the average of five numbers.');
    writeln ('please enter a value for number 1.'); readln (a);
    writeln ('please enter a value for number 2.'); readln (b);
    writeln ('please enter a value for number 3.'); readln (c);
    writeln ('please enter a value for number 4.'); readln (d);
    writeln ('please enter a value for number 5.'); readln (e);

    sum := a + b + c + d + e;
    avr := sum / noofints;

    writeln ('No.1 is: ', a);
    writeln ('No.2 is: ', b);
    writeln ('No.3 is: ', c);
    writeln ('No.4 is: ', d);
    writeln ('No.5 is: ', e);

    writeln ('The sum of the numbers is ', sum);
    writeln ('The average of the numbers is ', avr);
    readln;
    end
    else
    writeln ('incorrect password');
    readln;
    end
    else
    writeln ('You have yet to set a password. Please correctly type a password of');
    [b]
    writeln ('your choice...'); readln (setpass);
    assign (f, s);
    rewrite (f);
    writeln (setpass);
    close (f);
    [/b]
    end. {end of program code}
    [/code]

    Okay, first off, if we indent to make BEGINs & ENDs match, we see that the bolded area at the end always runs. we only want it to run as part of the ELSE statement. So we should add in a BEGIN/END pair.
    [code]
    end
    else
    begin
    writeln ('You have yet to set a password. Please correctly type a password of');
    writeln ('your choice...'); readln (setpass);
    assign (f, s);
    rewrite (f);
    writeln (setpass);
    close (f);
    end;
    end. {end of program code}
    [/code]

    Also, about your comparing line: without the quatations, you are comparing a FILE to a STRING. this isn't acceptable. what you need to do is read a line from the file and the compare the STRING to the line (STRING) read from the file.
    You can read the line from the file after it is open like this:
    [code]
    ReadLn(F,MyLine);
    [/code]
    where MYLINE is defined as a STRING. then you can just compare MYLINE to PASSINPUT (no quotations).

    Lastly, in order for the IOResult = 0 line to work properly, you need to make sure you have IO checking disabled. This can be done like so:
    [code]
    assign(f, s);
    [b]{$I-} { turns off IO checking } [/b]
    reset(f);
    [b]{$I+} { turns IO checking back on} [/b]
    if IOResult = 0 then
    begin
    [/code]

    without turning off IO checking, the program will halt at the RESET(F); if the file doesn't exist.

    Good luck, you code looks cleaner than alot of people who have been at it a while. Remember to keep your code clean and you'll have alot less problems.

    Phat Nat

Sign In or Register to comment.