❔ Arithmetic Sum help
Hey! So I want to run this simple code, but I'm getting an error saying: "Use of unnasigned local variable sum". I tried many things but none of them helped, could somebody edit this code and make it work? I'd be amazing!
11 Replies
void AA() { int cif,i; double nr, sum;
Console.WriteLine("How many numbers will you insert? ");
cif=Convert.ToInt32(Console.ReadLine());
for(i=1;i<=cif;i++) {
Console.WriteLine("Insert Number: ");
nr=Convert.ToDouble(Console.ReadLine());
sum=+nr;
}
Console.WriteLine("The Arithmetic Average is: " + sum/cif + "\n" + "Press any key to continue"); Console.ReadKey(); Start();
}
for gods sake pls use proper formating
what
thank you
what if the for loop was never executed? sum would have no value
thats what the compiler is complaining about
but shouldnt it have defaultvalue
since its a double 0
they didnt specify a default value
not for local variables
you should set sum's value to 0 when you declare it
double sum = 0;
hmm i will keep that in mind.. how wasnt i aware of this TIL
Thank you, I understand now lol, I could've sworn I setted it to 0
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.