Thread: new to C. need basic help

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    46

    new to C. need basic help

    hey guys.
    im new to programming and im having a bit of difficulty.


    i am trying to store a value with the form fx.x. eg f1.2, f1.6 etc.
    now the numbers 1.2, 1.6 need to be stored as floating point numbers,
    but when print the value back out it has to be in the form f1.2, f1.6.
    i also need to check for validity to make sure that the number entered is in the form fx

    this is my code so far but at the moment i have no idea how to approach the next bit.
    the value fx is the aperture.

    Code:
    #include <stdio.h>
    
    int main(int argc, char ** argv)
    {
      // Mainline Variable Declarations
      FILE * output = stdout;
      FILE * input = stdin;
    
      float  exposuretime;
      float aperture;
    
    
      fprintf(output,"please enter exposure time: ");
      fscanf(input,"%f",&exposuretime);
    
      fprintf(output,"please enter aperture value: ");
    
    
      fprintf(output,"the exposure time is: %f\n ",exposuretime);
    
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could just use the aperture value itself (e.g 1.4 or 5.6) without the f as input, and just output the value as f1.4

    Alternatively, you will have to use a home-made parsing mechanism - the easiest version is something that reads a string, checks if the first letter is 'f' (or 'F'?), then passes the next element(s) of the string to sscanf() to extract your float number.

    Of course, you'll still have to add the 'f' to the ouput.

    --
    Mats

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Hmm, suspiciously similar to this thread ?

    So basically you're trying to print out a float with a prefix? How about:
    Code:
     
    printf ("f&#37;f\n", exposure);
    and now apply similar logic to scanf...

    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

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    hmm. yeh

    that would work.
    i think i looked past that coz it seamed to simple lol.

    umm. how would i check to see if its valid?

    i would use an if statement.

    but im not sure how to check it using that.

  5. #5
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    That depends on what you want to check for! Write out the constraints on paper first and the if statement will jump out at you ;-)

    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

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Same answer as the other thread. Store them as strings. If you need to do math or something on the numbers, then convert them to floats/doubles... hint: strtod().

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You don't need to store them as strings... scanf can extract the float part of the string during input and you simply re-add the 'f' for the output. That way, you don't need to parse or convert anything.

    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

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One of the problems when attempting to validate data is to "know what's valid". The first step is to sort out comletely invalid values: f-stops can't be negative, and nore is zero a valid value. A sensible check would be 0.9 .. 40.0 as valid values, and anything else "invalid".

    The next level of "more accurate checks" would be to validate that "this is a valid aperture value" - only certain values can be used as an aperture value. Since cameras use either half or 1/3 steps, not all values are actually "possible" apertures. Two full steps doubles the aperture number, e.g. 2.8 is two steps more open than 5.6. One step is 1.4 times the previous value (1.4 is from square-root of 2, which has to do with the fact that to get twice as much or half as much light through an area, you need to half the area, which as long as it's a square or circle, means multiplying/dividing each side by square-root of 2).

    Half a step is approximately 1.2 (square root of 1.4), a third of a step is around 1.11 (cube root of 1.4).


    --
    Mats

  9. #9
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    ok.
    the acceptable values are f1.2, f1.4, f1.8, f2, f2.8, f4, f5.6, f8, f11, f16, f22, f32.

    now when i ask the user to enter an aperture value they will enter a value such as f1.2.
    how to i store the number in that. and how do i check again the acceptable values?


    i was told to look at getting the aperture as two parts (although as one single string e.g.
    f2.8) - the initial character, and then a floating point number.

    although i dont understand what he was talking about.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    The format string in scanf will let you specify characters that shouldn't appear in the variable you're assiging to.
    Code:
    scanf ("&#37;dcm", &number_of_centimetres);
    As an example. If you think about it, you don't need to store the 'f' at all, have scanf throw it away for you and store only the float part.

    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

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, one question is of course: Do you NEED to store the value as a floating point value, or just as a string (as suggested by others). The only good reason to store the aperture as a float is if you need to calculate on it, otherwise just store it as a string - then you could have a list of valid values and compare your string with the "list of valid".

    If you are going to use the aperture as floating point (either because your teacher requests is, or because you need to have values you can calculate with), then you could have a list of float values that are valid. Bear in mind here that floating point values are not precise, so comparisons should be done by using a differentiating function, such as absolute difference between input and table value is less than 0.001 or some such).

    --
    Mats

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    i have to later use this value to calculate exposure time,
    so yes i will have to have use the aperture as a floating point number.

    could you give me an idea as what the code would like like.
    but im really struggling with how to start this off.
    i know what i have to do,
    but to right this in C im not sure.

    i dont need you to write it for me,
    just kinda give me an idea of what the code would look like.
    pseudo code will help.

    if i limit the floating to say 1 decimal place will this then store the right value?


    just need someone to bounce ideas off as i dont know any1 in my class.

    thanks.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    i have to later use this value to calculate exposure time,
    so yes i will have to have use the aperture as a floating point number.

    could you give me an idea as what the code would like like.
    but im really struggling with how to start this off.
    i know what i have to do,
    but to right this in C im not sure.

    i dont need you to write it for me,
    just kinda give me an idea of what the code would look like.
    pseudo code will help.

    if i limit the floating to say 1 decimal place will this then store the right value?


    just need someone to bounce ideas off as i dont know any1 in my class.

    thanks.
    It would probably be a good idea if you try to get friendly with some of your class-mates...

    When you say "limit floating to 1 decimal point", I presume you are referring to the format when using for example printf. This has NOTHING to do with the internal representation of a floating point number. There is no way to change that (other than modifying the design of the processor your are using, which isn't an option). The problem with floating point comparisons is that the number itself is an approximation. If you have the value 1.0, it's quite possible that the ACTUAL value, at least after some calculations[1], is actually 0.9999999999999 or 1.0000000001375 or some such. If you then compare that value EXACTLY with 1.0, then it's not equal. So the solution is to use a calculation where you compare the difference (or the quotient) of two values, rather than an "equal" or "not equal" comparison.

    One famous "problem with floating point" is 0.7 - it can not be described precisely in floating point, it generates an infinitely repeating patters (just like 1/3 gives 0.333333...), 0.7 becoems 0.6999...

    As to some ideas of how you should solve your problem: Use an array of values, and then verify that the value entered by the user is one of the values in your array.

    [1] You may then say that "I just read the value in, not using any calculations on it", but the fact of the matter is that scanf() and similar functions use calculations to form the number, so yes, the number is CALCULATED if it's an input.

    --
    Mats

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    46
    this is what i now have.
    although the aperture does not work,
    it comes with a segmentation fault.
    is what im doing correct or am i on the wrong track.
    atm im not testing the aperture,
    just getting the input.



    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(int argc, char ** argv)
    
    
    {
      // Mainline Variable Declarations
      FILE * output = stdout;
      FILE * input = stdin;
    
      float  exposuretime;
      float aperture;
      char aperturef;
    
     fprintf(output,"please enter exposure time: ");
      fscanf(input,"%f",&exposuretime);
    
    
      fprintf(output,"please enter aperture value: ");
      fscanf(input,"%f",&aperture);
      fscanf(input,"%s",&aperturef);
    
    
      fprintf(output,"the exposure time is: %f\n ",exposuretime);
      fprintf(output,"the aperture value is %s%f\n ",aperturef, aperture);
    
    }

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by k4k45hi View Post
    this is what i now have.
    although the aperture does not work,
    it comes with a segmentation fault.
    is what im doing correct or am i on the wrong track.
    atm im not testing the aperture,
    just getting the input.



    Code:
    ...
      float  exposuretime;
      float aperture;
      char aperturef;
    ...
      fscanf(input,"%s",&aperturef);
    ...
      fprintf(output,"the aperture value is %s%f\n ",aperturef, aperture);
    
    }
    Reading a string into a single char will certainly overwrite some other data. Also, using fscanf() is dangerous way to read strings if too much input is given.

    printing a single character as a string is also bound to fail sooner or later.

    --
    Mats

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