473,321 Members | 1,669 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,321 software developers and data experts.

strange function behaviour

I have written a function that checks the first four characters in an
address are valid; i.e. 1d2 sour st would prove to be invalid.

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false;
for(i=1; i<=2; i++)
if( string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for(i= 1; i<=2; i++)
if( string[i] == isalpha(string[i])){}
for(i= 2; i <=3; i++)
if(string[i] != isalpha(string[i])){}
return false;
for(i= 1; i<=2; i++)
if( string[i] == isdigit(string[i])){}
for(i= 2; i <=3; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for( i= 2; i<=3; i++)
if( string[i] == isalpha(string[i])){}
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]))
return false;
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for(i= 3; i<=4; i++)
if(string[i] == isalpha(string[i])){}
for(i=4; i<=5; i++)
if(string[i] != isalpha(string[i])){}
return false;
for(i= 5; i< 25; i++)
if(string[i] != isalpha(string[i]) && string[i] != ';')
return false;


return true;

}
const char* string, is globally declared. const char* string
=&Newcrecord.customeraddress[0];

bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{

strncpy( Newcrecord.customeraddress, &record[27], 60 );
Newcrecord.customeraddress[61] = '\0';
Newcrecord.customeraddress[61] = atol(Newcrecord.customeraddress);
if( !checkdigitsinaddress( Newcrecord.customeraddress )){
prnfile<<"Invalid: incorrect format for customer address, address
must begin with numerical format:\n";
prnfile<< record <<endl;
}
return false;

there are entries and validdata is used, but none of thaat is causing
the problem. Record is the string of a file save on disk.

the line I'm reading from is:
c23454stevenlaw 12sceptrerd;Ilford;Essex;IG39By;
00000.0251600000

the address as you can see starts with a numerical entry but i still
get this return as false when I run the program. Any ideas on why that
is?

Thank you for your support.
Jul 19 '05 #1
9 2470
muser wrote:
I have written a function that checks the first four characters in an
address are valid; i.e. 1d2 sour st would prove to be invalid.

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false;
for(i=1; i<=2; i++)
if( string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;


I see you have an incorrect concept: isalpha, isdigit, etc returns true
or false, and so you cant do string[i] (character) == isalpha(...
that is boolean.
And the point you mention about that the address begins with a digit and
the function is not returning false.... your code is incorrect:
if (!isdigit(... return false; if it is a digit, this if cannot
be true never!!!

Plase read with more attention your code: it has a lot of superfluous
for instructions:
for (i=1;i<2;i++ ) <---- this only executes once!!! remove the for
and place the value of i (1) directly

What do you want this for??
for( i= 2; i<=3; i++)
if( string[i] == isalpha(string[i])){}
Its totally useless. No action is performed.

This is doing nothing and the return is executed every time the run
comes here!!!
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false; <---- Correct indenting of the code

And for only 0,1,2,3 indexes, no need to do so much for instructions. I
dont write the solution for your problem because it seems to be a home
work... :D

Jul 19 '05 #2
"muser" <ch**********@hotmail.com> wrote...
I have written a function that checks the first four characters in an
address are valid; i.e. 1d2 sour st would prove to be invalid.

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false;
The three statements above are the only meaningful statements in
your function. Others are _unreachable_. Your 'if' statement on
the second line has an empty control statement, and then 'return'
executes regardless of whether 'if' worked or not.

Start by dropping the {} after the 'if's parentheses.
for(i=1; i<=2; i++)
if( string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for(i= 1; i<=2; i++)
if( string[i] == isalpha(string[i])){}
for(i= 2; i <=3; i++)
if(string[i] != isalpha(string[i])){}
return false;
for(i= 1; i<=2; i++)
if( string[i] == isdigit(string[i])){}
for(i= 2; i <=3; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for( i= 2; i<=3; i++)
if( string[i] == isalpha(string[i])){}
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]))
return false;
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;
for(i= 3; i<=4; i++)
if(string[i] == isalpha(string[i])){}
for(i=4; i<=5; i++)
if(string[i] != isalpha(string[i])){}
return false;
for(i= 5; i< 25; i++)
if(string[i] != isalpha(string[i]) && string[i] != ';')
return false;


return true;

}
const char* string, is globally declared. const char* string
=&Newcrecord.customeraddress[0];

bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
record )
{

strncpy( Newcrecord.customeraddress, &record[27], 60 );
Newcrecord.customeraddress[61] = '\0';
Newcrecord.customeraddress[61] = atol(Newcrecord.customeraddress);
if( !checkdigitsinaddress( Newcrecord.customeraddress )){
prnfile<<"Invalid: incorrect format for customer address, address
must begin with numerical format:\n";
prnfile<< record <<endl;
}
return false;

there are entries and validdata is used, but none of thaat is causing
the problem. Record is the string of a file save on disk.

the line I'm reading from is:
c23454stevenlaw 12sceptrerd;Ilford;Essex;IG39By;
00000.0251600000

the address as you can see starts with a numerical entry but i still
get this return as false when I run the program. Any ideas on why that
is?

Thank you for your support.

Jul 19 '05 #3
> bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false; [snip] ...
return true;
[/snip]


when i format your code in a readable way i can see:

bool checkdigitsinaddress( const char* string )
{
for( int i =0; i<=1; i++ )
if( !isdigit( string[i] ))
{
}
return false;
...
return true;
}

As you can see, your function always returns false...

Christian
Jul 19 '05 #4
ch**********@hotmail.com (muser) wrote in message news:<f9*************************@posting.google.c om>...
I have written a function that checks the first four characters in an
address are valid; i.e. 1d2 sour st would prove to be invalid.

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
Nothing named isdigit has been declared. Also the if statement
controls an empty block {}, so nothing happens even if it evaluates
true.
return false;
This statement is always executed. Execution will never reach past
here.
for(i=1; i<=2; i++)
The variable i is no longer in scope.
[snip]


--
Eric Schmidt
#include <real_cool_sig>
Jul 19 '05 #5
Juan i get the feeling you've misunderstood me. I want the function to
return true, not false. the for( i= 1; i<=2; i++) surely that
increments to the next element in the array? That was the whole
thinking when I wrote the code.
The exact problem I'm having is that this function is not picking up
the fact that the line it is suppose to be reading does start with a
digit: i.e. 12sceptrerd. I want the line from the file I'm reading, to
be valid, to check that the program i've written is correct.
As my original post quite clearly implies if you can help please do.


Juan Antonio Domínguez Pérez <do*****@dominpe.com> wrote in message news:<bm**********@news.ya.com>...
muser wrote:
I have written a function that checks the first four characters in an
address are valid; i.e. 1d2 sour st would prove to be invalid.

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false;
for(i=1; i<=2; i++)
if( string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false;


I see you have an incorrect concept: isalpha, isdigit, etc returns true
or false, and so you cant do string[i] (character) == isalpha(...
that is boolean.
And the point you mention about that the address begins with a digit and
the function is not returning false.... your code is incorrect:
if (!isdigit(... return false; if it is a digit, this if cannot
be true never!!!

Plase read with more attention your code: it has a lot of superfluous
for instructions:
for (i=1;i<2;i++ ) <---- this only executes once!!! remove the for
and place the value of i (1) directly

What do you want this for??
for( i= 2; i<=3; i++)
if( string[i] == isalpha(string[i])){}
Its totally useless. No action is performed.

This is doing nothing and the return is executed every time the run
comes here!!!
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string[i] != isalpha(string[i]) || string[i] !=
isdigit(string[i])){}
return false; <---- Correct indenting of the code

And for only 0,1,2,3 indexes, no need to do so much for instructions. I
dont write the solution for your problem because it seems to be a home
work... :D

Jul 19 '05 #6


muser wrote:

Juan i get the feeling you've misunderstood me.
I don't think so.
I want the function to
return true, not false.
OK.
Then write the function the way you want it to behave.
At the moment you wrote your function such that it always
returns false. If this is not what you ment, rewrite the function.

But: Start with *indenting* your code !!!!!

A lot of troubles come from the fact, that your code right now
is unreadable. In order to analyze it, one needs to reindent it.
Well, if we need to reindent it, in order to analyze it, then
you definitly have to reindent it!
the for( i= 1; i<=2; i++) surely that
increments to the next element in the array?
That was the whole
thinking when I wrote the code.
The exact problem I'm having is that this function is not picking up
the fact that the line it is suppose to be reading does start with a
digit: i.e. 12sceptrerd.
The exact problem is that your function doesn't do what you think it does.
At the moment, as it is written now, your function does exactly:

bool checkdigitsinaddress( const char* string )
{
for( int i =0; i<=1; i++ )
if( !isdigit( string[i] ))
{
}

return false;
}

and nothing else.
Proper indentation shows this very clearly.
I want the line from the file I'm reading, to
be valid, to check that the program i've written is correct.
As my original post quite clearly implies if you can help please do.


I still think you need to learn a lot before you can even think about
solving that exact problem. You try to swallow to much.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #7
WW
Karl Heinz Buchegger wrote:
But: Start with *indenting* your code !!!!!


Note: If the code is indented using TABs, OutOfLuck and many other
newsreaders will eat the TABs away. :-(

--
WW aka Attila
Jul 19 '05 #8
"Christian Janßen" <cj@christian-janszen.de> wrote in message news:<3f********@news.arcor-ip.de>...
bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string[i] )){}
return false;

[snip]
...
return true;
[/snip]


when i format your code in a readable way i can see:

bool checkdigitsinaddress( const char* string )
{
for( int i =0; i<=1; i++ )
if( !isdigit( string[i] ))
{
}
return false;
...
return true;
}

As you can see, your function always returns false...

Christian


Thank you christian, why I didn't indent it was because it couldn't
fit on the newsgroup pages any other way. When you put it like you
have done it makes my post look incredibly stupid. I'm currently
trying to make sense of Bjarne stroustup C++, and there was another
problem I had with referencing the const char string with a struct
memeber and all the while i was writing this function.
With learning C++, you always have to take an object view of things, I
sometimes can't see the wood for the trees, I wonder if that is common
problem.
Also why I have your attention, can you tell me what templates
actually do
template<class T> void stack<T>:: push(T c). Is void stack, a member
function of class T, and is stack<T> accessing another function in
push(T c)?
Jul 19 '05 #9
muser wrote:
Thank you christian, why I didn't indent it was because it couldn't
fit on the newsgroup pages any other way. When you put it like you
have done it makes my post look incredibly stupid. I'm currently
trying to make sense of Bjarne stroustup C++, and there was another
problem I had with referencing the const char string with a struct
memeber and all the while i was writing this function.
With learning C++, you always have to take an object view of things, I
sometimes can't see the wood for the trees, I wonder if that is common
problem. Also why I have your attention, can you tell me what templates
actually do
template<class T> void stack<T>:: push(T c). Is void stack, a member
function of class T, and is stack<T> accessing another function in
push(T c)?


No. stack is the class, T is the template parameter and push is the
member function. Let's see how a non-template member function would
look:

void stack::push(int C);

Now if your stack is a template and you want the int to be replaced by
the template parameter, you get the above.
Basically that all means that stack is a class for which you can specify
the element type. So:

stack<int> s1;

creates a stack of ints, while:

class foo {};
stack<foo> s2;

creates a stack of objects of class foo. Now the above push function
takes as parameter an object of the element type of your stack.

Btw: Don't try to learn everything at the same time. Grab one concept
after another. Just learn how to use the standard containers, but don't
try to understand every detail of templates now. Save that for later.

Jul 19 '05 #10

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

Similar topics

4
by: Ben | last post by:
Hi all, I'm trying to figure out how how complex map, filter and reduce work based on the following piece of code from http://www-106.ibm.com/developerworks/linux/library/l-prog.html : ...
24
by: brian.bird | last post by:
Can anyone explain the behaviour of python when running this script? >>> def method(n, bits=): .... bits.append(n) .... print bits .... >>> method(1) >>> method(2)
0
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows...
2
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input...
3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
31
by: DeltaOne | last post by:
#include<stdio.h> typedef struct test{ int i; int j; }test; main(){ test var; var.i=10; var.j=20;
23
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
4
by: Gotch | last post by:
Hi, I'm getting a very strange behaviour while running a project I've done.... Let's expose it: I've two projects. Both of them use a Form to do some Gui stuff. Other threads pack up messages...
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.