Link to home
Start Free TrialLog in
Avatar of pieter78
pieter78

asked on

If-statement problem, can't figure it out...

hello,

rather a stupid question, but I just don't see where it goes wrong. I have got this code (explanation in comments):

---------------------------------------------------------------------------------------------------------------------------
// Two-dim array with causes of headache
// first field is the disease, second field is the onset (a = acute beginning; g = gradual beginning), and third field is the localisation (d = diffuse; f = focal)

string[,] Headache = {
{"disease01", "g", "f"},
{"disease02", "a", "d"},
{"disease03", "a", "f"}
};

// Then I fetch the result of two comboBoxes

switch (cboBegin.SelectedIndex)
{
      case 0:
      SymptomBegin = "a";            // acute
      break;
      case 1:
      SymptomBegin = "g";            // gradual
      break;
}
switch (cboLocation.SelectedIndex)
{
      case 0:
      SymptomLocation = "d";      // diffuse
      break;
      case 1:
      SymptomLocation = "f";      // focal
      break;
}

// Now, I want to calculate a score. Therefore, I want to check if the comboBox input equals the database,
// and depending on what data are present a score is given.

for (int i=0; i<MaxCounter; i++)
{
      int Score  = 0;
      if ((SymptomBegin       == Headache[i,1])&&(SymptomBegin       == "a")) {ScoreBegin       = 70;}
      if ((SymptomBegin       == Headache[i,1])&&(SymptomBegin       == "g")) {ScoreBegin       = 50;}
      if ((SymptomLocation == Headache[i,2])&&(SymptomLocation == "d")) {ScoreLocation = 30;}
      if ((SymptomLocation == Headache[i,2])&&(SymptomLocation == "f")) {ScoreLocation = 80;}
      Score += ScoreBegin + ScoreLocation;
        DiseaseString += Headache[i,0] + ": " + Score.ToString() + "\r";
}

// Show the score per disease
MessageBox.Show(DiseaseString);
---------------------------------------------------------------------------------------------------------------------------

I can't see any mistakes in this code, and Ctrl+Shift+B says all is OK.

But, when I run this, I get an unexpected result. I selected "acute" and "focal".....

 the first score (for disease1) is calculated as 80, and all the others as 150. So, it just counts 70 points for acute and 80 points for focal, NO MATTER WHETHER THIS CORRELATES TO THE DATABASE OR NOT!!

I really do not see what is going wrong. Perhaps important to know: I am developing for the .NET Compact Framework.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of softplus
softplus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pieter78
pieter78

ASKER

If it made sense??? Yes, and now I see what went wrong: indeed I needed to reset the variables ScoreBegin and ScoreLocation.

I did not understand what went wrong, because everytime the loop runs again, so I assumed points would be re-divided. But what I forgot, was that -of course- if the condition in the statement is not met, then the variable will hold its value from the previous run. And there it went wrong.

So now it works, txh :-D
(btw: I needed the second example, the score is calculated per disease, so per loop)