Why can’t I enter a new name after my while loop is finished? - average

Why can’t I enter a new name after my while loop is finished? The loop should ask for a new name and it should until terminated, but it doesn’t do that. Why?
I was trying to create a program to read the student name and ID for a student as well as finding their average and finally outputting the ID and name with the title they relieved as a result of their average.
program Students_of_ABC_Highschool;
var
ID:integer;
studentname:string;
Mathmark:integer;
ITmark:integer;
Englishmark:integer;
Chemistrymark:integer;
Spanishmark:integer;
Frenchmark:integer;
Averagemark:integer;
begin
Averagemark:= 0;
Mathmark:=0;
ITmark:=0;
Englishmark:=0;
Chemistrymark:=0;
Spanishmark:=0;
Frenchmark:=0;
while Averagemark <101 do begin
writeln (' Please enter a student name ');
read (studentname);
writeln ('Please enter students ID: ');
read (ID);
writeln ('Please enter Math mark ');
read (Mathmark);
writeln ('Please enter IT mark ');
read (ITmark);
writeln ('Please enter English mark ');
read (Englishmark);
writeln ('Please enter Chemistry mark ');
read (Chemistrymark);
writeln ('Please enter Spanish mark ');
read (Spanishmark);
writeln ('Please enter French mark ');
read (Frenchmark);
Averagemark:= (Mathmark + ITmark + Englishmark + Chemistrymark + Frenchmark +
Spanishmark) div 6;
writeln (' Average mark for student is ', Averagemark);
if Averagemark >=60 then begin
writeln ('Congratulations you are invited to the award ceremony ',studentname);
end
else
begin writeln ('You are encouraged to work harder ', studentname);
end;
end;
end.

You are employing read instead of readLn.
If you have a look into ISO standard 10206 “Extended Pascal”, you will find the following note:
e) If v is a variable‑access possessing a fixed‑string‑type of capacity c, read(f, v) shall satisfy the following requirements. [… super technical mumbo jumbo …]
NOTE — 6 If eoln(f) is initially true, then no characters are read, and the value of each component of v is a space.
Bottom line, you can “fix” your program by inserting the following conditional readLn which will “consume” any “remnant” end‑of‑line character prior your actual read:
writeln (' Please enter a student name ');
if EOLn then
begin
readLn
end;
read (studentname);
For the record: This is mostly a copy of an earlier answer of mine.

Related

character shows up as 'y' instead of a space '

In matlab i'm coding a Ceaser Cipher, but the space shows up as a 'y' character.
How can I replace that with a space
case 4
disp('Breaking Ceaser Cipher')
cs = menu('Please Enter your Choice','Encryption','Decryption');
if cs==1
c = input('Enter the message: ','s');
sh = str2double(input('Enter shift: ','s'));
c=upper(c);
lc=length(c);
for i=1:lc
p(i)=int16(c(i))-65+sh;
end
p=mod(p,26)+97;
p=char(p);
disp( p)
end
end
output example:
Breaking Ceaser Cipher
Enter the message:
my name is jeff
Enter shift:
5
rdysfrjynxyojkk
Here we see that the encryption is correct, but the space is being replaced by 'y'. It does not replace the character 'y' when used as an input, the space bar somehow comes out as a 'y'.
I'v also tried using p2 = regexprep(c, 'y', ' ') in order to replace the 'y' string with space.Also looked into isspace function. No luck
You are halfway there:
spaces=isspace(c)
% make array of spaces
out=blanks(size(c));
% get array without spaces
c=c(~spaces);
% do stuff to c, without spaces.
p=mod(p,26)+97;
p=char(p);
% Fill p in corresponding locations
out(~spaces)=p;

SEND EMAIL BASED ON CONDITIONS (SAS)

I found the below code (untouched) in this forum which is very close to what i was looking for however am having some issues when tweaking;
data Millenium_Falcon;
han='1';
luke='0';
darth='0';
run;
filename myemail EMAIL
to="me#lando.com"
cc="me#lando.com"
from="me#lando.com"
subject="Millenium Falcon"
importance="HIGH"
;
data _null_;
set Millenium_Falcon ;
file myemail;
IF (Luke = '1' and Darth = '0') then do;
put "Han,";
put " ";
put "Look out for the asteroids.";
put " ";
put "Thank you.";
put " ";
put "Obi";
end;
else do;
put '!EM_ABORT!';
end;
stop;
run;
Before tweaking, this code works fine however when i try to point to my data set (removing Millennium_Falcon step above) which just contains meta data from dictionary.tables (libname,memname,modate) and change the if statement to
IF (memname = 'TEST' and datepart(modate) = date()) then do;
the email does not send. It is almost like the data step (below) must be present (acting like datalines) for this to work.
data Millenium_Falcon;
han='1';
luke='0';
darth='0';
run;
Any help would be much appreciated.
Many thanks
Aaron
You probably want something more like this that will abort when no records meet the criteria and otherwise generate a list of the values that do.
data _null_;
file myemail;
if _n_=1 and eof then put '!EM_ABORT!';
set have end=eof ;
where (memname = 'TEST' and datepart(modate) = date()) ;
put memname= modate= ;
run;

