Reputation: 23
I have to calculate commissions for different interval of sales on different days and form a table "Day", "Sales", "Commissions". So far I've done this:
real::a(5),b(5)
character(10)::c(5)=(/Sunday,Monday,Tuesday,Wednesday,Thursday/)
open(1,file='in_1.txt')
open(2,file='out_1.txt')
read(1,*)(a(i),i=1,5)
do i=1,5
if(0.<a(i).and.a(i)<99.)then
b(i)=a(i)*.02
elseif(100.<a(i).and.a(i)<299.)then
b(i)=a(i)*.05
elseif(300.<a(i).and.a(i)<499.)then
b(i)=a(i)*.075
else
b(i)=500.*.1+(a(i)-500.)*.125
endif
enddo
write(2,*)' Day ',' Sales ',' Commissions '
do i=1,5
write(2,10)c(i),a(i),b(i)
10 format(a10,t15,f4.1,t25,f8.2)
enddo
total=0.
do i=1,5
total=total+b(i)
enddo
write(2,20)total
20 format('Total Commissions',t25,f8.2)
end
The sales from sunday to thursday are in the input file in_1.txt.
When run, it shows the error: Element in REAL(4) array constructor is INTEGER(4).
What does it mean and how to resolve it?
Upvotes: 0
Views: 1590
Reputation: 6915
You are using implicit typing which is masking your fundamental error. Implicit typing assigns a type based upon the first character of a variable name.
The array initializer:
(/Sunday,Monday,Tuesday,Wednesday,Thursday/)
resolves the types of these undeclared variables as
(/real, integer, real, real, real/)
and emits an error because you have an integer in your array of reals.
You can see this more clearly with the test case below:
print *,(/1., 2, 3., 4., 5./)
end
This does not compile with the same error as yours:
arcon.f90:1:13:
print *,(/1., 2, 3., 4., 5./)
1
Error: Element in REAL(4) array constructor at (1) is INTEGER(4)
Thus, the types in your array constructor must all match and that is what the error means.
To fix that error would be to use all reals in your array constructor, but your actual issue is that you are using variables where you should be using string literals. Change the line
character(10)::c(5)=(/Sunday,Monday,Tuesday,Wednesday,Thursday/)
to
character(10)::c(5)=(/"Sunday","Monday","Tuesday","Wednesday","Thursday"/)
but you'll find a new error:
print *,(/"Sunday","Monday","Tuesday","Wednesday","Thursday"/)
1
Error: Different CHARACTER lengths (6/7) in array constructor at (1)
and to address that you want to change your initialization to
character(10)::c(5)=(/"Sunday ","Monday ","Tuesday ","Wednesday","Thursday "/)
You should always use implicit none
in your code to avoid masking your real error. Your original array constructor with no implicit typing would have instead emitted the error
Error: Symbol ‘monday’ at (1) has no IMPLICIT type
letting you know that you either forgot to declare the variable monday
or that it should have been something else, e.g. a string literal in this case.
Upvotes: 2