C
C#2y ago
Cristi

❔ 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
Cristi
CristiOP2y ago
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(); }
ACiDCA7
ACiDCA72y ago
for gods sake pls use proper formating
Cristi
CristiOP2y ago
what
TheRanger
TheRanger2y ago
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();
}
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();
}
ACiDCA7
ACiDCA72y ago
thank you
TheRanger
TheRanger2y ago
what if the for loop was never executed? sum would have no value thats what the compiler is complaining about
ACiDCA7
ACiDCA72y ago
but shouldnt it have defaultvalue since its a double 0
TheRanger
TheRanger2y ago
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;
ACiDCA7
ACiDCA72y ago
hmm i will keep that in mind.. how wasnt i aware of this TIL
Cristi
CristiOP2y ago
Thank you, I understand now lol, I could've sworn I setted it to 0
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server