请帮助我的函数中的错误 - 将int转换为字符串 [英] Please help with bug in my function - converts int to string
问题描述
嗨伙计们 - 所以基本上我正在尝试实现一个函数,
将int转换为字符串,但由于某种原因它不起作用 -
任何想法?我的函数intToStr如下所示。我只是想用
来实现这个以获得c风格的字符串练习。
#include< iostream>
#include" testString.h"
int main(int argc,char * argv []){
char * c2 = new char [];
testString :: intToStr(c2,-254);
cout<< c2<< endl;
删除c2;
c2 = NULL;
返回0; * /
}
void testString :: intToStr(char str [],int number){
int x = number;
if(x< 0)
x = -x;
int order = 0;
while(x> 0){
x = x / 10;
命令++;
}
char * tmp =新字符[order + 2];
tmp [0] =''\''';
int y = number;
for(int i = 1; i< = order; i ++){
tmp [i] =(char)(y%10);
y = y / 10;
}
if(number< 0)
tmp [order + 1] ='' - '';
else
tmp [order + 1] =''+'';
testString :: reverseString(tmp); / * reverseString有效 - 该代码中没有
错误* /
while(* str ++ = * tmp ++);
delete tmp;
tmp = NULL;
}
谢谢
Ivar写道:testString :: reverseString(tmp); / * reverseString有效 - 该代码中没有
错误* /
但是您的使用中存在错误。因为tmp的第一个字符是
null字符,我怀疑该字符串是否会被反转(提供
reverseString不会做任何异常。)
>
Ivar写道:大家好 - 所以基本上我试图实现一个将int转换为字符串的函数,但它是因某种原因无法工作 -
任何想法?我的函数intToStr如下所示。我只是想尝试实现这个以获得c风格的字符串练习。
很多东西都不能在这里工作。
#include< iostream>
#include" testString.h"
int main(int argc,char * argv []){
char * c2 = new char [];
非法,你必须在这里指定数组大小。
testString :: intToStr(c2,-254);
cout<< c2<< endl;
删除c2;
非法。这应该是
删除[] c2;
c2 = NULL;
返回0; * /
删除* /
}
void testString :: intToStr(char str [],int number){
int x = number;
if(x <0)
x = -x;
使用std :: abs()。
int order = 0;
while(x> 0){
x = x / 10;
命令++;
}
char * tmp = new char [order + 2];
tmp [0] =''\ 0'';
int y = number;
小心! y应该是绝对的,否则你会得到负值!
for(int i = 1; i< = order; i ++){
tmp [i] =(char) (Y%10);
这应该是
tmp [i] =''0''+(y%10);
如果你想要字符。这只适用于ASCII机器。
y = y / 10;
}
if(number< 0)
tmp [order + 1] ='' - '';
其他
tmp [order + 1] =''+'';
testString :: reverseString(tmp); / * reverseString有效 - 该代码中没有错误* /
while(* str ++ = * tmp ++);
Nooo!你刚刚丢失了指向你分配的内存的指针。保存
*之前*
char * to_delete = tmp;
删除tmp;
这应该会导致应用程序崩溃,因为你没有从
正确的地址中删除(tmp已经在你的循环中移动了)。
tmp = NULL;
}
我认为还有一些其他错误,但首先要解决这些错误。
顺便说一下,我知道你这样做是为了好玩,但你应该使用
std :: istringstream:
#include< sstream>
int main()
{
int i = 0;
std :: istringstream iss(" - 254");
iss>> i;
}
谢谢
Jonathan
< blockquote> Jonathan Mcdougall写道:
Ivar写道:char * tmp = new char [order + 2];
删除tmp;
这会导致应用程序崩溃,因为你没有从
正确的地址中删除(tmp已经在你的循环中移动了)。
更重要的是,你应该做什么
删除[] tmp;
as在main()中。
int * i = new int;
删除i;
int * i = new int [10];
删除[] i;
Jonathan
Hi guys - So basically I am trying to implement a function that
converts an int to a string, but it is not working for some reason -
any thoughts? My function, intToStr, is shown below. I''m just trying to
implement this to gain practice with c-style strings.
#include<iostream>
#include"testString.h"
int main(int argc, char* argv[]) {
char* c2 = new char[];
testString::intToStr(c2, -254);
cout << c2 << endl;
delete c2;
c2 = NULL;
return 0;*/
}
void testString::intToStr(char str[], int number) {
int x = number;
if(x < 0)
x = -x;
int order = 0;
while(x > 0) {
x = x/10;
order++;
}
char* tmp = new char[order+2];
tmp[0] = ''\0'';
int y = number;
for(int i=1; i <= order; i++) {
tmp[i] = (char)(y%10);
y = y/10;
}
if(number < 0)
tmp[order+1] = ''-'';
else
tmp[order+1] = ''+'';
testString::reverseString(tmp); /*reverseString works - there is no
bug in that code*/
while(*str++ = *tmp++);
delete tmp;
tmp = NULL;
}
Thanks
Ivar wrote:testString::reverseString(tmp); /*reverseString works - there is no
bug in that code*/
but there is a bug in your usage. since the first character of tmp is
null character, I suspect whether the string will be reversed (provided
reverseString doesnot do anything unusual.)
Ivar wrote:Hi guys - So basically I am trying to implement a function that
converts an int to a string, but it is not working for some reason -
any thoughts? My function, intToStr, is shown below. I''m just trying to
implement this to gain practice with c-style strings.
Many things don''t work here.
#include<iostream>
#include"testString.h"
int main(int argc, char* argv[]) {
char* c2 = new char[];
Illegal, you must specify an array size here.
testString::intToStr(c2, -254);
cout << c2 << endl;
delete c2;
Illegal. This should be
delete[] c2;
c2 = NULL;
return 0;*/
Remove that */
}
void testString::intToStr(char str[], int number) {
int x = number;
if(x < 0)
x = -x;
Use std::abs().
int order = 0;
while(x > 0) {
x = x/10;
order++;
}
char* tmp = new char[order+2];
tmp[0] = ''\0'';
int y = number;
Watch out! y should be absolute here or you''ll get negative values!
for(int i=1; i <= order; i++) {
tmp[i] = (char)(y%10);
This should be
tmp[i] = ''0'' + (y%10);
if you want characters. This will only work on ASCII machines.
y = y/10;
}
if(number < 0)
tmp[order+1] = ''-'';
else
tmp[order+1] = ''+'';
testString::reverseString(tmp); /*reverseString works - there is no
bug in that code*/
while(*str++ = *tmp++);
Nooo! You just lost the pointer to the memory you allocated. Save it
*before*
char *to_delete = tmp;
delete tmp;
This should crash the application because you are not deleting from the
correct address (tmp has moved in your loop).
tmp = NULL;
}
I think there was some other errors as well, but start by fixing these.
By the way, I understand you are doing that for fun, but you should use
std::istringstream instead:
# include <sstream>
int main()
{
int i = 0;
std::istringstream iss("-254");
iss >> i;
}
Thanks
Jonathan
Jonathan Mcdougall wrote:Ivar wrote:char* tmp = new char[order+2];
delete tmp;
This should crash the application because you are not deleting from the
correct address (tmp has moved in your loop).
What''s more, you should do
delete[] tmp;
as in main().
int *i = new int;
delete i;
int *i = new int[10];
delete[] i;
Jonathan
这篇关于请帮助我的函数中的错误 - 将int转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!