1 Replies - 2440 Views - Last Post: 03 January 2012 - 10:17 PM

#1 stwo28  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-January 12

Error with array supposedly out of bounds

Posted 03 January 2012 - 09:54 PM

In this program I am suppose to get an integer and turn it into English (e.g. 941=Nine Four One). I am getting an error with inside the for loop-character=UserNum[y];- is supposedly out of bounds. please help.
#pragma endregion
	private: String^ GetWord(String^ UserNum)
			 {
			 String^ fullword;
			 String^ word;
			 char character;
			 int x=UserNum->Length;
			 for (int y=0; y<=x; y++)
			 {
				 character=UserNum[y];
				 if (character=='-') word="Negative";
				 if (character=='0') word="Zero";
				 if (character=='1') word="One";
				 if (character=='2') word="Two";
				 if (character=='3') word="Three";
				 if (character=='4') word="Four";
				 if (character=='5') word="Five";
				 if (character=='6') word="Six";
				 if (character=='7') word="Seven";
				 if (character=='8') word="Eight";
				 if (character=='9') word="Nine";
				 fullword=fullword+word;
			 }
			 
			 return(fullword);
			 }
	
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 String^ UserNum=textUserNum->Text;
				 String^ English;
				 English=GetWord(UserNum);
				 labelEnglish->Text=English;
			 }
	};
}



Is This A Good Question/Topic? 0
  • +

Replies To: Error with array supposedly out of bounds

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,347
  • Joined: 25-December 09

Re: Error with array supposedly out of bounds

Posted 03 January 2012 - 10:17 PM

Your problem is the use of the operator<=. Arrays start at zero and end at size - 1. So when you say y = x you are out of bounds for your array. You should just use the operator<.
for (int y=0; y<x; y++)


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1