Thread: Converting strings to ints

  1. #1
    Unregistered
    Guest

    Converting strings to ints

    Is there any chance anybody could help me by showing me how to convert a string to an int in c++, and also how to convert a string to a char array?

    Thanks in advance

    Devs

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    You see the button labeled "search" up there? Try using that one the next time...

    Anyway, for the question:

    Code:
    char string[50];
    int inty;
    inty = atoi(string);

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Ooops i missed the string to array part...

    does strcpy() work with strings?
    I dont think so, i dont use strings.....

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: Converting strings to ints

    Originally posted by Unregistered
    also how to convert a string to a char array?
    A string IS an array of chars...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Unregistered
    Guest
    Mate i know that much, but for this program i have to write i am given a string which i then must turn into a char array which is the EXACT size of the string im given. Then as this string is numerical i have to then convert the char array to the numerical value using the atoi method, however its the converting to a char array of exact size i cant do.

    sorry bout not being detailed before.

    thanks for the responce

    Devs

  6. #6
    Unregistered
    Guest
    char * newString = new char[strlen(oldString)];
    strcpy(newString, oldString);

    //depending on how many digits in oldString and whether integer
    //or decimal number allowed use one of the following
    int inty = atoi(newString);
    long longy = atoi(newString);
    float floaty = atof(newString);

    //do dah

    delete [] newString;

  7. #7
    Unregistered
    Guest
    Cheers Guys!

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char * newString = new char[strlen(oldString)+1];
    Remember the NULL-terminating character
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to handle ints and strings when writing to file
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 04-23-2008, 04:44 AM
  2. converting a vector of strings into an array.
    By LightsOut06 in forum C++ Programming
    Replies: 2
    Last Post: 10-27-2005, 07:14 PM
  3. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  4. Strings into Ints
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2002, 04:07 PM
  5. converting strings to ints
    By HomerJ in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2002, 06:08 PM