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];
} 12 1476
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
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(break pt[0]); j++)
and got an error (The best overloaded method match for 'int.Parse(stri ng)'
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
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(break pt[0]); j++) and got an error (The best overloaded method match for 'int.Parse(stri ng)' 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
melanieab <me*******@disc ussions.microso ft.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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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*******@disc ussions.microso ft.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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
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*******@disc ussions.microso ft.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.co m> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
tbain <tb***@discussi ons.microsoft.c om> 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.WriteLi ne (Convert.ToInt3 2('5'));
}
}
Result: 53
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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(break pt[0]); j++) and got an error (The best overloaded method match for 'int.Parse(stri ng)' 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
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)). This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 general construct, which might be what is called for.
But I wonder if most of the time it would be used to accomplish
something like:
|
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 have a programmable exit condition. It keeps
looping till the channels in its socket map (a dictionary) are closed
and don't have any pending...
|
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)
n = md5.new(make_string)
match = n.hexdigest()
|
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);
|
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)
| |
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 hoping someone
could point me in the right direction.
Code:
#include <stdio.h>
#include <math.h>
|
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 problem is it works fine for smaller value of X or N but not
for higher say base 15 power 20. How can I get true value displayed
with such higher base...
|
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 previewed the example.
On the off chance that example exposed a weakness in HTML-Kit, I ran
the example in IE7, which also hung.
The example seems to...
|
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 between all of the grids is the query and the widows form it is on.
I basicly just copied the form changing the SQL for the datasource.
any way so here...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |