Thread: Handling User Input

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    3

    Post Handling User Input

    I'm writing a small program in which a user is to input different data about themselves and then a funny quote pops up. My problem is that, for some question I want the person to enter a number and at other times words. My program works fine except when a person enters a string when I am expecting a number like an int. Is there some way that I can check the type of data that was inputted by the user before the computer tries to store the data in the int pointer.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    157
    you could read each char that the user inputs and then test for values (from '0' - '9', etc..). don't forget the value of 0 is different form the value of '0'. one is a char, the other an int.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read all data into a char array, then convert the ones you want to an int with strtol() or atoi() or something similar

    Use fgets() to do the reading too, not scanf().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    3
    Thanx for the help!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Ending user input with # character
    By jowatkins in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2004, 10:41 AM
  4. Handling input from cin
    By Zarkon in forum C++ Programming
    Replies: 1
    Last Post: 12-17-2001, 08:03 AM