CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Join Date
    Mar 2009
    Location
    Michigan
    Posts
    19

    C - <= and >= Problems

    I have to convert an array of characters to a Binary value. Without using any C functions the only way that I know to check if something is an integer or letter is to use the ASCII chart. I followed the numbers in my functions but somehow it is not working out at all. The program executes and falls into every if statement even when it shouldnt.

    Code:
    int convert( const char Val[], int Base , int * InValue )
    {
       int i;
       int count = 0;
       bool negFlag = 0;
       for( i = 0 ;; i++ )
       {
          if ( Val[i] == '\0' )
          {
             break;
          }
          printf ( "%i\n" , Val[i] );
          if (Val[i]<=90 || Val[i]>=65);
          {
          printf ( "%i\n" , count );
             count++;
          }
       }
    
       i = 0;
       int Base10Value = 0;
       int Base10Count = count;
          printf ( "%i\n" , count );
       for( count ; count >= 0 ;  )
       {
          if ((65<=Val[i]<=90)or(97<=Val[i]<=122));
          {
             int BaseToPower = 1 ;
             for ( Base10Count ; ( Base10Count - 1 ) > 0  ; Base10Count-- )
             {
                BaseToPower *= Base;
             }
             if ( 65 <= Val[i] <= 90 )
             {
                Base10Value += ( ((Val[i] - 'A')+10) * BaseToPower);
             }
             if (Val[i]<= 97 )
             {
                Base10Value += ( ((Val[i] - 'a')+10) * BaseToPower);
             }
             count--;
          }
          if ( ( 48 <= Val[i] <= 57 ));
          {
             int BaseToPower = 1 ;
             for ( Base10Count ; ( Base10Count - 1 ) > 0  ; Base10Count-- )
             {
                BaseToPower *= Base;
    
             }
             Base10Value += ( (Val[i] - '0') * BaseToPower);
             count--;
          }
          i++;
       }
    thanks in advance

  2. #2
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: C - <= and >= Problems

    Quote Originally Posted by boydale1 View Post
    Code:
          if (Val[i]<=90 || Val[i]>=65);
    There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  3. #3
    Join Date
    Oct 2009
    Posts
    26

    Re: C - <= and >= Problems

    can you provide your input string also.

  4. #4
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: C - <= and >= Problems

    Quote Originally Posted by treuss View Post
    There are not many numbers which are not either smaller or equal than 90 or greater or equal than 65. For which number do you expect the above to be false?

    Agreed. But you also have a ; at the end of your if statement!!
    Meaning the code in the { } will always run.

    Edit: In fact, you do this numerous times.

  5. #5
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: C - <= and >= Problems

    Quote Originally Posted by boydale1 View Post
    Code:
    if ( 65 <= Val[i] <= 90 )
    This might not do what you expect it to do either.
    it basically evaluates to
    Code:
    if (( true or false ) <= 90)
    you could as well write

    Code:
    if ( true )
    try
    Code:
    if ( Val[i] >= 65 &&  Val[i] <= 90 )
    Kurt

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: C - <= and >= Problems

    Quote Originally Posted by ZuK View Post
    Code:
    if ( Val[i] >= 65 &&  Val[i] <= 90 )
    This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: C - <= and >= Problems

    Quote Originally Posted by ninja9578
    This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code.
    Personally, I feel that it is not ambiguous because the relative precedence of >= and && should be well known, thus grouping with parentheses clutters the code. This is also a matter of taste, since one might think that despite the clutter ensuring against misinterpretation by a reader is more important. As for mathematics: I recall that in the mathematical notation that I was taught, the relative precedence of these operators is the same as in C++; but then mathematics should not be a factor here anyway.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Mar 2009
    Location
    Michigan
    Posts
    19

    Re: C - <= and >= Problems

    The input to the function is as follows.

    Code:
    char A[] = "100";
    int * Value;
    
    
    convert( A, 10, Value );
    Thanks for the help. I cleaned up my code a lot.

    Things like:

    ( 65 <= Val[i] <= 90 )

    I picked that up in python which is correct, but in C doesn't work.

    thanks for the help.
    Last edited by boydale1; October 8th, 2009 at 12:06 PM.

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C - <= and >= Problems

    Quote Originally Posted by ninja9578 View Post
    This is correct in C++, but not mathematics, so even though it would work, I would consider this ambiguous code. I much prefer to use parenthesis in situations like that.
    That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?

  10. #10
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: C - <= and >= Problems

    Quote Originally Posted by boydale1 View Post
    The input to the function is as follows.

    Code:
    char A[] = "100";
    int * Value;
    
    
    convert( A, 10, Value );
    Thanks for the help. I cleaned up my code a lot.

    Things like:

    ( 65 <= Val[i] <= 90 )

    I picked that up in python which is correct, but in C doesn't work.

    thanks for the help.
    You only created a pointer to an int, not an actual int.
    Your program will most likely crash if you try to assign a value to it.

    It should read:
    Code:
    char A[] = "100";
    int Value;
    convert(A,10,&Value);

    Edit: Actually you never even touch *InValue so why are you passing it?
    Nor are you returning any values. I have no idea how to help you.

  11. #11
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C - <= and >= Problems

    Quote Originally Posted by GCDEF View Post
    That's not even slightly ambiguous. Why clutter it up with unnecessary parenthesis?
    I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  12. #12
    Join Date
    Jun 2009
    Location
    oklahoma
    Posts
    199

    Re: C - <= and >= Problems

    I agree that its not ambiguous at all.
    How do the parenthesis force anything?

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: C - <= and >= Problems

    Quote Originally Posted by JohnW@Wessex View Post
    I find the extra parenthesis help with the visual grouping of conditions. More complicated boolean conditions can be fiddly to disassemble without the visual help that parenthesis provide. It also helps when the coder got the operator precedence wrong. The parenthesis force the condition to mean what they actually intended.
    But that case is really simple. To me, the parenthesis imply there's more going on than is readily obvious. Simple is always best IMHO.

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: C - <= and >= Problems

    A simple example

    A && B || C

    You could assume that the coder knows the operator precedence and it does what he intended.

    If he were to type A && (B || C) then it doesn't matter whether the coder remembers && or || has the higher precedence, it will work as intended.
    Now imagine a much longer and more complicated logic test.
    I stick to this principle because it alleviates having to debug silly mistakes and lets me concentrate on the bigger issues.

    If parenthesis are superfluous, then what about spaces and carriage returns?

    Code:
    #include<iostream>
    #include<string>
    int main(){std::string name;std::cout<<"Name?";std::cin>>name;std::cout<<"Hello "<<name;}
    It's not about whether things are superfluous or not, it's about readability and conciseness of meaning;
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: C - <= and >= Problems

    Quote Originally Posted by JohnW@Wessex View Post
    A && (B || C)
    Sure, but the question is whether it's worthwhile to type

    (A) && ((B) || (C)).

    I'd say no; everyone should know that the logical operators have lower precedence than comparison operators.

    If you had a bitwise operator in there, then it might make sense for clarity.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured