Velocity Reviews > C++ > strange function behaviour

# strange function behaviour

muser
Guest
Posts: n/a

 10-13-2003
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

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

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?

=?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=
Guest
Posts: n/a

 10-13-2003
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...

Victor Bazarov
Guest
Posts: n/a

 10-13-2003
"muser" <(E-Mail Removed)> 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
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
>
> bool Processcrecord( ofstream& prnfile, ofstream& validdata, char*
> record )
> {
>
> strncpy( Newcrecord.customeraddress, &record[27], 60 );
> 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.

Christian Janßen
Guest
Posts: n/a

 10-13-2003
> 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

EPerson
Guest
Posts: n/a

 10-13-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (muser) wrote in message news:<(E-Mail Removed) 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>

muser
Guest
Posts: n/a

 10-13-2003
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
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 <(E-Mail Removed)> wrote in message news:<bmedb7\$6k0\$(E-Mail Removed)>...
> 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...

Karl Heinz Buchegger
Guest
Posts: n/a

 10-13-2003

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.

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
> 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
(E-Mail Removed)

WW
Guest
Posts: n/a

 10-13-2003
Karl Heinz Buchegger wrote:

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

--
WW aka Attila

muser
Guest
Posts: n/a

 10-14-2003
"Christian Janßen" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)-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)?

Rolf Magnus
Guest
Posts: n/a

 10-14-2003
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:ush(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.