Game Career Guide Forums

Game Career Guide Forums (http://www.gamecareerguide.com/forums/index.php)
-   Programming (http://www.gamecareerguide.com/forums/forumdisplay.php?f=7)
-   -   Help with a small program please (http://www.gamecareerguide.com/forums/showthread.php?t=2777)

Hazard117 07-15-2009 08:36 PM

Help with a small program please
 
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

Claxon 07-16-2009 12:01 AM

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.

Hazard117 07-16-2009 08:54 AM

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]);




}

}

yaustar 07-16-2009 12:47 PM

You are going beyond the end of the array. Re-examine the while loop.

Hazard117 07-16-2009 02:17 PM

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).

yaustar 07-16-2009 02:44 PM

Quote:

Originally Posted by Hazard117 (Post 14883)
I ended up changing the: while (count1 <= req)
to: while (count1 < req)
and I'm still having the same problem.

What you did here is correct. However, the line after the while loop causes the same error.

Hazard117 07-16-2009 03:00 PM

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.

Hazard117 07-16-2009 08:12 PM

Found out what was wrong /facepalm

Thanks for the help!

yaustar 07-17-2009 01:34 AM

Quote:

Originally Posted by Hazard117 (Post 14886)
I feel like I'm annoying you or doing something stupid.

The latter. Rather then point it out directly, it help you more if you see it for yourself.


All times are GMT -8. The time now is 11:44 PM.

Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.