My code suppos to print an array of integers in wich the user enters as many elements he wants and enter -99 (as a string) to exit the loop.Also valid entries range is 0 - 10 with 0 and 10 included.I get wrong results when printing the array tha it prints the last entry only and zeros after .The code is in Console C#.Any help will be appreciated.Thanks.
namespace ExeNo1Ch7
{
class Program
{
static void Main(string[] args)
{
int numOfEntry;
int[] intNumbers = new int[100];
numOfEntry = GetArrayOfIntegers(intNumbers);
Console.WriteLine("\t\n You have entered " + numOfEntry + " values " + "\t\n" + "They are:");
for (int i = 0; i < numOfEntry; i++)
{
Console.WriteLine("\t\n" + intNumbers[i]);
}
Console.WriteLine("\t\n<< Press any key to Exit >> ");
Console.ReadKey();
}
public static int GetArrayOfIntegers(int[] anArray)
{
string strValue;
int counter = 0;
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
for (int i = 0; i < anArray.Length; i++)
{
while (strValue != "-99")
{
anArray[i] = int.Parse(strValue);
counter = counter + 1;
if (anArray[i] >= 0 && anArray[i] <= 10)
{
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
else
{
Console.WriteLine("\t\n Please try again entering an integer in the range (0 - 10) only,");
Console.Write("\t\n Enter an enteger from 0 - 10 :");
strValue = Console.ReadLine();
}
}
}
return counter;
}
iat all - all your "spinning" is done in the while cycle inside, so you only assign to the first index, and when you finally input-99it does the remaining 99 steps (without reading any ints) and returns.