-3

Index was outside the bounds of an array -compiler told me once I ran my program
The problem is at line 44 -he said
PLEASE HELP ME GUYS

    static string[] timeArr = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "tweny one", "twenty two", "twenty three", "twenty four","twenty five", "twent six", "twenty seven", "twenty eight", "twenty nine", "thirty"};

    static string GetMinutesInWords(int minutes){
            string minutesPreposition = "past";
            minutesStr = timeArr[minutes];
            if (minutes>=31){
                minutes-=30;
                minutesPreposition="to";
                minutesStr = timeArr[minutes];
            }
            minutesStr+=minutesPreposition;
            return minutesStr;
        }
  • 1
    You're getting the array at index minutes prior to the if condition, so if you're passing in an int larger than 31, the index is going to be out of bounds. – gelfiusm Aug 28 '20 at 13:55

1 Answers1

-2
static string GetMinutesInWords(int minutes){
        string minutesPreposition = "past";
        
        if (minutes>=31){
            minutes-=30;
            minutesPreposition="to";


            minutesStr = timeArr[minutes];
        }
        minutesStr+=minutesPreposition;
        return minutesStr;
    }
Roman Ryzhiy
  • 1,540
  • 8
  • 5