my program will not work and i don't no y and its stressing me out so much
it says i need a semi-colon on the line until menu ='4' but it still says that wen i put one on
program task2a;
uses crt;
var
password, userpass, newpass1, newpass2 : string[10];
flag, count, n, add, times, total : integer;
answer, menu : char;
begin
clrscr;
password := 'computer';
flag := 0;
repeat
clrscr;
writeln('please enter the password');
readln(userpass);
if userpass = password then
begin
writeln('the password you entered is correct');
readln;
clrscr;
repeat
clrscr;
writeln('1 - change password');
writeln('2 - add several numbers');
writeln('3 - multiply several numbers');
writeln('4 - exit');
readln(menu);
case menu of
'1' : begin
writeln('do you wish to change ur password? y/n');
readln(answer);
if (answer = 'y') or (answer = 'Y') then
begin
repeat
clrscr;
writeln('please enter your new password');
readln(newpass1);
writeln('please enter your new password');
readln(newpass2);
clrscr;
if newpass1 = newpass2 then
begin
writeln(' your new password has been set');
readln;
end
else
begin
writeln('the two passwords do not match');
writeln;
writeln('please try again');
readln;
end;
until newpass1 = newpass2
end
else
begin
writeln('your password has remained the same');
readln;
end;
end;
'2' : begin
writeln('how many numbers do you wish to add?');
readln(n);
for count := 1 to n do
begin
clrscr;
writeln('please enter a number');
readln(add);
total := total + add;
writeln('the total so far is ',total);
end;
writeln('the total is ',total);
end;
'3' : begin
writeln('how many numbers do you wish to multiply?');
readln(n);
for count := 1 to n do
begin
clrscr;
writeln('please enter a number');
readln(times);
total := total * times;
writeln('the total so far is ',total);
end;
writeln('the total is ',total);
end;
'4' : begin
writeln(' you have chosen to exit');
readln;
end;
else
begin
writeln('this is not a valid option');
readln;
end;
until menu = '4'
end;
else
begin
flag := flag + 1;
writeln('the password you entered is incorrect');
writeln('you have used ', flag,' out of 3 tries');
readln;
end;
until (flag = 3) or (userpass = password);
if flag = 3 then
begin
writeln('you have used up all of your attempts');
writeln;
end;
writeln('the program will now end');
readln;
end. writeln('you have used up all of your attempts');
writeln;
end;
writeln('the program will now end');
readln;
end.
I KNOW I AM BEING SELFISH,
BUT MAYBE I NEED TO BE.
last cut: 13th January, 18th January, 5th febuaury, 18th febuary, 16th march, 20th march, 29th may 2008.
Sorry been ages since I used pascal, but if I remember right a ; before end is optional, so chances are its being fussy and the problem isn't where it thinks it is
best to add the ; to the end of every line anyway just to rule out a missing one higher up that should be optional but isn't (also good practice for if you move onto c or java based languages) obviously don't add to the end of lines that aren't supposed to have them or you'll possibly find unexpected results
Also make sure your begin and ends all match up
another thing worth trying is to enclose the condition in () this just makes sure the compiler sees what you want it to see so "until (menu = '4');"
Sorry can't actually see anything obvious, it all looks fine to me
I don't know pascal so I don't know if this is wrong or not, but on the last line you put a full stop after 'end' instead of a semicolon.
Also, "end. writeln('you have used up all of your attempts');" looks like it could be wrong, since all your other "end"s are on their own line.
Ahhhh...pascal..the language that is fantastic if you don't want to do any input, output or processing. Anyhow, I have got you program to compile (see below). There seems to be a a few bugs in it - if you want me to take a look at them let me know.
* deleted code block - see next post *
Last edited by HandThatFeeds : 07-10-2008 at 07:04 PM.
Reason: removed part of the message
I was rushing before and missed a bit out of the last post. I went to add it and then couldn't resist having a bit of a play. Here are the changes:
It was missing a second end before 'until (menu = '4')'
There was an 'end.' Before 'writeln('you have'.
Moved the 'if flag = 3' test inside the else block otherwise it displays on exit
Removed the duplicate 'writeln('the pr' statements at the end
Added readln statements to the calculations so you can see the result
Added 'total := 0' to the calculation code to initialise the value
Added a condition to the multiply calculation so that the first number entered will not be multiplied or else the result will always be zero and moved the 'total so far' into the statement into the if condition (no point in displaying it on the first loop iteration).
Just thought that I would add a couple of words of wisdom:
I know that it is tedious but add comments like 'case 1:...end {end case 1}' as this will make your code far easier to read and debug.
Never ever try to write a whole program in one go as it just leads to a world of pain. Compile constantly to make sure that there are no syntax errors and test after each bit of new functionality to make sure that it works as expected. Getting into the habit of doing this religiously will make your life a lot easier.
Here is the updated code….
Code:
program task2a;
uses crt;
var password, userpass, newpass1, newpass2 : string[10];
flag, count, n, add, times, total : integer;
answer, menu : char;
begin
clrscr;
password := 'computer';
flag := 0;
repeat
clrscr;
writeln('please enter the password');
readln(userpass);
if userpass = password then
begin
writeln('the password you entered is correct');
readln;
clrscr;
repeat
clrscr;
writeln('1 - change password');
writeln('2 - add several numbers');
writeln('3 - multiply several numbers');
writeln('4 - exit');
readln(menu);
case menu of
'1' : begin
writeln('do you wish to change your password? y/n');
readln(answer);
if (answer = 'y') or (answer = 'Y') then
begin
repeat
clrscr;
writeln('please enter your new password');
readln(newpass1);
writeln('please enter your new password');
readln(newpass2);
clrscr;
if newpass1 = newpass2 then
begin
writeln(' your new password has been set');
readln;
end
else
begin
writeln('the two passwords do not match');
writeln;
writeln('please try again');
readln;
end;
until (newpass1 = newpass2);
end
else
begin
writeln('your password has remained the same');
readln;
end
end;
'2' : begin
writeln('how many numbers do you wish to add?');
readln(n);
total := 0;
for count := 1 to n do
begin
clrscr;
writeln('please enter a number');
readln(add);
total := total + add;
writeln('the total so far is ',total);
readln;
end;
writeln('the total is ',total);
readln;
end;
'3' : begin
writeln('how many numbers do you wish to multiply?');
readln(n);
total := 0;
for count := 1 to n do
begin
clrscr;
writeln('please enter a number');
readln(times);
if( count = 1 ) then
begin
total := times;
writeln('the first number entered is ',total);
readln;
end
else
begin
total := total * times;
writeln('the total so far is ',total);
readln;
end;
end;
writeln('the total is ',total);
readln;
end;
'4' : begin
writeln(' you have chosen to exit');
readln;
end;
else begin
writeln('this is not a valid option');
readln;
end;
end;
until (menu = '4');
end
else
begin
flag := flag + 1;
writeln('the password you entered is incorrect');
writeln('you have used ', flag,' out of 3 tries');
readln;
if flag = 3 then
begin
writeln('you have used up all of your attempts');
writeln;
end;
end;
until (flag = 3) or (userpass = password);
writeln('the program will now end');
readln;
end.