The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Check variable types
Discuss Check variable types in the C Programming forum on Dev Shed. Check variable types C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 30th, 2012, 10:45 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 19 m 53 sec
Reputation Power: 0
|
|
|
Check variable types
I am wondering, how do i prevent users from entering wrong data types.
for example i declare x as an integer but instead of entering a integer they enter a character, and well my program bugs out.
So then can i check to see if they enter the correct data type and if not re-prompt them to enter another value.
Thanks in advance! 
|

December 30th, 2012, 11:22 AM
|
 |
Contributed User
|
|
|
|
|
1. Read everything into a buffer using fgets()
2. Try to convert the string to whatever data type you're interested in.
3. Check the status result of your conversion function.
|

December 30th, 2012, 12:19 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 19 m 53 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by salem 1. Read everything into a buffer using fgets()
2. Try to convert the string to whatever data type you're interested in.
3. Check the status result of your conversion function. |
I am not quite sure what you mean.
1. How do i use fget(), i have never used that function before, could you explain it.
2. that would probably crash the program due to the large size of the number after the program is done with calculations.
3. I didn't use any conversion functions.
|

December 30th, 2012, 12:21 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 5
Time spent in forums: 1 h 19 m 53 sec
Reputation Power: 0
|
|
|
I am not quite sure what you mean.
1. How do i use fget(), i have never used that function before, could you explain it.
2. that would probably crash the program due to the large size of the number after the program is done with calculations.
3. I didn't use any conversion functions.
|

December 30th, 2012, 01:49 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | Originally Posted by InfinityCounter 1. How do i use fget(), i have never used that function before, could you explain it. |
RTFM! ("Read The Manual!")
If your development environment is in Windows, then read the help file. If you're using gcc, then read the man page; man pages are available on-line, so you can Google on, for example, man page fgets.
Quote: | Originally Posted by InfinityCounter 2. that would probably crash the program due to the large size of the number after the program is done with calculations. |
That was not part of the original question. Also, overflow does not normally cause the program to crash, but rather to yield bogus results.
Quote: | Originally Posted by InfinityCounter 3. I didn't use any conversion functions. |
What? Then what did you use? That input is a string of characters, which you somehow converted to an integer value. How did you do that if you didn't use any conversion functions?
Instead of expecting us to read your mind ( HINT: We cannot read your mind!) and trying to make us waste our time by forcing us to play stupid guessing games, why don't you post your code so that we can know what you are talking about?
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|