|
|||||||
| Forum Home | Register | Members List | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | ||||||||
|
Junior Member
|
Hello, I'm a High School Student whom is aiming for a job as a Game Programmer (still haven't gotten specific yet).
This is part (what i got so far) of a program that I'm writing for a friend. In this program the aim is to ask how many requirements there are. Then i made an array with the variable that held the amount of requirements needed. Then using a loop, i started to assign the elements with the requirements that are going to be inputted by the user. using System; public class Work { public static void Main() { int req, count1; String strreq; Console.Out.Write("The number of total requirements that a person needs is: "); strreq = Console.ReadLine(); req = Convert.ToInt32(strreq); string [] requirements = new string [req - 1]; count1 = 1; while (count1 <= req) ; { Console.Out.Write("A requirement that is needed is: "); requirements[count1] = Console.ReadLine(); count1 = count1 + 1; } Console.Out.WriteLine(requirements[req]); } } A message comes up when compiled- Work.cs(21,2): warning CS0642: Possable mistaken empty statement It seems I'm using it (the variable "count1") to make it so the user can add a value to the Array's elements. When ran it ends up being an infinite loop. The last code "Console.Out.WriteLine(requirements[req]);" was just for me to see if it'd work (obviously didn't since i can't even get that far) but is not "part" of the program I'll be running once i figure this out. If anyone can help that would be great. I'd appreciate a reply of why you think its not working. Have a good one |
||||||||
|
|
|
|
|
#2 | ||||||||
|
Senior Member
Location: London |
Remove the Semi-colon after the while. That is ending the while statment early before it reach the code block inside of the braces, so it goes into an infinate loop like you said.
|
||||||||
|
|
|
|
|
#3 | ||||||||
|
Junior Member
|
Wow, that was a stupid mistake...
After I updated the program, i get a problem when i insert the requirements. Unhandled Exception: System.IndexOutOfRangeException: Idex was outside the bounds of the array. at work.Main() Anyone have an idea?? Program- using System; public class Work { public static void Main() { int req, count1; String strreq; Console.Out.Write("The number of total requirements that a person needs is: "); strreq = Console.ReadLine(); req = Convert.ToInt32(strreq); string [] requirements = new string [req - 1]; count1 = 0; while (count1 <= req) { Console.Out.Write("A requirement that is needed is: "); requirements[count1] = Console.ReadLine(); count1 = count1 + 1; } Console.Out.WriteLine(requirements[req]); } } Last edited by Hazard117 : 07-16-2009 at 09:02 AM. |
||||||||
|
|
|
|
|
#4 | ||||||||
|
Administrator
Location: UK |
You are going beyond the end of the array. Re-examine the while loop.
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
||||||||
|
|
|
|
|
#5 | ||||||||
|
Junior Member
|
I've been thinking and I'm not seeing it. I'm not to well with arrays yet but it doesn't seem like I'm surpassing the element amount. In C# i was told that the Array element count starts at 0 (ex. requirements [0] = College BA
.I ended up changing the: while (count1 <= req) to: while (count1 < req) and I'm still having the same problem. What I'm trying to do... Say there are 5 Requirements. So the array would have 5 elements (requirements [5-1] ) 0 1 2 3 4 is five elements. In the loop it starts with the 0 element and assigns it a requirement (in string form) Then the count1 variable is added by 1 so it equals 1 now Then i get a requirement for the 1 element Then repeats untill the 4th element After it assigns a requirement to the 4th element, it adds 1 to 4 and makes count1 = 5 Which should stop the while loop and move on I may have been overlooking something small but it seems to make sense to me (I'm a beginner so what do i know though). |
||||||||
|
|
|
|
|
#6 | ||||||||
|
Administrator
Location: UK |
What you did here is correct. However, the line after the while loop causes the same error.
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
||||||||
|
|
|
|
|
#7 | ||||||||
|
Junior Member
|
I feel like I'm annoying you or doing something stupid.
The sentence after the while loop would be: Console.Out.WriteLine(requirements[req]); since the body is in brackets. Since req is 1+ the element number in the array its asking it to state whats in an "imaginary" element. I changed it to: Console.Out.WriteLine(requirements[2]); to test it out but it doesn't seems to change the problem. This means it's in the blocked statements but i don't see whats wrong with them. |
||||||||
|
|
|
|
|
#8 | ||||||||
|
Junior Member
|
Found out what was wrong /facepalm
Thanks for the help! |
||||||||
|
|
|
|
|
#9 | ||||||||
|
Administrator
Location: UK |
The latter. Rather then point it out directly, it help you more if you see it for yourself.
__________________
Steven Yau [Alix Games Blog] [Portfolio] [How I broke into the Games Industry] [Why I left my Games Job] [How to be a Games Tester] [Getting back into the Game] |
||||||||
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Thread Tools | |
| Display Modes | |
|
|
Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 05:24 PM.
























.
Linear Mode

