Dave
Dave

Reputation: 199

"Invalid operand types for operand = "ada

I am trying to check if two strings are the same and I keep getting an error saying the operands are incorrect. Both sides of the "=" are of the type StateAbbreviation, where is the error?

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure ElectionPrediction is
        MaxCandidates: constant Integer := 100;
        subtype StateAbbreviation is String (1..2);
        subtype Initials is String (1..2);
        type CandidateInfo is record
                CandidateInitials:  Initials;
                CandidateScore: Integer;
        end record;

        type ScoreArray is array (1..MaxCandidates) of CandidateInfo;

        Score: ScoreArray;
        CurrentState, HomeState: StateAbbreviation;
        CandidateName: Initials;

        function CalculatePointsFromState(CurrentState: StateAbbreviation; CandidateState: StateAbbreviation) return Integer is
                Total: Integer := 0;
                temp: Integer := 0;

        type ScoreArray is array (1..MaxCandidates) of CandidateInfo;
        type NewEngland is array (1..6) of StateAbbreviation;
        type NorthEast is array (1..5) of StateAbbreviation;
        type SouthEast is array  (1..12) of StateAbbreviation;
        type Lakes is array  (1..6) of StateAbbreviation;
        type Central is array (1..8) of StateAbbreviation;
        type West is array (1..8) of StateAbbreviation;
        type Pacific is array (1..5) of StateAbbreviation;

        begin

                NewEngland := ("ME", "NH", "VT", "MA", "CT", "RI");
                NorthEast := ("NY", "PA", "NJ", "DE", "MD");
                SouthEast := ("VA", "NC", "SC", "GA", "FL", "AL", "MS", "TN", "KY", "WV", "AR", "LA");
                Lakes := ("OH", "MI", "IN", "IL", "WI", "MN");
                Central := ("IA", "MO", "ND", "SD", "NE", "KS", "OK", "TX");
                West := ("MT", "WY", "CO", "NM", "AZ", "UT", "ID", "NV");
                Pacific :=("WA", "OR", "CA", "AK", "HI");

                if CandidateState = CurrentState then Total := Total + 50;
                end if;
                for I in NewEngland'range loop
                        **if CurrentState = NewEngland(NewEngland'First + I) then temp := temp + 1; end if;
                        if CandidateState = NewEngland(NewEngland'First + I) then temp := temp + 1; end if;**
                end loop;
                if temp = 2 then return  Total + 20;
                end if;

                return 0;
        end CalculatePointsFromState;

end ElectionPrediction;

Upvotes: 0

Views: 799

Answers (1)

Simon Wright
Simon Wright

Reputation: 25501

Earlier errors in your code give messages like

39.       NorthEast := ("NY", "PA", "NJ", "DE", "MD");
          |
    >>> invalid use of subtype mark in expression or call

because you have defined

29.       type NorthEast is array (1..5) of StateAbbreviation;

NorthEast is a (sub)type, not a variable! and this serious error has confused the compiler to the point where the later error messages don’t make as much sense as they could.

What you might consider is creating a type for an array of StateAbbreviations of any length

type StatesArray is array (Positive range <>) of StateAbbreviation;

and then creating the regional data as specific (constant: you wouldn’t want your program to overwrite them by mistake) arrays of this type

NewEngland : constant StatesArray := ("ME", "NH", "VT", "MA", "CT", "RI”);
NorthEast  : constant StatesArray := ("NY", "PA", "NJ", "DE", "MD");
...

after which the rest of the code will compile OK.

Upvotes: 4

Related Questions