[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

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?
0
pieter78
Asked:
pieter78
1 Solution
 
softplusCommented:
if I understand correctly, you want to add up the scores? if so: you need to place your definition outside the loop, + reset your ScoreBegin + ScoreLocation, i.e.:
int Score  = 0;
for (int i=0; i<MaxCounter; i++)
{
     ScoreBegin = 0;
     ScoreLocation = 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";
}

if you just need the score within an entry:
for (int i=0; i<MaxCounter; i++)
{
     ScoreBegin = 0;
     ScoreLocation = 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;}
     int Score  = ScoreBegin + ScoreLocation;
        DiseaseString += Headache[i,0] + ": " + Score.ToString() + "\r";
}

Does that make sense? :)
0
 
pieter78Author Commented:
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)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now