Connecting Tech Pros Worldwide Help | Site Map

strange function behaviour

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:11 PM
muser
Guest
 
Posts: n/a
Default 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.

  #2  
Old July 19th, 2005, 07:11 PM
Juan Antonio Domínguez Pérez
Guest
 
Posts: n/a
Default Re: strange function behaviour

muser wrote:
[color=blue]
> 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;[/color]

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

  #3  
Old July 19th, 2005, 07:11 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: strange function behaviour

"muser" <charlie12345@hotmail.com> wrote...[color=blue]
> 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;[/color]

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.
[color=blue]
> 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.[/color]


  #4  
Old July 19th, 2005, 07:11 PM
Christian Janßen
Guest
 
Posts: n/a
Default Re: strange function behaviour

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

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


  #5  
Old July 19th, 2005, 07:11 PM
EPerson
Guest
 
Posts: n/a
Default Re: strange function behaviour

charlie12345@hotmail.com (muser) wrote in message news:<f9a2a258.0310130624.24f3017@posting.google.c om>...[color=blue]
> 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] )){}[/color]

Nothing named isdigit has been declared. Also the if statement
controls an empty block {}, so nothing happens even if it evaluates
true.
[color=blue]
> return false;[/color]

This statement is always executed. Execution will never reach past
here.
[color=blue]
> for(i=1; i<=2; i++)[/color]

The variable i is no longer in scope.
[color=blue]
> [snip][/color]

--
Eric Schmidt
#include <real_cool_sig>
  #6  
Old July 19th, 2005, 07:11 PM
muser
Guest
 
Posts: n/a
Default Re: strange function behaviour

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 <dominpe@dominpe.com> wrote in message news:<bmedb7$6k0$1@news.ya.com>...[color=blue]
> muser wrote:
>[color=green]
> > 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;[/color]
>
> 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[/color]
  #7  
Old July 19th, 2005, 07:11 PM
Karl Heinz Buchegger
Guest
 
Posts: n/a
Default Re: strange function behaviour



muser wrote:[color=blue]
>
> Juan i get the feeling you've misunderstood me.[/color]

I don't think so.
[color=blue]
> I want the function to
> return true, not false.[/color]

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!
[color=blue]
> 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.[/color]

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.
[color=blue]
> 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.[/color]

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
kbuchegg@gascad.at
  #8  
Old July 19th, 2005, 07:11 PM
WW
Guest
 
Posts: n/a
Default Re: strange function behaviour

Karl Heinz Buchegger wrote:[color=blue]
> But: Start with *indenting* your code !!!!![/color]

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

--
WW aka Attila


  #9  
Old July 19th, 2005, 07:12 PM
muser
Guest
 
Posts: n/a
Default Re: strange function behaviour

"Christian Janßen" <cj@christian-janszen.de> wrote in message news:<3f8ac242_1@news.arcor-ip.de>...[color=blue][color=green]
> > bool checkdigitsinaddress( const char* string )
> > {
> >
> > for( int i =0; i<=1; i++ )
> > if( !isdigit( string[i] )){}
> > return false;[/color]
> [snip][color=green]
> > ...
> > return true;
> >[/snip][/color]
>
> 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[/color]

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)?
  #10  
Old July 19th, 2005, 07:12 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: strange function behaviour

muser wrote:
[color=blue]
> 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.[/color]
[color=blue]
> 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)?[/color]

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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.