for loop not stopping when it should

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm trying to check and see if something other than numbers (either the +,
-, *, or /) are entered into a textbox, where bigR is what I call the text in
the textbox. I can get what was entered and where along the string (the
where part is the first section of below). I'm running into trouble just
after that. I tested what the value of breakpt[0] and got 3, which it should
be if entering something like 100 + 200. But when I run the for j = 0 to j
= < breakpt[0], it doesn't stop at 2, but keeps going until the index is out
of the array.
If the messagebox says breakpt[0] = 3, why doesn't j stop at 2?
Thanks!!!
Melanie

int i, j;
for(i = 0; i < bigR.Length; i++)
{
if ((bigR == '+') || (bigR == '-') || (bigR == '*') || (bigR
== '/'))
{breakpt += i;}
}

// breakpt[0] = 3 if type 100+200

MessageBox.Show(breakpt[0].ToString());
for (j = 0; j < breakpt[0]; j++)
{
MessageBox.Show(j.ToString());
nums += bigR[j];
}
 
melanieab said:
Hi,
I'm trying to check and see if something other than numbers (either
the +, -, *, or /) are entered into a textbox, where bigR is what I
call the text in the textbox. I can get what was entered and where
along the string (the where part is the first section of below). I'm
running into trouble just after that. I tested what the value of
breakpt[0] and got 3, which it should be if entering something like
100 + 200. But when I run the for j = 0 to j = < breakpt[0], it
doesn't stop at 2, but keeps going until the index is out of the
array.
If the messagebox says breakpt[0] = 3, why doesn't j stop at 2?
Thanks!!!
Melanie

int i, j;
for(i = 0; i < bigR.Length; i++)
{
if ((bigR == '+') || (bigR == '-') || (bigR == '*') ||
(bigR == '/'))
{breakpt += i;}
}

// breakpt[0] = 3 if type 100+200

MessageBox.Show(breakpt[0].ToString());
for (j = 0; j < breakpt[0]; j++)
{
MessageBox.Show(j.ToString());
nums += bigR[j];
}


What is the type of "breakpt"?
First you use "breakpt += i;" (no [], looks like integer addition)
Then you use "breakpt[0];" so it can't be an integer.

What happens if just before the "j" loop, you do
int max = breakpt[0];
and then end the loop on "j < max" ?


Hans Kesting
 
Hi again,
breakpt is a string that I define earlier in the program (string breakpt =
"";).
When I say breakpt += i, I get a string of numbers that tell me where a +,
-, *, or / was entered. (if I entered 100+200/20 , breakpt would be "37").
Then when I use breakpt[0], it gives me the first character in the string.
I tried the int max thing you suggested, but get the same thing as before -
the second messagebox shows j going from 0 on up until it crashes when it's
out of the array.
I also tried saying
for (j = 0; j < int.Parse(breakpt[0]); j++)
and got an error (The best overloaded method match for 'int.Parse(string)'
has some invalid arguments).
Help! Any ideas???
Thanks!
Melanie

Hans Kesting said:
melanieab said:
Hi,
I'm trying to check and see if something other than numbers (either
the +, -, *, or /) are entered into a textbox, where bigR is what I
call the text in the textbox. I can get what was entered and where
along the string (the where part is the first section of below). I'm
running into trouble just after that. I tested what the value of
breakpt[0] and got 3, which it should be if entering something like
100 + 200. But when I run the for j = 0 to j = < breakpt[0], it
doesn't stop at 2, but keeps going until the index is out of the
array.
If the messagebox says breakpt[0] = 3, why doesn't j stop at 2?
Thanks!!!
Melanie

int i, j;
for(i = 0; i < bigR.Length; i++)
{
if ((bigR == '+') || (bigR == '-') || (bigR == '*') ||
(bigR == '/'))
{breakpt += i;}
}

// breakpt[0] = 3 if type 100+200

MessageBox.Show(breakpt[0].ToString());
for (j = 0; j < breakpt[0]; j++)
{
MessageBox.Show(j.ToString());
nums += bigR[j];
}


What is the type of "breakpt"?
First you use "breakpt += i;" (no [], looks like integer addition)
Then you use "breakpt[0];" so it can't be an integer.

What happens if just before the "j" loop, you do
int max = breakpt[0];
and then end the loop on "j < max" ?


Hans Kesting
 
I also tried
int k = (int)(breakpt[0])
which seems like it should work (converts characters to ints), but isn't .

