#### Howdy, Stranger!

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

# whats wrong with this?

Member Posts: 43

[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

if ('passinput' = 'F') then
begin
clrscr;
writeln ('This is a program to work out the average of five numbers.');

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);

end

else

end

else

assign (f, s);
rewrite (f);
writeln (setpass);
close (f);

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

• 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.
• 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?
• 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 ''.
• 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.

• 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

if ('passinput' = 'F') then
begin
clrscr;
writeln ('This is a program to work out the average of five numbers.');

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);
end
else
end
else
[b]
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
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]
[/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