Solved

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

Posted on 2005-05-12
208 Views
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)

{"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:
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
Question by:pieter78

LVL 13

Accepted Solution

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

Author Comment

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…