to convert from string to integer

This is a discussion on to convert from string to integer within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm having some problems trying to change a string into an integer. I've looked up some examples, but can't ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    53

    to convert from string to integer

    Hi,

    I'm having some problems trying to change a string into an integer. I've looked up some examples, but can't seem to find the problem.

    Here's my code:
    Code:
    #include <stdio.h> 
    #include<iostream> 
    #include<iomanip>
    #include<sstream>
    #include<fstream>
    #include<string>
    #include<cctype>
    
    using std:: cout;
    using std:: cin;
    using std:: endl;
    using namespace std;
    
    void conversion(string& text);
    
    
    int main()
    {
    	string textX = "234";
    	conversion(textX);
    	
    	return 0;
    }
    
    
    void conversion(string& text)
    {
    	int number;
    	number = atoi(text);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    The problem is that atoi() takes a const char*, not a std::string, as its argument.

    Here is a simple example that uses a string stream for the conversion:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    int main()
    {
        std::string text("123");    // The string with the number.
        std::stringstream ss(text); // The string stream that is initialised with the string.
        int number;                 // The resultant number.
        ss >> number;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    atoi takes a c type string

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
      int i;
      string number = "321";
    
      i = atoi(number.c_str());
      
      cout << i;
      
      system("PAUSE");
      return 0;
    }

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    You might also want to replace atoi with strtol, since it's safer and better.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Up until recently, I too was also confused on how to convert an int or double into a string internally in a program to generate larger more complex strings that weren't destined for the user, and string streams are definitely the easy way to go.

    Code:
     
    #include <sstream> 
    ...
    int abc = 123 ; 
    char doremi[] = "do re mi" ; 
    ...
    stringstream ss ; 
    
    ss << "ABC, easy as... " << abc << ", simple as... " << doremi <<", baby it's you and me!"  ;
    Todd (showing my age)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. convert from integer to string
    By peter_hii in forum C++ Programming
    Replies: 2
    Last Post: 04-06-2006, 09:28 AM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM
  5. FAQ how do i convert a string to an integer?? (C)
    By Unregistered in forum FAQ Board
    Replies: 1
    Last Post: 12-02-2001, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21