strange function behaviour

M

muser

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 )){}
return false;
for(i=1; i<=2; i++)
if( string != isalpha(string) || string !=
isdigit(string)){}
return false;
for(i= 1; i<=2; i++)
if( string == isalpha(string)){}
for(i= 2; i <=3; i++)
if(string != isalpha(string)){}
return false;
for(i= 1; i<=2; i++)
if( string == isdigit(string)){}
for(i= 2; i <=3; i++)
if(string != isalpha(string) || string !=
isdigit(string)){}
return false;
for( i= 2; i<=3; i++)
if( string == isalpha(string)){}
for(i= 3; i<=4; i++)
if(string != isalpha(string))
return false;
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string != isalpha(string) || string !=
isdigit(string)){}
return false;
for(i= 3; i<=4; i++)
if(string == isalpha(string)){}
for(i=4; i<=5; i++)
if(string != isalpha(string)){}
return false;
for(i= 5; i< 25; i++)
if(string != isalpha(string) && string != ';')
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.
 
?

=?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=

muser said:
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 )){}
return false;
for(i=1; i<=2; i++)
if( string != isalpha(string) || string !=
isdigit(string)){}
return false;


I see you have an incorrect concept: isalpha, isdigit, etc returns true
or false, and so you cant do string (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 == isalpha(string)){}
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 != isalpha(string) || string !=
isdigit(string)){}
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
 
V

Victor Bazarov

muser said:
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 )){}
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 != isalpha(string) || string !=
isdigit(string)){}
return false;
for(i= 1; i<=2; i++)
if( string == isalpha(string)){}
for(i= 2; i <=3; i++)
if(string != isalpha(string)){}
return false;
for(i= 1; i<=2; i++)
if( string == isdigit(string)){}
for(i= 2; i <=3; i++)
if(string != isalpha(string) || string !=
isdigit(string)){}
return false;
for( i= 2; i<=3; i++)
if( string == isalpha(string)){}
for(i= 3; i<=4; i++)
if(string != isalpha(string))
return false;
for(i =2; i<=3; i++)
for(i= 3; i<=4; i++)
if(string != isalpha(string) || string !=
isdigit(string)){}
return false;
for(i= 3; i<=4; i++)
if(string == isalpha(string)){}
for(i=4; i<=5; i++)
if(string != isalpha(string)){}
return false;
for(i= 5; i< 25; i++)
if(string != isalpha(string) && string != ';')
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.
 
C

Christian Janßen

bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string )){}
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 ))
{
}
return false;
...
return true;
}

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

Christian
 
E

EPerson

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


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.
 
M

muser

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 said:
muser said:
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 )){}
return false;
for(i=1; i<=2; i++)
if( string != isalpha(string) || string !=
isdigit(string)){}
return false;


I see you have an incorrect concept: isalpha, isdigit, etc returns true
or false, and so you cant do string (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 == isalpha(string)){}
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 != isalpha(string) || string !=
isdigit(string)){}
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
 
K

Karl Heinz Buchegger

muser said:
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 ))
{
}

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.
 
W

WW

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

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

muser

Christian Janßen said:
bool checkdigitsinaddress( const char* string )
{

for( int i =0; i<=1; i++ )
if( !isdigit( string )){}
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 ))
{
}
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)?
 
R

Rolf Magnus

muser said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Lexical Analysis on C++ 1
function error 6
Reading a file 1
Access violation error 10
The program/ header file contents 2
Reading a file. 3
templated function help 14
TF-IDF 1

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top