Get the latest Education e-news
 

Go Back   Game Career Guide Forums > Programming
Forum Home Register Members List Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 07-15-2009, 08:36 PM   #1
Hazard117
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss13
Question 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
Hazard117 is offline   Reply With Quote
Old 07-16-2009, 12:01 AM   #2
Claxon
Senior Member

Activity Longevity
0/20 18/20
Today Posts
0/11 ssssss345
Location: London
Default

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.
Claxon is offline   Reply With Quote
Old 07-16-2009, 08:54 AM   #3
Hazard117
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss13
Default

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.
Hazard117 is offline   Reply With Quote
Old 07-16-2009, 12:47 PM   #4
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
2/11 sssss2269
Location: UK
Default

You are going beyond the end of the array. Re-examine the while loop.
yaustar is offline   Reply With Quote
Old 07-16-2009, 02:17 PM   #5
Hazard117
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss13
Default

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).
Hazard117 is offline   Reply With Quote
Old 07-16-2009, 02:44 PM   #6
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
2/11 sssss2269
Location: UK
Default

Quote:
Originally Posted by Hazard117 View Post
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.
yaustar is offline   Reply With Quote
Old 07-16-2009, 03:00 PM   #7
Hazard117
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss13
Default

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 is offline   Reply With Quote
Old 07-17-2009, 01:34 AM   #8
yaustar
Administrator
 
yaustar's Avatar

Activity Longevity
2/20 19/20
Today Posts
2/11 sssss2269
Location: UK
Default

Quote:
Originally Posted by Hazard117 View Post
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.
yaustar is offline   Reply With Quote
Old 07-16-2009, 08:12 PM   #9
Hazard117
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 sssssss13
Default

Found out what was wrong /facepalm

Thanks for the help!
Hazard117 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Powered by vBulletin® Version 3.6.9
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
All times are GMT -8. The time now is 04:22 PM.






UBM Tech