(newbie) converting string to int..

This is a discussion on (newbie) converting string to int.. within the C++ Programming forums, part of the General Programming Boards category; I'm trying to convert string to int with atoi, but I always get this error when I try to compile: ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,762
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 04: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

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