Customizing data tips in editor

This is not exactly a question, because i have the answer, but as I couldn't find sources about it, i'll post the question along with the answer here in case it is of some interest for you. (And also to be able to find it again if i forget about how i did it).
I came across a problem while reviewing some code. The huge amount of variables were not explained in the code and everytime I wanted to know the meaning of a variable I had to look in the Data files and/or guess their meaning from the operations that were done in the code.
No need to say that this was pretty time consuming and i've searched the best way to lose less time.
First of all, I put all the explanations about the variables in comments, the problem being it's adding a (huge!) lot of lines in the script.
I'm also using the "Enable data tips in edit mode" in MATLAB. When you hover over a variable name MATLAB already evaluated, it gives you it's size along all dimensions and at least its first values, making it already easier to know what objects you are manipulating. (see image below)
The natural idea that came to my mind was the following : would it be possible to make MATLAB display some more custom informations in the data tips?
And the answer is yes!
See code in answer
We will need to do some preprocessing in order for this to work, i.e. :
1) Create a data file linking the variable names to their description
(This is the boring part, although i already had to do it in order to understand the code. Just add a line everytime you come across a new variable)
I chose to save these datas in a CSV file, where the first column contains the variable names and the second one contains the descriptions, e.g.
2) Edit MATLAB's function datatipinfo (It's inner code can be accessed by typing edit datatipinfo in MATLAB's command window)
The datatipinfo function looks like :
function datatipinfo(val)
% Some error checking / Initialization
function prefix=sizeType %#ok<DEFNU> All uses are in EVALC calls.
s = size(val);
D = numel(s);
if D == 2
theSize = [num2str(s(1)), 'x', num2str(s(2))];
elseif D == 3
theSize = [num2str(s(1)), 'x', num2str(s(2)), 'x', ...
num2str(s(3))];
else
theSize = [num2str(D) '-D'];
end
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val)];
end
end
% Some other stuff
end
And it's the prefix function that we'll edit in order to do what we want to do, along with some filescanning and string comparison in the initialization phase :
A) Initialization phase :
% Read data from csv file :
fid = fopen('ToyVars.csv');
Data = textscan(fid, '%s%s','whitespace','','delimiter',';');
fclose(fid);
B) Compare the name of the variable you're hovering on with the names of the variables in Data
NameFound=0;
% Get Variable Names and Corresponding comments
TmpNames=Data{1,1};
TmpComments=Data{1,2};
% Loop through TmpNames. If a Name matches, assign corresponding comment to the variable Comment
for ii=1:size(TmpNames,1)
if(isequal(char(TmpNames(ii))),name)
Comment=char(TmpComments(ii));
NameFound=1;
end
end
C) Add the comment in the datatip if NameFound==1
if NameFound
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val) ' : ' Comment];
else
prefix = [name ': empty ' theSize ' ' class(val) ' : ' Comment];
end
else
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val) ];
end
end
And voilà!
With a slight tweak and some modifications to the calling function you can also utilize #BillBokeey's answer without any external dependencies. By placing a structure array in the same workspace as the variable you are previewing, you can store a string in a field with the same name as your variable and utilize evalin with the existing logic in datatipinfo to get the inputs for #BillBokeey's modifications.
For my test case I am storing my strings in a structure named mydatastrings:
mydatastrings.test = 'Test Variable';
In the body of datatipinfo I have added a try block:
NameFound = 0;
try
Comment = evalin('caller', sprintf('mydatastrings.%s', name));
NameFound = 1;
end
Along with #BillBokey's modifications to the nested prefix function:
if NameFound
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val) ' : ' Comment];
else
prefix = [name ': empty ' theSize ' ' class(val) ' : ' Comment];
end
else
if isempty(val) == 0
prefix = [name ': ' theSize ' ' class(val)];
else
prefix = [name ': empty ' theSize ' ' class(val) ];
end
end
And we achieve the same results.
Full files are in this Gist
EDIT:
You can also make a minimal adjustment to datatipinfo to display a comment without modifying prefix. It seems like whatever executes datatipinfo also captures all of the outputs to the command window and displays them in the popup rather than the command window itself.
If we replace the previous try block with simply:
try
Comment = evalin('caller', sprintf('mydatastrings.%s', name))
end
And leave prefix as it is in the default MATLAB install, we obtain the following popup:
This is also included in the Gist as datatipinfo_noprefix.m
Another approach is to subclass a Matlab's already defined class such as double and add a string property. This string will appear on the data tip.
How to subclass with additional property:
http://fr.mathworks.com/help/matlab/matlab_oop/built-in-subclasses-with-properties.html
The result:

Formula won't display when certain fields are null

