Thread: to convert from string to integer

  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
    28,413
    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;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Chappell Hill, Texas
    Posts
    2,332
    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)
    Mainframe assembler programmer by trade. C coder when I can.

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, 11: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, 05:03 PM