Reputation: 2261
I am trying to create a function that accepts 5 integers, holds them in an array and then passes that array back to the main. I have wrestled with it for a couple of hours and read some MSDN docs on it, but it hasn't "clicked". Can someone please shed some light as to what I am doing wrong? Errors: Not all code paths return a value; something about invalid arguments:
Code:
using System;
using System.IO;
using System.Text;
namespace ANumArrayAPP
{
class Program
{
static void Main()
{
int[] i = new int[5];
ReadInNum(i);
for (int a = 0; a < 5; a++)
{
Console.Write(a);
}
}
static int ReadInNum(int readIn)
{
int[] readInn = new int[5];
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please enter an interger");
readInn[i] = Console.Read();
}
}
}
}
Upvotes: 4
Views: 9001
Reputation: 273189
To summarize zome aswer already here, there are two ways to go about it:
static void Main()
{
int[] i = new int[5];
ReadInNum(i);
for (int a = 0; a < 5; a++)
{
Console.Write(i[a]); // !
}
}
static int ReadInNum(int[] readIn)
{
for (int i = 0; i < rreadIn.Length; i++)
...
}
static void Main()
{
// int[] i = new int[5];
int[] i = ReadInNum(5);
for (int a = 0; a < i.Length; a++)
{
Console.Write(i[a]); // !
}
}
static int[] ReadInNum(int size)
{
int[] readIn = new int[size];
for (int i = 0; i < readIn.Length; i++)
...
}
Upvotes: 1
Reputation: 8951
Pass an array to the function:
class Program
{
static void Main()
{
int[] i = new int[5];
ReadInNum(i);
for (int a = 0; a < 5; a++)
{
Console.Write(a);
}
}
static int[] ReadInNum(int[] readIn)
{
for (int i = 0; i < readIn.Length; i++)
{
Console.WriteLine("Please enter an interger");
readInn[i] = Console.Read();
}
return readIn;
}
}
Notice how I returned an int[]
from ReadInNum. This isn't necessary but it makes the code more readable since it's not more obvious that you're planning on changing readIn.
Upvotes: 0
Reputation: 1500135
The parameter to ReadInNum
is a single int
, but you're trying to pass an array. You have to make the parameter match what you're trying to pass in. For example:
static void ReadInNum(int[] readIn)
{
for (int i = 0; i < readIn.Length; i++)
{
Console.WriteLine("Please enter an interger");
readIn[i] = int.Parse(Console.ReadLine());
}
}
That will fill the array you pass into the method. Another alternative is to pass in how many integers you want, and return the array from the method:
static int[] ReadInNum(int count)
{
int[] values = new int[count];
for (int i = 0; i < count; i++)
{
Console.WriteLine("Please enter an interger");
values[i] = int.Parse(Console.ReadLine());
}
return values;
}
You'd call this from Main
like this:
int[] input = ReadInNum(5);
for (int a = 0; a < 5; a++)
{
Console.Write(input[a]);
}
Upvotes: 4
Reputation: 11213
You should complete your function with return statement since its return type is not void. Also, you should change your return type to int[] since your return an array of ints, not just one int:
static int[] ReadInNum(int readIn)
{
int[] readInn = new int[5];
for (int i = 0; i < 5; i++)
{
Console.WriteLine("Please enter an interger");
readInn[i] = Console.Read();
}
return readInn;
}
You should also change your main function in order to get the result of the function ReadInNum. It seems to me that you don't really use the parameter so you can remove it, if you don't have special attention:
static void Main()
{
int j;
int[] i = ReadInNum(j);
for (int a = 0; a < 5; a++)
{
Console.Write(a);
}
}
Upvotes: 2