strange function behaviour

Discussion in 'C++' started by muser, Oct 13, 2003.

  1. muser

    muser Guest

    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[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.[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    muser, Oct 13, 2003
    #1
    1. Advertising

  2. 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 )){}
    > 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
    =?ISO-8859-1?Q?Juan_Antonio_Dom=EDnguez_P=E9rez?=, Oct 13, 2003
    #2
    1. Advertising

  3. "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 )){}
    > 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[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.[/i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    [i][i][i][i][i][i][i][i][i][i][/i][/i][/i][/i][/i][/i][/i][/i][/i][/i]
    Victor Bazarov, Oct 13, 2003
    #3
  4. > 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
    Christian Janßen, Oct 13, 2003
    #4
  5. muser

    EPerson Guest

    (muser) wrote in message news:<>...
    > 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.

    > [snip]


    --
    Eric Schmidt
    #include <real_cool_sig>
    EPerson, Oct 13, 2003
    #5
  6. muser

    muser Guest

    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 <> wrote in message news:<bmedb7$6k0$>...
    > 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 )){}
    > > 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
    muser, Oct 13, 2003
    #6
  7. 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 ))
    {
    }

    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
    Karl Heinz Buchegger, Oct 13, 2003
    #7
  8. muser

    WW Guest

    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
    WW, Oct 13, 2003
    #8
  9. muser

    muser Guest

    "Christian Janßen" <> wrote in message news:<-ip.de>...
    > > 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)?
    muser, Oct 14, 2003
    #9
  10. muser

    Rolf Magnus Guest

    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.
    Rolf Magnus, Oct 14, 2003
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    23
    Views:
    701
    Dave Thompson
    Nov 20, 2006
  2. Andy Chambers
    Replies:
    1
    Views:
    377
    Daniel Dyer
    May 14, 2007
  3. Paulo da Silva
    Replies:
    8
    Views:
    353
  4. sunrrrise
    Replies:
    2
    Views:
    399
    Chris Angelico
    May 23, 2011
  5. Rahul!!

    strange behaviour w.r.t get function

    Rahul!!, Mar 30, 2011, in forum: Perl Misc
    Replies:
    2
    Views:
    103
    Willem
    Mar 30, 2011
Loading...

Share This Page