I am running Fortran 90 on AIX. I'm not sure if this is a compatibility issue or not. I am trying to print a character string to the screen containing the last two characters of the current year (ex: it's 2016 and I want 16) and the output is displaying a black diamond with a question mark inside of it. What does that indicate/represent?
CHARACTER ( LEN=4 ) :: year_str
print *, "year_str(3:4) is: ", year_str(3:4)
Output:
period_year_str(3:4) is: ��
As previous users commented, the black diamond represents an unknown character. This is because year_str was not properly defined.
Related
I am working on a project and as I have not coded with Fortran before, I am struggling a lot. My professor gave me a code file which I need to fix but I don't understand the syntax.
So, in the file he has
g = some formula,
1 some formula
2 * some formula
3 / some formula.
What does 1, 2, 3, * and / do?
I asked my Professor, and he said that this is Fortran 77 code and 1, 2, 3 are used as indexing in column 6 and the g is in column 7 as that's how the Fortran code is written. But I was very confused why Fortran 77 only accepts code after column 7?
Thank you for all the replies.
What you are most likely looking at is Fixed source-form statement continuation which is part of the Fixed source form.
Fixed-form formatting is an old way of formatting code which still stems from the old punched-cards. Lines could only be 72 characters long, but sometimes you needed more. Hence, the statement-continuation character:
Except within commentary, character position 6 is used to indicate continuation. If character position 6 contains a blank or zero, the line is the initial line of a new statement, which begins in character position 7. If character position 6 contains any character other than blank or zero, character positions 7–72 of the line constitute a continuation of the preceding non-comment line.
source: Fortran 2018 Standard, Section 6.3.3.3
Which character is used as statement-continuation marker, is up to the programmer and his style. Often you see a <ampersand>-character (&), or <dollar>-character ($) or the <asterisk>-character (*) like so:
c23456789012345678901234567890123456789012345678901234567890123456789012
g = something long
& + something_longer
& + something_even_longer
However, in the really old days, people often numbered their lines.
c23456789012345678901234567890123456789012345678901234567890123456789012
0g = something long
1 + something_longer
2 + something_even_longer
and because space was limited, they removed all spaces, which sometimes becomes very confusing when you have numbers in your line:
c23456789012345678901234567890123456789012345678901234567890123456789012
0g=1.2345+
10.35697-
22.5789
This does not add 10.35697 and subtract 22.5789, but adds 0.35697 and subtracts 2.5789
The usage of numbers as statement continuation markers is again inherited from the punched-cards. A single punched-card represented a single Fortran statement. And on the card, the row and column numbers were printed (Thanks to High Performance Mark for this information)
Note: the asterisk and slash in the OP are nothing more than the normal multiplication and division.
I am trying to use trim/adjustl for the following code. It seems that I'm getting either X_eq_ 10.0.dat or X_eq_10.0 .dat as the output file's name where I'm expecting it to be X_eq_10.0.dat (no blank space). Any remedy?
Program Test
double precision:: X
character (len=10) :: tag
character (len=100) :: outfile
X=10.0
write(tag,'(f10.1)') X
print*,'tag=',tag
outfile='X_eq_'//trim(tag)//'.dat'
print*,'Output file: ',outfile
outfile='X_eq_'//trim(tag)//trim('.dat')
print*,'Output file: ',outfile
outfile='X_eq_'//adjustl(trim(tag))//adjustl(trim('.dat'))
print*,'Output file: ',outfile
End Program Test
I have used gfortran as the compiler.
What you want is:
outfile='X_eq_'//trim(adjustl(tag))//'.dat'
adjustl shifts the characters left, leaving trailing blanks, so you need to trim that result. It does no good to do trim(tag) as that is already right-adjusted. Lastly, '.dat' doesn't need any processing.
In
write(tag,'(f10.1)') X
we say that we want tag to be of width 10 with one digit in the fractional part. With the one decimal symbol that leaves us 8 places before the decimal: there will be blank padding beyond the (optional) sign.
This is why we see lots of blanks in outfile='X_eq_'//trim(tag)//'.dat'.
We can avoid this either with adjustl as noted in the question or another answer, or by using 0 in the edit descriptor:
write(tag,'(F0.1)') X
The F0.d form makes the field width the smallest appropriate field with: without leading blanks.
When tag has length 100 there will still be (lots of) trailing blanks, so a trim will be necessary.
Further, there are even ways to avoid using an intermediary such as tag without using trim.
I found this one old fortran 77 program printed in one old book, and i typed in the program but it isnt running. When I give the dimensions of variables, appear the next:
DIMENSION XN(20),YN(20),W(20),NUMAJ(20),NUMAAJ(20),SSW(20)
1,NEJI(5,20),QX(20,20),QY(20,20),IACTE(20),N1(20),N2(20),X(20)
2,Y(20),DD(20),TT(20),NUMAJA(20)
And the error code:
Error: Non-numeric character in statement label at (1)
Error: Unclassifiable statement at (1)
Thanks a lot,
Comparing the original and edited version shows still incorrect indenting: fixed format reserves columns 1-5 for labels, 6 for the continuation character and starting from 7 for the statements. If column 6 is not a space the line continues the previous line.
In the 3 line example above the characters "1" and "2" are the continuation characters of line 2 and 3, so they have to be exactly in column 6. The first line has no label or continuation character so it must start in column 7. The comment from #melpomene was incomplete regarding continuation character and based on the old version where one space less was used (the edited version has one space too much).
I have little knowledge of the Fortran language. I have come across some programs written in the 90s (see attached snapshot showing just a portion of a long script).
I'd like to know what kind of compiler is appropriate to execute such codes? I have installed gfortran-4.2.3 on my mac. I'm also not sure about the indentation in the attached code: if C (comment) is at column 1, does the main code start at column 9 or 7? what about the position of numbers placed in between referred by GO TO?
This is not Fortran 90. This is Fortran 77.
That said, gfortran is able to compile this code. Make sure that the file extension for the file is .f so that gfortran realises it's fixed-form.
The C needs to be in the first column, the numbers that you reference are labels, they can be between column 1 and 5. The asterisk at line 198 is a continuation character, meaning that this should be treated as part of the previous line. It must be in column 6. Everything else needs to be between columns 7 and 72 (inclusive)
Oh, and the 3-digit numbers at the very beginning are line numbers, and must not be in the source code.
Edited to add: Since you have to type it all again anyway, you might as well make it free-form:
Replace the C in the first column with !, and change the way continuation lines are marked: Turn this:
write (*, *) "This is some long text,
* which doesn't fit into a line"
Into this:
write(*, *) "This is some long text, " // &
"which doesn't fit into a line"
Everything else can stay like it is. (You can now use proper indentation, too!)
New Edit
So you've pasted the code that you wrote and the error messages, so I'm replying to that now.
In Fixed Form, any character past column 72 is ignored. You have a few lines with long strings, where the terminating quotation mark is in that ignored region.
Make sure that no line exceeds the 72nd row.
To show a comma separated number I set local as
setlocale(LC_NUMERIC, "en_US");
then compose a message
char msg[100]={0};
sprintf(msg,"Reach a score %'ld.",mission.param1);
mission.msg=msg;
Then I display this message as :
sprintf( msgStr,"%s",mission.msg.c_str());
_missionStatusLabel1=CCLabelTTF::create(msgStr, "MarkerFelt-Thin", 52 * _scaleY, CCSizeMake(1000 * _scaleX,0),
kCCTextAlignmentLeft);
_missionStatusLabel1->setPosition( ccp(_screenSize.width * 0.53f, _screenSize.height*0.704f) );
Problem is, on simulator it shows correct format like for 25000 it shows 25,000 but when I run on iPad(ios7) it simply show 25000 without comma.
what I may be missing ?
EDIT:
Or How would you show a comma separated number ?
As you can see In my code above I am setting locale using the c++ setLocale. But this code showed comma only in simulator but not actual device . So I tried NSNumberFormatter class and everything worked just perfect. I picked the code from NSNumberFormatter with comma decimal separator and just wrapped under c++ interface for use