![]() |
|
#1
|
|||
|
|||
c programming, how to input a whole number?Hi,
C programming is one of several modules I'm doing on a course so I won't be too keyed up on it but I have 3 assignments to do. I have the basic program working but what I am after is how to improve it. I would like to know how can I allow the user to input only whole numbers eg 2, 65, 36, etcetc. My programme can not work with decimals being inputted and so I would like that if a decimal is inputted that the programme goes through a loop of asking please input a whole number until a whole number is inputted. So far I have tried making the input a float and then doing the inputted number/1 but using % to look at the remainder (my theory was if remainder is not equal to 0 it is not a whole number so then loop) but then I found this dosent work (i think coz its a float u wouldnt have a remainder but it would just enter the entire number when dividing) Sorry I have gone on a bit here its a simple question really, how to prevent the user entering non whole numbers. I do not have access to the code right now and I wouldnt know how to put it up here in a scroll bar thingy, I hope I'm not breaking forum rules too much hope someone can help I will keep searching to see if anyone else has asked this question before on this or other websites, so far no such luck tho! |
|
#2
|
||||
|
||||
Re: c programming, how to input a whole number?Hi Zany,
Welcome to the GID forums. Instead of telling the user that he has to input only whole numbers, you can ignore the decimal points by just using an integer. so, get the float input. Then make an integer equal to the float. Consider this sample program: CPP / C++ / C Code:
Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: c programming, how to input a whole number?I hadn't thought of it that way round! so even if they put in a decimal it will only look at the whole number part?
thanks i will try this out as soon as i can! |
|
#4
|
||||
|
||||
Re: c programming, how to input a whole number?Quote:
But there can also be better methods than this one. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#5
|
|||
|
|||
Re: c programming, how to input a whole number?Quote:
__________________
Please read http://www.gidforums.com/t-5566.html. They were written to help you create a request that is readable and has enough information we can actually tell what you need help with. |
|
#6
|
||||
|
||||
Re: c programming, how to input a whole number?Quote:
Never underestimate the cruelty of a user __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Binary number systems: BCD, twos complement, ones complement, etc. | machinated | Miscellaneous Programming Forum | 6 | 08-Feb-2006 11:51 |
| Knights Tour - Reloaded . | kobi_hikri | C Programming Language | 12 | 03-Oct-2005 13:15 |
| Getinput error. Error 2660. 4244, and 2447 | blackstone99 | C++ Forum | 1 | 13-Feb-2005 20:11 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 21:25 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The