Similar to my first question. I want to show my address in a text box containing the fields as follows
{Company}
{AddLine1}
{AddLine2}
{ZIP}{State}{City}
{Country}
The line that concerns me (and hopefully some of you guys) is {ZIP} {City} {State}. What I want to produce is a consistent addressing format, so that there will be no blank space or indentation even if a ZIP, City or State field has been left blank in the DB. This line should still line up with the rest of the rows and not be indented. I also wish to insert commas between zip, state, city where they are relevant and leave them out where not. For this I have written a formula. Below:
If isnull({BILL_TO.ZIP}) or trim({BILL_TO.ZIP})= "" Then "" else {BILL_TO.ZIP}
+
(If isnull({BILL_TO.State}) or trim({BILL_TO.State})= "" Then ""
else(If not isnull({BILL_TO.ZIP}) and length(trim({BILL_TO.ZIP})) <> 0 Then ", " else "") + {BILL_TO.State})
+
(If isnull({BILL_TO.CITY}) or length(trim({BILL_TO.CITY})) = 0 Then ""
else(
If (not isnull({BILL_TO.State}) and length(trim({BILL_TO.State})) <> 0)
or
(not isnull({BILL_TO.Zip}) and length(trim({BILL_TO.Zip})) <> 0)
Then ", " else "")+ {BILL_TO.CITY}))
The problem is that when there is only a city (no state or zip entered) the formula itself will not display. It does however display when the others are present.
Can anyone see a bug in this code??? Its killing me
Thanks for the help.
Look forward to hearing from you guys!
There are a whole lot of if-then-elses going on in that formula so it's hard to tell, but if it's not displaying anything that probably means that you're using a field somewhere without handling its null condition first. Something simpler might be your best bet:
local stringvar output;
if not(isnull({Customer.Postal Code})) then output:=trim({Customer.Postal Code}) + ', ';
if not(isnull({Customer.Region})) then output:=output + trim({Customer.Region}) + ', ';
if not(isnull({Customer.City})) then output:=output + trim({Customer.City}) + ', ';
left(output,length(output)-2)
Create a formula field for each database field. For example:
// {#CITY}
If Isnull({BILL_TO.CITY}) Then
""
Else
Trim({BILL_TO.CITY})
// {#STATE}
If Isnull({BILL_TO.STATE}) Then
Space(2)
Else
Trim({BILL_TO.STATE})
// {#ZIP}
If Isnull({BILL_TO.ZIP}) Then
Space(5) //adjust to meet your needs
Else
Trim({BILL_TO.ZIP})
Embed each formula field in a text object.
Format text object and its fields to meet your need.
** edit **
I you have data-quality issues, address them in the STATE and ZIP formulas (because they will be a constant length). I would add the commas and spacing the text object.

Can't insert newline in msword form field using Powebuilder OLE

I have an application written in Powerbuilder 11.5 that automatically fills in form fields of a Word document (MS Word 2003).
The Word document is protected so only the form fields can be altered.
In the code below you can see I use char(10) + char(13) to insert a newline, however in the saved document all I see is 2 little squares where the characters should be.
I've also tried using "~r~n", this also just prints 2 squares.
When I fill in the form manually I can insert newlines as much as I want.
Is there anything else I can try? Or does anybody know of a different way to fill in word forms using Powerbuilder?
//1 Shipper
ls_value = ids_form_info.object.shipper_name[1]
if not isnull(ids_form_info.object.shipper_address2[1]) then
ls_value += char(10) + char(13) + ids_form_info.object.shipper_address2[1]
end if
if not isnull(ids_form_info.object.shipper_address4[1]) then
ls_value += char(10) + char(13) + ids_form_info.object.shipper_address4[1]
end if
if not isnull(ids_form_info.object.shipper_country[1]) then
ls_value += char(10) + char(13) + ids_form_info.object.shipper_country[1]
end if
if lnv_word.f_inserttextatbookmark( 'shipper', ls_value ) = -1 then return -1
The f_inserttextatbookmark is as follows:
public function integer f_inserttextatbookmark (string as_bookmark, string as_text, string as_fontname, integer ai_fontsize);
if isnull(as_text) then return 0
iole_word = create OLEOBJECT
iole_word.connectToNewobject( "word.application" )
iole_word.Documents.open( <string to word doc> )
iole_word.ActiveDocument.FormFields.Item(as_bookmark).Result = as_text
return 1
end function
Part of your problem is that carriage return is char(13), and line feed is char(10), so to make a CRLF in Windows and DOS you usually need to make char(13) + char(10). If these are out of order, many programs will balk. However, "~r~n" should have produced that for you.
I have success with (and I'm converting for brevity so it might only be close to correct):
lole_Word.ConnectToNewObject ("Word.Application")
...
lole_Word.Selection.TypeText (ls_StringForWord)
Maybe you can try other Word OLE commands to see if it's something to do with the specific command. (After the definition of the line break, I'm grasping at straws.)
Good luck,
Terry
Sounds like it may be a Unicode/Ansi character conversion thing.
for what its worth you could try this ...
http://www.rgagnon.com/pbdetails/pb-0263.html
Hope it helps.
I'm not using form fields, but I am able to insert newlines into a Word document from PowerBuilder using TypeText and "~n". Maybe you just need "~n".