melanieab said:
Hi again,
breakpt is a string that I define earlier in the program (string breakpt =
"";).
When I say breakpt += i, I get a string of numbers that tell me where a +,
-, *, or / was entered. (if I entered 100+200/20 , breakpt would be "37").
Then when I use breakpt[0], it gives me the first character in the string.
I tried the int max thing you suggested, but get the same thing as before -
the second messagebox shows j going from 0 on up until it crashes when it's
out of the array.
I also tried saying
for (j = 0; j < int.Parse(breakpt[0]); j++)
and got an error (The best overloaded method match for 'int.Parse(string)'
has some invalid arguments).
Help! Any ideas???
Thanks!
Melanie

Hans Kesting said:
melanieab said:
Hi,
I'm trying to check and see if something other than numbers (either
the +, -, *, or /) are entered into a textbox, where bigR is what I
call the text in the textbox. I can get what was entered and where
along the string (the where part is the first section of below). I'm
running into trouble just after that. I tested what the value of
breakpt[0] and got 3, which it should be if entering something like
100 + 200. But when I run the for j = 0 to j = < breakpt[0], it
doesn't stop at 2, but keeps going until the index is out of the
array.
If the messagebox says breakpt[0] = 3, why doesn't j stop at 2?
Thanks!!!
Melanie

int i, j;
for(i = 0; i < bigR.Length; i++)
{
if ((bigR == '+') || (bigR == '-') || (bigR == '*') ||
(bigR == '/'))
{breakpt += i;}
}

// breakpt[0] = 3 if type 100+200

MessageBox.Show(breakpt[0].ToString());
for (j = 0; j < breakpt[0]; j++)
{
MessageBox.Show(j.ToString());
nums += bigR[j];
}


What is the type of "breakpt"?
First you use "breakpt += i;" (no [], looks like integer addition)
Then you use "breakpt[0];" so it can't be an integer.

What happens if just before the "j" loop, you do
int max = breakpt[0];
and then end the loop on "j < max" ?


Hans Kesting
 
melanieab said:
I also tried
int k = (int)(breakpt[0])
which seems like it should work (converts characters to ints), but isn't .

It converts characters to ints by casting - so Unicode character 0 goes
to 0, etc.

If you really want to convert a single character to an integer value,
the easiest way is to just subtract '0' (rather than 0).

I strongly suspect this isn't the best way of accomplishing the bigger
goal though. You shouldn't convert from ints to strings and back again
unless you really need to.
 
Hi,
I guess I don't understand what you mean by "subtract '0'" (sorry, I'm still
new).
Could you explain?
Thanks,
Melanie

Jon Skeet said:
melanieab said:
I also tried
int k = (int)(breakpt[0])
which seems like it should work (converts characters to ints), but isn't .

It converts characters to ints by casting - so Unicode character 0 goes
to 0, etc.

If you really want to convert a single character to an integer value,
the easiest way is to just subtract '0' (rather than 0).

I strongly suspect this isn't the best way of accomplishing the bigger
goal though. You shouldn't convert from ints to strings and back again
unless you really need to.
 
breakpt[0] is a character in a string in the range of '0' to '9'. Subtracting
'0' which is hex 30 from that will give the actual integer value. I'd think
that Convert.ToInt32(breakpt[0]) may work better. Hope this helps.
--
Thom


melanieab said:
Hi,
I guess I don't understand what you mean by "subtract '0'" (sorry, I'm still
new).
Could you explain?
Thanks,
Melanie

Jon Skeet said:
melanieab said:
I also tried
int k = (int)(breakpt[0])
which seems like it should work (converts characters to ints), but isn't .

It converts characters to ints by casting - so Unicode character 0 goes
to 0, etc.

If you really want to convert a single character to an integer value,
the easiest way is to just subtract '0' (rather than 0).

I strongly suspect this isn't the best way of accomplishing the bigger
goal though. You shouldn't convert from ints to strings and back again
unless you really need to.
 
tbain said:
breakpt[0] is a character in a string in the range of '0' to '9'. Subtracting
'0' which is hex 30 from that will give the actual integer value. I'd think
that Convert.ToInt32(breakpt[0]) may work better. Hope this helps.

No, Convert.ToInt32(breakpt[0]) is equivalent to just casting (i.e.
it's a no-op). Try it:

using System;

class Test
{
static void Main()
{
Console.WriteLine (Convert.ToInt32('5'));
}
}

Result: 53
 
melanieab said:
Hi again,
breakpt is a string that I define earlier in the program (string
breakpt = "";).
When I say breakpt += i, I get a string of numbers that tell me where
a +, -, *, or / was entered. (if I entered 100+200/20 , breakpt
would be "37").

So when there is an operator beyond position 9, you will run into trouble ..
Then when I use breakpt[0], it gives me the first
character in the string. I tried the int max thing you suggested, but
get the same thing as before - the second messagebox shows j going
from 0 on up until it crashes when it's out of the array.

What is the value of that max? Is it the value you expected (probably not)?
See also the other replies about Unicode value. If the debugger showed
a char '3', that is not the same as an int 3 !
I also tried saying
for (j = 0; j < int.Parse(breakpt[0]); j++)
and got an error (The best overloaded method match for
'int.Parse(string)' has some invalid arguments).
Help! Any ideas???
Thanks!
Melanie

Some other idea:
don't use a string where you add characters, but use an int[].
You can't change the size on the fly, so you will need to start "big enough",
say the size of your formula.
int[] breakpt = new int[bigR.Length];
Now all elements of the array should have value 0 (I think ..).

You will need a extra counter for the position within the array.
int counter = 0;

When you detect an operator, add the position to the array
and increase the counter
breakpt[counter] = i;
counter++;

When you are processing this breakpt list, stop if you find
a 0 value (or if you arrive at the "counter" position, which should be the same)

This way you are not converting from int to string to char to int.


Hans Kesting

Hans Kesting said:
melanieab said:
Hi,
I'm trying to check and see if something other than numbers (either
the +, -, *, or /) are entered into a textbox, where bigR is what I
call the text in the textbox. I can get what was entered and where
along the string (the where part is the first section of below).
I'm running into trouble just after that. I tested what the value
of breakpt[0] and got 3, which it should be if entering something
like 100 + 200. But when I run the for j = 0 to j = < breakpt[0],
it doesn't stop at 2, but keeps going until the index is out of the
array.
If the messagebox says breakpt[0] = 3, why doesn't j stop at 2?
Thanks!!!
Melanie

int i, j;
for(i = 0; i < bigR.Length; i++)
{
if ((bigR == '+') || (bigR == '-') || (bigR == '*') ||
(bigR == '/'))
{breakpt += i;}
}

// breakpt[0] = 3 if type 100+200

MessageBox.Show(breakpt[0].ToString());
for (j = 0; j < breakpt[0]; j++)
{
MessageBox.Show(j.ToString());
nums += bigR[j];
}


What is the type of "breakpt"?
First you use "breakpt += i;" (no [], looks like integer addition)
Then you use "breakpt[0];" so it can't be an integer.

What happens if just before the "j" loop, you do
int max = breakpt[0];
and then end the loop on "j < max" ?


Hans Kesting
 
Hans Kesting wrote:
[...snip...]
Some other idea:
don't use a string where you add characters, but use an int[].
You can't change the size on the fly, so you will need to start "big enough",
say the size of your formula.
int[] breakpt = new int[bigR.Length];

Maybe using an ArrayList would be appropriate.

ArayList breakpt = new ArrayList();
Now all elements of the array should have value 0 (I think ..).

You will need a extra counter for the position within the array.
int counter = 0;

When you detect an operator, add the position to the array
and increase the counter
breakpt[counter] = i;
counter++;

With an ArrayList, you'd just add the position:

breakpt.Add(i);
When you are processing this breakpt list, stop if you find
a 0 value (or if you arrive at the "counter" position, which should be the
same)

Iterate over the ArrayList using "foreach"... You'd have to cast, though:

foreach (object element in breakpt)
{
int position = (int)element;
// Do whatever you want to do with the positions
}
This way you are not converting from int to string to char to int.
[...snip...]

And that's the most important thing: Store the positions as integers, not as
characters (unless there is a very strong argument for storing the positions
in a string (I can't see any and it's introducing errors)).
 
Michael said:
Hans Kesting wrote:
[...snip...]
Some other idea:
don't use a string where you add characters, but use an int[].
You can't change the size on the fly, so you will need to start "big
enough", say the size of your formula.
int[] breakpt = new int[bigR.Length];

Maybe using an ArrayList would be appropriate.

ArayList breakpt = new ArrayList();

It's easy to use, BUT you need to cast to int (=unbox) when you
want to use the stored values. That's why I chose int[].

Would 2.0 allow an arraylist of int's? That would (probably)
even be better.


Hans Kesting
 
v2.0 allows you to write the following

using System.Collections.Generic;

class App
{
static void Main()
{
List<int> list = new List<int>();
list.Add(5); // no box
int i = list[0]; // no unbox
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

It's easy to use, BUT you need to cast to int (=unbox) when you
want to use the stored values. That's why I chose int[].

Would 2.0 allow an arraylist of int's? That would (probably)
even be better.
 
Michael Voss said:
same)

Iterate over the ArrayList using "foreach"... You'd have to cast, though:

foreach (object element in breakpt)
{
int position = (int)element;
// Do whatever you want to do with the positions
}

You don't have to cast - the foreach can do this for you:

using System;
using System.Collections;

class Test
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add(5);
list.Add(10);

foreach (int x in list)
{
Console.WriteLine (x);
}
}
}
 
Back
Top