GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Nov-2005, 04:19
zanyzanny zanyzanny is offline
New Member
 
Join Date: Nov 2005
Posts: 6
zanyzanny is on a distinguished road
Red face

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  
Old 10-Nov-2005, 06:00
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

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:
#include<stdio.h>
int main()
{
    float f;
    int i;
    
    printf("Enter a number(only whole numbers accepted)...");
    scanf("%f",&f);
    
    i = f;
    printf("The whole number is %d \n",i);
    
    return 0;
}

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  
Old 10-Nov-2005, 06:33
zanyzanny zanyzanny is offline
New Member
 
Join Date: Nov 2005
Posts: 6
zanyzanny is on a distinguished road

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  
Old 10-Nov-2005, 06:46
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 929
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: c programming, how to input a whole number?


Quote:
Originally Posted by Zany
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?
Yes.


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  
Old 10-Nov-2005, 10:23
Guidelines Plz Guidelines Plz is offline
Junior Member
 
Join Date: Sep 2005
Posts: 87
Guidelines Plz is on a distinguished road

Re: c programming, how to input a whole number?


Quote:
Originally Posted by zanyzanny
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
You could try reading the post that tells you how... found easily at the top of the forum thread.
__________________

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  
Old 10-Nov-2005, 10:28
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: c programming, how to input a whole number?


Quote:
Originally Posted by Paramesh
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.
And what happens if the user enters a letter just to be ornery? This info on scanf(). It's part of a series explaining the trouble using scanf() for user-input.

Never underestimate the cruelty of a user
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 20:08.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.