473,785 Members | 2,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 checkdigitsinad dress( 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.cu stomeraddress[0];

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

strncpy( Newcrecord.cust omeraddress, &record[27], 60 );
Newcrecord.cust omeraddress[61] = '\0';
Newcrecord.cust omeraddress[61] = atol(Newcrecord .customeraddres s);
if( !checkdigitsina ddress( Newcrecord.cust omeraddress )){
prnfile<<"Inval id: 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;Ilf ord;Essex;IG39B y;
00000.025160000 0

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 2491
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 checkdigitsinad dress( 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**********@h otmail.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 checkdigitsinad dress( 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.cu stomeraddress[0];

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

strncpy( Newcrecord.cust omeraddress, &record[27], 60 );
Newcrecord.cust omeraddress[61] = '\0';
Newcrecord.cust omeraddress[61] = atol(Newcrecord .customeraddres s);
if( !checkdigitsina ddress( Newcrecord.cust omeraddress )){
prnfile<<"Inval id: 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;Ilf ord;Essex;IG39B y;
00000.025160000 0

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 checkdigitsinad dress( 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 checkdigitsinad dress( 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**********@ho tmail.com (muser) wrote in message news:<f9******* *************** ***@posting.goo gle.com>...
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 checkdigitsinad dress( 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_si g>
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*****@dominp e.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 checkdigitsinad dress( 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 checkdigitsinad dress( 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 checkdigitsinad dress( 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 checkdigitsinad dress( 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
5613
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 : bigmuls = lambda xs,ys: filter(lambda (x,y):x*y > 25, combine(xs,ys)) combine = lambda xs,ys: map(None, xs*len(ys), dupelms(ys,len(xs))) dupelms = lambda lst,n: reduce(lambda s,t:s+t, map(lambda l,n=n: *n, lst))
24
2038
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
1552
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 Service, with my own class extending from ServiceBase in the normal fashion. The service starts a remoting object (WKO and singleton) which many clients can then make calls on. Clients may also register with events on the server such that the server...
2
8927
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 onclick="javaScript:alert('document.forms(0)='+document.forms(0)); parent.myFunc(document.forms(0));" type="button" value="Open" name="Button" ID="Button"> The strange part is that the debug alert says that the document.forms(0) is an object så all seem to be well. But...
3
2362
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
6
2936
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 have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
31
2642
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
1631
by: g.ankush1 | last post by:
#include <stdio.h> /* 1st example int a() { return 1; }
4
2101
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 this way like: public class UiMsg { public enum MsgType { StatusOk }; public MsgType Type;
8
5321
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. (Sorry, long email) The first two examples are behaving normal, the thirth is strange....... I wrote the following flabbergasting code: #-------------------------------------------------------------
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5380
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.