C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 30th, 2012, 10:45 AM
InfinityCounter InfinityCounter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 InfinityCounter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old December 30th, 2012, 11:22 AM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,836 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 16 h 15 m 5 sec
Reputation Power: 1774
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.
Comments on this post
InfinityCounter agrees!
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old December 30th, 2012, 12:19 PM
InfinityCounter InfinityCounter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 InfinityCounter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old December 30th, 2012, 12:21 PM
InfinityCounter InfinityCounter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 5 InfinityCounter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old December 30th, 2012, 01:49 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,125 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 16 h 57 m 4 sec
Reputation Power: 1949
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?

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Check variable types

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap