1

i got a problems about calculating exam's marks.

int midterm,finalexam,makeupexam;
double average;
try
{
midterm=Int32.Parse(txtMidterm.Text);
finalexam=Int32.Parse(txtFinal.text);
}
catch
{
txtResult.Text="any mark has not been written";
}
average= midterm*30/100+finalexam*70/100;
if(average>=49.5 && finalexam>50)
{
txt.Result.Text="passed, average: "+average.To.String();
}
else
{
txtResult.Text="makeup stayed";
}
try
{
makeupexam=Int32.Parse(txtMakeUpExam.Text);
}
catch
{
txt.Result.Text="MakeUp's mark has not been written";
}
average= midterm*30/100+makeupexam*70/100;
if(average>=49.5 && makeupexam>50)
{
txtResult.Text="Passed: " + average;
}
else
{
txtResult.Text="failed, average: ", average;
}

The program is saying" use of unassigned local variable 'midterm,finalexam,makeupexam" please help me thank you all from now : )

Erdem Nayir
  • 85
  • 2
  • 9

3 Answers3

1

You are trying to use the variable but they may never have values assigned to them. Set them to 0 at the start and it should work.

Neel
  • 11,625
  • 3
  • 43
  • 61
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
0

Change your variable declaration:

int midterm = 0,finalexam = 0,makeupexam = 0;
double average = 0.0;

This occur because if you fail in your catch the variable never assign and you cannot perform actions on unassigned varible

AsfK
  • 3,328
  • 4
  • 36
  • 73
0

Local variables aren't initialized. You have to manually initialize them.

local variables don't have a default value.

Just add below code :-

int midterm = 0,finalexam = 0,makeupexam = 0;
double average = 0.0;

its happening because you have not assigned value to the variables.

Neel
  • 11,625
  • 3
  • 43
  • 61