0

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?

9
  • The compiler bulks at line 2. I'm using Code::Blocks 13.12 as a compiler. Commented May 25, 2015 at 16:00
  • 2
    If this is your complete code then implicit none as your first line will show you why there is a problem Commented May 25, 2015 at 16:05
  • 3
    implicit rule -> Sunday = single-precision-real -> real(4) array -> Monday = integer -> Error ? Commented May 25, 2015 at 16:09
  • 2
    Indeed, @roygvib. But I suspect the better answer is Sunday is not "Sunday". Mentioning that if character literals are meant, the array constructor isn't valid, for which a duplicate can be found. So: I think clarification is required as to what the problem is. Commented May 25, 2015 at 16:14
  • 4
    So? This just points you towards the (second) comment of @francescalus. thursday is a variable, "thursday" is a character literal. The variable thursday has not been defined yet, that is what the compiler is complaining about. Commented May 25, 2015 at 16:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.