Thread: C Programming code help

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    3

    C Programming code help

    Hi all, I'm new to this forum. I'm writing a C program most of which I've finished, just having some difficulty with the start of it.

    Basically I'm asking the user to enter a real number to an accuracy of 4 decimal places. It can't be anything else but that, so if they enter a number with less or more decimal places, without decimal places or enter a word, letter or anything else, they will be prompted again to enter a real number to an accuracy of 4 decimal places.

    Obviously a while loop will be involved to keep prompting the user until a valid value is entered, but I'm not sure what the code for the conditional statement would be to determine whether the value entered is valid/invalid, given the limitations I mentioned.

    And would I apply the conditional statement to scanf alone, or to the variable which has been assigned the value from scanf. Hope that makes sense. Code help/ideas appreciated, thanks.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use fgets() to read data from the user as a string of characters. Then check if the content of those characters meet the requirements. Keep doing that until a string is received that meets requirements.

    It doesn't necessarily have to be a while loop - any loop construct will do. A do-while loop may be appropriate, as it is will be necessary to read data and check it at least once. For example, you might check for the presence of digits,decimal points, sign ('+' or '-'), and the number of digits after the decimal point. You might also reject input that contains unwanted characters (letters, etc).

    Personally, I wouldn't use scanf() for this task at all. And don't make the mistake of trying to use floating point for this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by grumpy View Post
    Use fgets() to read data from the user as a string of characters. Then check if the content of those characters meet the requirements. Keep doing that until a string is received that meets requirements.

    It doesn't necessarily have to be a while loop - any loop construct will do. A do-while loop may be appropriate, as it is will be necessary to read data and check it at least once. For example, you might check for the presence of digits,decimal points, sign ('+' or '-'), and the number of digits after the decimal point. You might also reject input that contains unwanted characters (letters, etc).

    Personally, I wouldn't use scanf() for this task at all. And don't make the mistake of trying to use floating point for this.
    Hi grumpy I'm new to the forum and to C, thanks for your advice. I'll need to do some research on fgets() :] I've never really used the do-while loop so I'll look into that also. Once I get a valid character string input I'll need to do some calculations with it as a float. Any idea what the best way would be to convert the string?

    A friend of mine just mentioned something similar to what you said - that I should take the input as a string, search for a "." character, check that the substring preceding the decimal point consists of any number of digits and the substring following the decimal point is exactly four digit characters. And also that any numerical method won't work because for example, 4.2 and 4.2000 are identical, and because computers use binary numbers, not decimal.
    Last edited by stevesy; 12-21-2014 at 04:01 AM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    OK; what I meant is not to try converting to float BEFORE you've checked the conditions are met. Converting to a float will not enforce your requirements, as your friend has said. Also, a floating point variable cannot accurately represent the value 0.1 either.

    Once you've checked the string is valid, then convert it to a floating point if needed.

    There are various ways - sscanf(), atof(), strtof, strtod() are all standard functions that you might consider. All have different advantages and disadvantages, but all will work.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Dec 2014
    Posts
    3
    Quote Originally Posted by grumpy View Post
    OK; what I meant is not to try converting to float BEFORE you've checked the conditions are met. Converting to a float will not enforce your requirements, as your friend has said. Also, a floating point variable cannot accurately represent the value 0.1 either.

    Once you've checked the string is valid, then convert it to a floating point if needed.

    There are various ways - sscanf(), atof(), strtof, strtod() are all standard functions that you might consider. All have different advantages and disadvantages, but all will work.
    Brilliant, thanks very much grumpy I'll look into all of those options and post some code when I (hopefully) get it working :]
    Cheers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need c programming code for the following specifications
    By VNS Sarvani in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2014, 11:41 AM
  2. Check C Programming Code
    By meeshvl in forum C Programming
    Replies: 7
    Last Post: 09-11-2014, 06:35 AM
  3. modification of C programming code.
    By engr_waqar290 in forum C Programming
    Replies: 7
    Last Post: 04-01-2013, 02:13 AM
  4. Help with programming code!!!
    By Manny01 in forum C Programming
    Replies: 3
    Last Post: 01-08-2012, 06:25 PM
  5. Can someone help me with my C programming lottery code?
    By OvdoMan9 in forum C Programming
    Replies: 2
    Last Post: 09-02-2010, 11:07 PM

Tags for this Thread