Fix this program - if exit trouble
Author |
Message |
silent_k
|
Posted: Fri Jun 03, 2005 12:37 pm Post subject: Fix this program - if exit trouble |
|
|
Ok, I am already done this program and it fully works, the only problem is that it is not suppose to exit if the first input by the user is -99(it should say the error message *invalid number......* .... -99 is like the end of file item - if the user inputs it - it stops the input of data). I cannot use functions or processes cause it is a class assignement and apparenlty there is very simple way to stop the error from occuring (according to my teacher). Here is my code - please help
code: |
%Declaring an array called count
var count : array 0 .. 10 of int
%Declaring other variables
var errorl : string
var num : int
%Beginning a procedure called invalid which shows up when user enters anything
%else other than a number from 0-10
procedure invalid
put "The value you have entered is either a letter or an invalid value."
put "Please re- enter a mark between 0-10"
end invalid %Ending the prodcedure
%Beginning a for loop
for number : 0 .. 10
%Initializing count to 0
count (number) := 0
end for
color (3)
locate (1, 1)
put "++++++++++++++++++++++++++"
locate (2, 1)
put "The Mark Frequency Program"
locate (3, 1)
put "++++++++++++++++++++++++++"
locate (5, 1)
put "This program asks the user to input a series of marks between 0 - 10."
locate (6, 1)
put "The program automatically stops the input of data once -99 is entered."
locate (7, 1)
put "The program then displays how many students got the same mark."
locate (9, 1)
put "Please enter the the marks. Input -99 to end the input of data."
locate (10, 1)
put "Press <ENTER> after each mark."
put ""
loop
%Beginning error trap
loop
get errorl : *
%Condition applied if user inputs a number between "0"-"9"
if errorl >= "0" and errorl <= "9" then
exit
elsif
%Condition applied if user inputs "-99"
errorl = "-99" then
exit
%Condition applied if user inputs a letter or word
elsif errorl >= "A" and errorl <= "z" then
invalid
%Condition applied if user inputs a number below "0"
elsif errorl < "0"
then
invalid
else
%Condition applied if user inputs anything else
invalid
end if
end loop
%Converting the information entered by user into integer
num := strint (errorl)
%Exit condition
if num = - 99 then
exit
end if
%Condition applied if the number entered is greater than 10
if num > 10 then
invalid
end if
%Using counters to count the number of times a same number was entered
if num = 0 then
count (0) := count (0) + 1
elsif num = 1 then
count (1) := count (1) + 1
elsif num = 2 then
count (2) := count (2) + 1
elsif num = 3 then
count (3) := count (3) + 1
elsif num = 4 then
count (4) := count (4) + 1
elsif num = 5 then
count (5) := count (5) + 1
elsif num = 6 then
count (6) := count (6) + 1
elsif num = 7 then
count (7) := count (7) + 1
elsif num = 8 then
count (8) := count (8) + 1
elsif num = 9 then
count (9) := count (9) + 1
elsif num = 10 then
count (10) := count (10) + 1
end if
end loop
delay (200)
cls
put "+++++++++++"
put "The Results"
put "+++++++++++"
put ""
for number1 : 0 .. 10
%Displaying how many students got the same marks
put count (number1), " student(s) recieved ", number1, "/10."
end for
put ""
colour(4)
put "Press F1 to rerun the program" |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
MysticVegeta
|
Posted: Fri Jun 03, 2005 3:31 pm Post subject: (No subject) |
|
|
code: |
%Declaring an array called count
var count : array 0 .. 10 of int
%Declaring other variables
var errorl : string
var num : int
var counter : int := 1
%Beginning a procedure called invalid which shows up when user enters anything
%else other than a number from 0-10
procedure invalid
put "The value you have entered is either a letter or an invalid value."
put "Please re- enter a mark between 0-10"
end invalid %Ending the prodcedure
%Beginning a for loop
for number : 0 .. 10
%Initializing count to 0
count (number) := 0
end for
color (3)
locate (1, 1)
put "++++++++++++++++++++++++++"
locate (2, 1)
put "The Mark Frequency Program"
locate (3, 1)
put "++++++++++++++++++++++++++"
locate (5, 1)
put "This program asks the user to input a series of marks between 0 - 10."
locate (6, 1)
put "The program automatically stops the input of data once -99 is entered."
locate (7, 1)
put "The program then displays how many students got the same mark."
locate (9, 1)
put "Please enter the the marks. Input -99 to end the input of data."
locate (10, 1)
put "Press <ENTER> after each mark."
put ""
loop
%Beginning error trap
loop
get errorl : *
%Condition applied if user inputs a number between "0"-"9"
if errorl >= "0" and errorl <= "9" then
exit
elsif
%Condition applied if user inputs "-99"
errorl = "-99" then
exit
%Condition applied if user inputs a letter or word
elsif errorl >= "A" and errorl <= "z" then
invalid
%Condition applied if user inputs a number below "0"
elsif errorl < "0"
then
invalid
else
%Condition applied if user inputs anything else
invalid
end if
end loop
%Converting the information entered by user into integer
num := strint (errorl)
%Exit condition
if num = -99 and counter not= 1 then
exit
elsif num = -99 and counter = 1 then
put "Invalid number..."
delay (2000)
end if
%Condition applied if the number entered is greater than 10
if num > 10 then
invalid
end if
%Using counters to count the number of times a same number was entered
if num = 0 then
count (0) := count (0) + 1
elsif num = 1 then
count (1) := count (1) + 1
elsif num = 2 then
count (2) := count (2) + 1
elsif num = 3 then
count (3) := count (3) + 1
elsif num = 4 then
count (4) := count (4) + 1
elsif num = 5 then
count (5) := count (5) + 1
elsif num = 6 then
count (6) := count (6) + 1
elsif num = 7 then
count (7) := count (7) + 1
elsif num = 8 then
count (8) := count (8) + 1
elsif num = 9 then
count (9) := count (9) + 1
elsif num = 10 then
count (10) := count (10) + 1
end if
counter += 1
end loop
delay (200)
cls
put "+++++++++++"
put "The Results"
put "+++++++++++"
put ""
for number1 : 0 .. 10
%Displaying how many students got the same marks
put count (number1), " student(s) recieved ", number1, "/10."
end for
put ""
colour (4)
put "Press F1 to rerun the program"
|
Like this? |
|
|
|
|
|
silent_k
|
Posted: Sat Jun 04, 2005 7:44 am Post subject: Just a question |
|
|
Thank youy so much . Just one question: what does the counter+= 1 do, we weren't taught the += function so that why I am just curious |
|
|
|
|
|
MysticVegeta
|
Posted: Sat Jun 04, 2005 8:55 am Post subject: (No subject) |
|
|
well,
is the same as
code: | counter:= counter + 1 |
just reduces the spaces, and they can be used with any operators
same for * and /
and its not nescessary to have 1 there
like you can have
code: | counter := counter + num |
reduces spaces
same as
|
|
|
|
|
|
silent_k
|
Posted: Sat Jun 04, 2005 1:16 pm Post subject: (No subject) |
|
|
oh ok, got it! thnks a lot once again! |
|
|
|
|
|
|
|