C ++字符串到整数问题使用的atoi(str.c_str()) [英] C++ String to Integer Issue Using atoi(str.c_str())
问题描述
我试图重新present一个变量在一个字符串转换为整数,我这样做使用的形式;
I am trying to represent a variable in the form of a string to a integer, I have done so using;
atoi(str.c_str())
该字符串最初从一个文本文件中获取并存储成;
The string is originally obtained from a text file and stored into a;
CharArrayPtr cmemblock;
,然后再psented作为一个字符串$ P $;
Which is then represented as a string;
string str;
for(int i = 0; i < numberofvalues; i++)
{
str = cmemblock[i];
int number = atoi(str.c_str());
cout << number;
}
如果我是改变COUT打印STR;
If I was to change the 'cout' to print str;
str = cmemblock[i];
int number = atoi(str.c_str());
cout << str;
数显示正确存储在文本文件中。
The number show correctly as stored in the text file
不过,我需要的输出是一个整数,这样我就可以重新present它在一个循环来搜索存储在阵列中的价值。因此,这是数字进场,这就是为什么我要求你的帮助,当原因;
However, I require the output to be an integer so that I could represent it in a loop to search for a value stored in a array. So this is where 'number' comes into play, which is the reason why I am asking for your help, when;
cout << number;
每当一个新行读它重新psented为$ P $'0'我将如何去删除此?如果你需要我的全code是在几个不同的.cpp文件和prevent有人复制我的工作,我只能通过电子邮件发送你的,我确定你已经猜到这是一个大学的任务的一部分。
Whenever a new line is read it is represented as '0' how would I go about removing this? If your require my full code it is in several different .cpp files and to prevent anyone copying my work I can only email it you, im sure you have already guessed it is part of a University Assignment.
使用会员Adosi code,我想出了这一点;
Using Member Adosi code I came up with this;
std::string str;
for(int i = 0; i < numberofvalues; i++)
{
str = cmemblock[i];
std::stol(str);
int number = std::stoi(str);
cout << number;
}
我得到一个错误R6010。我这样做不对吗?
I get an error R6010. Have I done this wrong?
推荐答案
的std :: Stoi旅馆(STR)
使用它来代替的atoi
C ++ 11有这个和其他一些功能,如的std ::短距起降()
长材,的std :: STOF()
的花车等。
C++11 has this and a few other functions such as std::stol()
for longs, std::stof()
for floats, etc.
HTTP://en.cp$p$pference。 COM / W / CPP /串/ basic_string的/ STOL
这篇关于C ++字符串到整数问题使用的atoi(str.c_str())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!