The write program needs to convert the string to int, so I explored it.
Method One: Atoi function
The Atoi function converts a string to an integer, noting that the Stdlib library is required. So I tried it on a bit:
1#include <iostream>2#include <string.h>3#include <stdlib.h>4 using namespacestd;5 intMain ()6 {7 stringA=" One", b=" A";8Cout<<atoi (a) +atoi (b) <<Endl;9 return 0;Ten One}
However, the error was found:
Obviously, what Atoi needs is a const char* type, and I'm going to add a function string.c_str () to the above string type. String.c_str is a function in the Borland encapsulated string class that returns the first character address of the current string.
The return value of the C_STR function is const char*, so we add the C_str () function:
#include <iostream><string.h><stdlib.h>usingnamespace std; int Main () { string a="One", b=" ; cout<<atoi (A.c_str ()) +atoi (B.c_str ()) <<Endl; return 0 ;}
And then succeed, what's wrong with the hope that you will point out.
Convert string type to int type in C + +