Flawed logic

G

Guest

I've been working on a project for the last week and I can't seem to get it working. It's supposed to be finished within the next few days so a speedy reply would be appriciated. Anyways, it seems that my program goes into some sort of endless loop that I can't find, as when I click a link to get to the page I'm working on, the page never loads, and the aspnet_wp process consumes 98% of cpu time (the other 2% is used by task manager). I'm using visual studio 2005 express beta 1 with ASP.NET 2.0.40607.0.
Here is the code:

public partial class Testaspx
{
private StreamReader questions;
private XmlTextReader questionReader;
string number;
private string prefix;

void Page_Load(object sender, EventArgs e)
{

this.btnNext.Click += new EventHandler(this.getAnswer);
if (!openFile()) {
lbldisplay.Text = "Error opening";
return;
}
if (!prepare())
{
lbldisplay.Text += "Error preparing";
}
getQuestion();
}

private bool prepare()
{
//do
//{
while (!questionReader.LocalName.Equals("benchmark") && !questionReader.EOF)
{
questionReader.Read();
}

if (questionReader.EOF)
{
return false;
//Server.Transfer("grade.aspx");
}
//} while (!questionReader.IsStartElement("benchmark"));

if (questionReader.ReadToDescendant("preface"))
{
prefix = questionReader.ReadString();
}
else
{
lbldisplay.Text = "Error getting preface";
return false;
}

return true;
}

public void getQuestion()
{
string passage = "";
do
{
//do
//{
while (!questionReader.LocalName.Equals("question") && !questionReader.LocalName.Equals("benchmark"))
{
questionReader.Read();
}

if (questionReader.LocalName.Equals("benchmark"))
{
if (!prepare())
{
lbldisplay.Text += "Error preparing in getQuestion";
return;
}
continue;
}
// } while (!questionReader.IsStartElement("question"));

if (questionReader.ReadToDescendant("number"))
{
number = questionReader.ReadString();
}
else
{
lbldisplay.Text = "error getting passage number";
}

} while (number != Session["next"]);

if (questionReader.ReadToNextSibling("passage"))
{
passage = questionReader.ReadString();
}
else
{
lbldisplay.Text = "Error getting passage";
}

while (questionReader.ReadToNextSibling("option"))
{
options.Items.Add(questionReader.ReadString());
}
lblNumber.Text = number;
lbldisplay.Text = prefix + passage;
return;

}

/// <summary>
/// Finds and stores which answer was selected
/// </summary>
private void getAnswer(object sender, EventArgs e)
{
string selected = "";
selected = options.SelectedItem.Text.Substring(0, 1);
if (selected != "")
{
string number = (string)(Session["next"]);
Session.Add(number, selected);
Session["next"] = getNextNumber();
Server.Transfer("Test.aspx");
}
//return true;
}

/// <summary>
/// Gets the number of the next passage
/// </summary>
/// <returns>Number of next passage as string</returns>
private string getNextNumber()
{
while (!questionReader.LocalName.Equals("number") && !questionReader.EOF)
{
questionReader.Read();
}

if (questionReader.EOF)
{
//replace with redirect to grade page
Server.Transfer("grade.aspx");
//return "none";
}

string next = questionReader.ReadString();
return next;

}

/// <summary>
/// Opens the next questions file
/// </summary>
private bool openFile()
{
questions = new StreamReader(Session["location"] + "\\tests\\" + (string)(Session["file"]) + ".xml");
questionReader = new XmlTextReader(questions);
if (questions == null || questionReader == null)
{
return false;
}
return true;

}
}
 
S

S. Justin Gengo

bstoddart,

I believe the problem is where you're testing if you should continue the
read.

The first thing I would do is change your while loops:

//set default return value here

while (questionReader.Read)
{
//check each row here and set return value(s)
}

//return value here

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
bstoddart said:
I've been working on a project for the last week and I can't seem to get
it working. It's supposed to be finished within the next few days so a
speedy reply would be appriciated. Anyways, it seems that my program goes
into some sort of endless loop that I can't find, as when I click a link to
get to the page I'm working on, the page never loads, and the aspnet_wp
process consumes 98% of cpu time (the other 2% is used by task manager). I'm
using visual studio 2005 express beta 1 with ASP.NET 2.0.40607.0.
Here is the code:

public partial class Testaspx
{
private StreamReader questions;
private XmlTextReader questionReader;
string number;
private string prefix;

void Page_Load(object sender, EventArgs e)
{

this.btnNext.Click += new EventHandler(this.getAnswer);
if (!openFile()) {
lbldisplay.Text = "Error opening";
return;
}
if (!prepare())
{
lbldisplay.Text += "Error preparing";
}
getQuestion();
}

private bool prepare()
{
//do
//{
while (!questionReader.LocalName.Equals("benchmark") && !questionReader.EOF)
{
questionReader.Read();
}

if (questionReader.EOF)
{
return false;
//Server.Transfer("grade.aspx");
}
//} while (!questionReader.IsStartElement("benchmark"));

if (questionReader.ReadToDescendant("preface"))
{
prefix = questionReader.ReadString();
}
else
{
lbldisplay.Text = "Error getting preface";
return false;
}

return true;
}

public void getQuestion()
{
string passage = "";
do
{
//do
//{
while (!questionReader.LocalName.Equals("question") && !questionReader.LocalName.Equals("benchmark"))
{
questionReader.Read();
}

if (questionReader.LocalName.Equals("benchmark"))
{
if (!prepare())
{
lbldisplay.Text += "Error preparing in getQuestion";
return;
}
continue;
}
// } while (!questionReader.IsStartElement("question"));

if (questionReader.ReadToDescendant("number"))
{
number = questionReader.ReadString();
}
else
{
lbldisplay.Text = "error getting passage number";
}

} while (number != Session["next"]);

if (questionReader.ReadToNextSibling("passage"))
{
passage = questionReader.ReadString();
}
else
{
lbldisplay.Text = "Error getting passage";
}

while (questionReader.ReadToNextSibling("option"))
{
options.Items.Add(questionReader.ReadString());
}
lblNumber.Text = number;
lbldisplay.Text = prefix + passage;
return;

}

/// <summary>
/// Finds and stores which answer was selected
/// </summary>
private void getAnswer(object sender, EventArgs e)
{
string selected = "";
selected = options.SelectedItem.Text.Substring(0, 1);
if (selected != "")
{
string number = (string)(Session["next"]);
Session.Add(number, selected);
Session["next"] = getNextNumber();
Server.Transfer("Test.aspx");
}
//return true;
}

/// <summary>
/// Gets the number of the next passage
/// </summary>
/// <returns>Number of next passage as string</returns>
private string getNextNumber()
{
while (!questionReader.LocalName.Equals("number") && !questionReader.EOF)
{
questionReader.Read();
}

if (questionReader.EOF)
{
//replace with redirect to grade page
Server.Transfer("grade.aspx");
//return "none";
}

string next = questionReader.ReadString();
return next;

}

/// <summary>
/// Opens the next questions file
/// </summary>
private bool openFile()
{
questions = new StreamReader(Session["location"] + "\\tests\\"
+ (string)(Session["file"]) + ".xml");
 
G

Guest

Your suggestion will probably make the code easier to read and debug, but it turns out that I was having a problem with casting. In the code, I had while statement like this:
} while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it works:
} while (number != (string)(Session["next"]));

Thank you for your sugesstion.
 
S

S. Justin Gengo

bstoddart,

Interesting! I didn't think a cast (or lack thereof) could cause an infinite
loop.

In that case I would highly suggest setting your code to Option Strict and
Option Explicit.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
bstoddart said:
Your suggestion will probably make the code easier to read and debug, but
it turns out that I was having a problem with casting. In the code, I had
while statement like this:
} while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it works:
} while (number != (string)(Session["next"]));

Thank you for your sugesstion.
 
S

S. Justin Gengo

Jeremy,

That's too bad that c# doesn't have option strict / explicit. They can't be
"Always On" in the same way that setting those in VB.NET work, because if it
worked the same way bstoddart never could have made the comparison without
explicitly casting the session variable in the first place.

Option Explicit forces the cast to be made or the code won't run. Since
number was declared as a string in VB.NET Option Explicit would have not let
the code run unless the session object were also cast as a string...

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
Jeremy Davis said:
The problem was that since Session["next"] was coming out as object, the
!= was doing a reference comparison rather than a value comparison--and was
always evaluating false, since number and Session["next"] are two different
objects in memory. The explicit cast caused a string comparison, as was
desired.
Oh, and C# doesn't have the concept of "Option Strict" or "Option
Explicit". They're effectively always on.
S. Justin Gengo said:
bstoddart,

Interesting! I didn't think a cast (or lack thereof) could cause an infinite
loop.

In that case I would highly suggest setting your code to Option Strict and
Option Explicit.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
bstoddart said:
Your suggestion will probably make the code easier to read and debug,
but
it turns out that I was having a problem with casting. In the code, I had
while statement like this:
} while (number != Session["next"]);
where number is a string, by doing an explicit cast to string it works:
} while (number != (string)(Session["next"]));

Thank you for your sugesstion.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top