Thread: Reading input as an integer

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Question Reading input as an integer

    I have a (hopefully) simple question about getting some input from the user. I need to take input from the user and convert it to an integer.

    Any ideas?

  2. #2
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    the atoi() function is the thing you are looking for if the input is in char form
    -

  3. #3
    Registered User OxYgEn-22's Avatar
    Join Date
    Apr 2002
    Posts
    36
    and itoa() if you want to go the other way
    Is that air you're breathing?

  4. #4
    Unregistered
    Guest
    the >> operator will place user input directly into an int variable, assuming the input is appropriate length, and consists only of numerals. If you try to put inappropriate input into an int variable it may cause any of a number of things to happen, not the worst of which is crash your program. Therefore, one approach is to accept all input as a string, and then parse the string for erroneous input, and convert it to the desired type when adequately evaluated. (There are other ways to ensure data entry integrity, too, but that's a different story).

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Thumbs up Thanks

    Excellent! Thank you all for your help, I'll put this to use immediately.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. 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
  3. Problem reading input
    By gp364481 in forum C Programming
    Replies: 3
    Last Post: 09-24-2008, 05:10 PM
  4. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:43 AM
  5. Reading the input file into integer arrays
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 12-06-2002, 07:04 PM