473,434 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

for loop not stopping when it should

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[i] == '+') || (bigR[i] == '-') || (bigR[i] == '*') || (bigR[i]
== '/'))
{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];
}

Nov 17 '05 #1
12 1473
melanieab wrote:
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[i] == '+') || (bigR[i] == '-') || (bigR[i] == '*') ||
(bigR[i] == '/'))
{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
Nov 17 '05 #2
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" wrote:
melanieab wrote:
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[i] == '+') || (bigR[i] == '-') || (bigR[i] == '*') ||
(bigR[i] == '/'))
{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

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

"melanieab" wrote:
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" wrote:
melanieab wrote:
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[i] == '+') || (bigR[i] == '-') || (bigR[i] == '*') ||
(bigR[i] == '/'))
{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

Nov 17 '05 #4
melanieab <me*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
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 [C# MVP]" wrote:
melanieab <me*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #6
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" wrote:
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 [C# MVP]" wrote:
melanieab <me*******@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #7
tbain <tb***@discussions.microsoft.com> wrote:
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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
melanieab wrote:
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" wrote:
melanieab wrote:
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[i] == '+') || (bigR[i] == '-') || (bigR[i] == '*') ||
(bigR[i] == '/'))
{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

Nov 17 '05 #9
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)).
Nov 17 '05 #10
Michael Voss wrote:
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
Nov 17 '05 #11
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.

Nov 17 '05 #12
Michael Voss <mi********************@lvrREMOVE.CAPSde> wrote:
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
}


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);
}
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Wayne Folta | last post by:
Two observations about PEP-315: 1. It's clever, addresses a definite "wart", and is syntactically similar to try/except. But it's syntax seems like an acquired taste to me. 2. It is a very...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
9
by: rbt | last post by:
What is the appropriate way to break out of this while loop if the for loop finds a match? while 1: for x in xrange(len(group)): try: mix = random.sample(group, x) make_string = ''.join(mix)...
3
by: Jean Pierre Daviau | last post by:
Hi every C one, Which key entry (if there is any) should stop this loop? #include <stdio.h> int main(){ int c, getch(void); while((c = getch()) != EOF){ printf("%c\n", c);
2
by: alxasa | last post by:
Hi, I have a setInterval which executes its command every 10 seconds in a infinite loop. I've got something real basic like: var processes=0; function startme(){ if(stopthisloop>1)
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
18
by: Vijaykumar Dave | last post by:
I have a program for base X power N as under. The problem is that when the range specified in loop is given it works well, but when any character is pressed, it goes to infinite loop. Second...
19
by: Richard | last post by:
Hi All, I copied a script example from http://www.irt.org/script/640.htm into a local .html file. I opened that file first in HTML-kit, which hung (in an infinite loop, I think) when I...
2
daniel aristidou
by: daniel aristidou | last post by:
Hi i wrote code to print records off a datagrid.the code works on all but one of my data grids. The problem is that loop continues without stopping, Causing the program to crash. The only diff...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.