Thread: (newbie) converting string to int..

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

    (newbie) converting string to int..

    I'm trying to convert string to int with atoi, but I always get this error when I try to compile:
    test.cpp: In function ‘int main()’:
    test.cpp:11: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int atoi(const char*)’
    Code:
    int main() {
    	cout<<"insert string\n";
    	string a;
    	cin>>a;
    	int b=0;
    	while(a!="s") {
    		b=atoi(a);
    		cout<<b<<"\n";
    		cin>>a;
    	}
    }
    This is prototype, no checking if string is not number or smth.. How to make this prototype to work?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    b=atoi( a.c_str() );
    But consider using a string stream.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Thank you, now you're my new superhero

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM