Thread: Convert to int

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    9

    Convert to int

    hello
    can anybody say the code required to convert a string to integer datatype

    that is get a number as a string type and convert it integer

    ex
    char num[10];
    cout<<"Enter a number: ";
    cin>>num;

    // convert num to int datatype so that arithmetic calcs is possible

  2. #2
    Drunken Progammer CaptainMorgan's Avatar
    Join Date
    Feb 2006
    Location
    On The Rocks
    Posts
    45

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    A char can hold a small integer type, but captainMorgans link is useful to you
    Double Helix STL

  4. #4
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196
    it puzzles me why dont you start with int from the beginning and save yourself the trouble of converting? because "cin" can take int input also...
    nextus, the samurai warrior

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because forum members keep recommending doing it the way he does it?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    That is simply wasteful of time and resources. Just start out with an int. If for some reson you need to use C style strings, there is a nonstandard function atoi that will convert for you.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  7. #7
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I think strtol() is better...
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > it puzzles me why dont you start with int from the beginning and save yourself the trouble of converting?
    Because functions which do input AND conversion have such complicated error recovery procedures (which almost nobody follows, and which noobs certainly don't).

    Reading the input into a string, then attempting the conversion allows you to separate input errors from conversion errors.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM