Thread: new to C. need basic help

  1. #46
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just as a note, the f at the end of 1.2f is to indicate that the number is a float (rather than the double floating point format, whcih can't be distinguished by the compiler simply from the digits themselves) - so it has nothing to do with the "f-value" that you are working with.

    --
    Mats

  2. #47
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Haha so many people here are from the EP100 class LOL

  3. #48
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    Quote Originally Posted by zacs7 View Post
    Remember that floats are approximates, but that only *should* matter if you do any calculations on it.

    writing a function, or even a macro to check if it's aperture is good, for example
    Code:
    int isValid(char focus, float app)
    {
        int i, a;
        char validFocuses[] = {'f'};
        float validApp[] = {1.2f, 1.4f, 1.8f, 2.0f, 2.8f, 4.0f, 5.6f, 8.0f, 11.0f, 16.0f, 22.0f, 32.0f};
    
        /* check if the focus is valid for each app */
        for(i = 0; i < (sizeof(validFocuses) / sizeof(validFocuses[0])); i++)
        {
            for(a = 0; a < (sizeof(validApp) / sizeof(validApp[0])); a++)
            {
                if(focus == validFocuses[i] && app == validApp[a])
                    return 1;       /* valid */
            }
        }
        return 0;   /* failed, not valid */
    }
    Then you can simply check,
    Code:
    /* ... */
    res = fscanf(input, "%c%f", &focus,&aperture);
    
    if(IsValid(focus, aperture) == 0)
        fprintf(output,"error");
    
    /* ... */
    Remember that '=' is the assignment operator, '==' means "Is Equal to" (use in conditions).
    hey mate.
    i dont understand the bit highlighted

  4. #49
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    my code just prints out errorerrorerrorerror, even if i enter the correct value.

    Code:
     for (i=0;i<(sizeof(validfocus) / sizeof(validfocus[0]));i++)
      {
       for (j=0;j<(sizeof(validaperture) / sizeof(validaperture[0]));j++)
       {
        if ((focus == validfocus[i])&&(aperture == validaperture[j]))
        {
         fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
        }
        else
        {
        fprintf(output,"error");
        }
       }
      }

  5. #50
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    (sizeof(validFocuses) / sizeof(validFocuses[0])) = The size of the focus array, I wasn't sure you would need more than 1 focus value, ie other than 'f' - so I put it in an array anyway.

    It Prints error because you're checking it every iteration, leave it in a function like I demonstrated...

  6. #51
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    ok im confused.
    for now i just want the program to check to see what is valid
    and if they are valid print the value if not print error.

    i know later i can return 1 when i need to go back to the menu.
    but atm i dont have a menu im just creating the test code.

  7. #52
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    ok im confused.
    for now i just want the program to check to see what is valid
    and if they are valid print the value if not print error.

    i know later i can return 1 when i need to go back to the menu.
    but atm i dont have a menu im just creating the test code.
    You obviously only have one valid value (at any given time), so there are a few options:
    - use a function as zacs7 suggests, and let the loop be in the function.
    - use two loops like you've done, but use a variable to say "there's a valid value", which is set initially to "not valid", and when you find a value that is valid, set it to one.

    By the way, the way your loop is designed right now, it tests both values together in one test, which means that you need to test EVERY permutation (n * m, where n is number of shutter speeds and m is number of apertures). But if aperture is in the valid set, and shutter speed is in the valid set, then by definition it is a valid combination. So you could do two loops, one after the other, rather than one inside the other, and only have to do n + m loops. The bigger the n and m values are, the bigger the difference would be. Consider n and m being 10 each, you do 100 loops in the first model, and 20 in the second model. If there's n=100, m=200, then the model one does 20000 tests, and model 2 does 300 tests - that is a BIG difference. [Of course, it doesn't make much difference here, it's just good practice to do not put loops inside each other when they don't need to be].

    --
    Mats

  8. #53
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    like this (although this doesnt work, just prints out random large number)

    Code:
     for (i=0;i<(sizeof(validfocus) / sizeof(validfocus[0]));i++)
      {
       for (j=0;j<(sizeof(validaperture) / sizeof(validaperture[0]));j++)
       {
        if ((focus == validfocus[i])&&(aperture == validaperture[j]))
        {
        // fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
        valid = 1;
        }
       }
      }
      if (valid = 1)
      {
       fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
      }
      else if (valid =0)
      {
       fprintf(output,"error");
      }
    }

  9. #54
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    like this (although this doesnt work, just prints out random large number)

    Code:
     for (i=0;i<(sizeof(validfocus) / sizeof(validfocus[0]));i++)
      {
       for (j=0;j<(sizeof(validaperture) / sizeof(validaperture[0]));j++)
       {
        if ((focus == validfocus[i])&&(aperture == validaperture[j]))
        {
        // fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
        valid = 1;
        }
       }
      }
      if (valid = 1)
    This sets valid to one, then tests if it's true which it always is - you want double = signs to test for equality.
      {
       fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
      }
      else if (valid =0)
    This sets valid to zero, then tests if it's true which it is never - and since this is the only other alternative, why use a second if - assuming valid is set to zero, the above code just sets to one possible other value, 1, which the above code tests for, so if we get past that valid should be zero and nothing else [if it is something else, then it's because of some other bug!] 
      {
       fprintf(output,"error");
      }
    }
    See comments in red above.

    --
    Mats

  10. #55
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    oh yeh. my bad.

    how come nothing prints,
    it still prints the exposure which the printf statement is located before the for loops.

    but for the aperture, it doesnt print anything out, not even an error.

    can anyone explain.

  11. #56
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Just a little note: If you want to help yourself to avoid the "single equal sign error", you could reverse the comparison:
    Code:
       // The following compiles. 
       if( 1 == valid )   ...
       // This gives an error - because you are trying to assign a value to the constant 1, which is forbidden.
       if (1 = valid ) ...
    At first, it looks a bit different, but if you consistenly use this form, then you'll avoid those nasty errors that happen when you assign instead of compare. I don't use this style, but I've seen it many times, and if you are starting out, it's perhaps a good habit to start with.

    --
    Mats

  12. #57
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    Quote Originally Posted by k4k45hi View Post
    oh yeh. my bad.

    how come nothing prints,
    it still prints the exposure which the printf statement is located before the for loops.

    but for the aperture, it doesnt print anything out, not even an error.

    can anyone explain.
    this is whats happening after i change it to ==.
    before it was printing random large numbers.

  13. #58
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Could you post the code, please (the interesting parts, not the whole lot).

    --
    Mats

  14. #59
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    Code:
      int i;
      int j;
      float  exposuretime;
      float aperture;
      char focus;
      int res;
      char validfocus[] = {"f"};
      float validaperture[] = {1.2f,1.4f,1.8f,2.0f,2.8f,4.0f,5.6f,8.0f,11.0f,16.0f,22.0f,32.0f};
      int valid = 0;
    
    
      for (i=0;i<(sizeof(validfocus) / sizeof(validfocus[0]));i++)
      {
       for (j=0;j<(sizeof(validaperture) / sizeof(validaperture[0]));j++)
       {
        if ((focus == validfocus[i])&&(aperture == validaperture[j]))
        {
        // fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
        valid = 1;
        }
       }
      }
      if (valid == 1)
      {
       fprintf(output,"the aperture value is: %c%f\n ",focus,aperture);
      }
      else if (valid ==0)
      {
       fprintf(output,"error");
      }
    }

  15. #60
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
      char validfocus[] = {"f"};
       ...
       if ((focus == validfocus[i])&&(aperture == validaperture[j]))
    I don't think that's what you want. I'm guessing there is only one char that is 'valid', so do this instead:
    Code:
    const char validfocus = 'f';
    ...
    if ((validfocus == focus) && (aperture == validaperture[j]))
    and then lose the outer for loop.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  3. visual basic vs C or C++
    By FOOTOO in forum Windows Programming
    Replies: 5
    Last Post: 02-06-2005, 08:41 PM
  4. Basic Graphics programming in C, please help
    By AdRock in forum Game Programming
    Replies: 3
    Last Post: 03-24-2004, 11:38 PM