| | |
HELP!! how do you use atoi?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2008
Posts: 91
Reputation:
Solved Threads: 0
I'm trying to figure out how to use the atoi function I have wrote and understand this so far:
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int x;
string str="34";
x=atoi(str);
cout<<x;
system("pause");
return 0;
}
And Then I get error reports about a freakin const char?! HELP!
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
int main()
{
int x;
string str="34";
x=atoi(str);
cout<<x;
system("pause");
return 0;
}
And Then I get error reports about a freakin const char?! HELP!
Always post the exact errors you get for the best help.
atio() takes input of type
do this:
Or, better yet, use stringstreams:
atio() takes input of type
const char* .do this:
x=atoi(str.c_str());Or, better yet, use stringstreams:
C++ Syntax (Toggle Plain Text)
#include <sstream> using namespace std; int main() { stringstream ss; int x; string str = "34"; ss << str; ss >> x; return 0; }
Last edited by CoolGamer48; Aug 4th, 2008 at 2:58 pm.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
the string::c_str() method converts the contents of an std::string to const char*. For example:
further information here
You'd be better off using string streams though. there isn't a need to convert an std::string into an old C-string (const char*). string streams can handle std::strings and C-strings.
C++ Syntax (Toggle Plain Text)
string str1 = "Hello"; const char* str2 = "Hello"; str1.c_str();//would return a value equivalent to str2
further information here
You'd be better off using string streams though. there isn't a need to convert an std::string into an old C-string (const char*). string streams can handle std::strings and C-strings.
I'm a student. If my statements seem too absolute, feel free to coat them with "In my opinion..." or "I believe...".
![]() |
Similar Threads
- atoi function (C)
- atoi(string.substr(x,x)) Possible? (C++)
- Help with atoi (C++)
- help me about atoi (C++)
- Please help with atoi() (C)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Visual C++ New form
- Next Thread: program to generate random numbers for a given range
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





