Reputation: 511
My problem is how to make my array accept decimal values.
The code is good for integers but i need it adjusted for decimal values.I tried to use Convert.ToDecimal(Console.ReadLine())
, but it wont work. Here is error message:
Error 1 Cannot implicitly convert type '
decimal
' to 'int
'. An explicit conversion exists (are you missing a cast?)
The problem is, how do I use a decimal in for loop?
I really need it to be like this, because I dont need to put prevalued number for quantity of values for my array or something like that. I need to enter number for size of array, then to enter a number that are a decimal in nature.
Here is the code:
public int Unos_brojeva()
{
Console.WriteLine("Unesi broj clanova niza:");
int [] broj = new int[Convert.ToInt32 (Console.ReadLine())];
Console.WriteLine("Unesi brojeve:");
for (int i = 0; i < broj.Length; i++)
{
broj[i] = (Convert.ToInt32 (Console.ReadLine()));
}
Console.WriteLine("Unos je zavrsen");
Console.ReadLine();
return 0;
}
static void Main()
{
BrojniNiz brojka;
brojka = new BrojniNiz();
brojka.Unos_brojeva();
}
Upvotes: 0
Views: 1981
Reputation: 1230
ok so if you want the input value within the array to be a decimal, then you should initialize the array as a decimal array.
int[] intArray = new int[<size of the array>];
decimal[] decimalArray = new decimal[<size of this array>];
hope that helps.
Upvotes: 1
Reputation: 149020
Have you tried changing it to a decimal array (decimal[]
)?
decimal[] broj = new decimal[Convert.ToInt32(Console.ReadLine())];
for (int i = 0; i < broj.Length; i++)
{
broj[i] = Convert.ToDecimal(Console.ReadLine());
}
Upvotes: 3