strange function behaviour

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • muser

    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.
  • Juan Antonio Domínguez Pérez

    #2
    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 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;[/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

    Comment

    • Victor Bazarov

      #3
      Re: strange function behaviour

      "muser" <charlie12345@h otmail.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 checkdigitsinad dress( 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.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.[/color]


      Comment

      • Christian Janßen

        #4
        Re: strange function behaviour

        > bool checkdigitsinad dress( 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 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


        Comment

        • EPerson

          #5
          Re: strange function behaviour

          charlie12345@ho tmail.com (muser) wrote in message news:<f9a2a258. 0310130624.24f3 017@posting.goo gle.com>...[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 checkdigitsinad dress( 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_si g>

          Comment

          • muser

            #6
            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@dominp e.com> wrote in message news:<bmedb7$6k 0$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 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;[/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]

            Comment

            • Karl Heinz Buchegger

              #7
              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 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.
              [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

              Comment

              • WW

                #8
                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


                Comment

                • muser

                  #9
                  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 checkdigitsinad dress( 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 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[/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)?

                  Comment

                  • Rolf Magnus

                    #10
                    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.

                    Comment

                    Working...