[Dev-C++] <Dec-C++>Please give me your feedbacks!
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Justin F. <jus...@gm...> - 2004-11-05 08:25:51
|
Hi,
I'm newer to C++ programming so I hope you can help me to improve my
coding skill. Here is my first code to implement Strtoi(char*), please
give me your feedback.
My lead ask me to implement it as Strtoi(wchar*), but I can't find
which head contains the definition of wchar and how to use it. And
some known bugs are there after tesing:
when I input a long string such as "12345678987654321", this app will crash.
Also when I add a sentence before sleep(), it crashed.
BTW, I want to know how to debug an app and how to track views I added.
Any ideas?
Thanks in advance and any suggestions will be appreciated!!
#include <stdlib.h>
int strtoi(char* a)
{
int sum=0,size=0,i=0;
while(a[i]!='\0') // calculate the size of the array
{i++;size++;}
printf("The size of string is: %d\n",size);
i=0;
for(i=0;i<size;i++)
{
if (a[i]<'0' || a[i]>'9') //Don't deal with invalid strings
{ printf( "I think it's meaningless to covert a English word to
int.\n Please input some string such as 123");
return 0;
}
else
{
int j=0; //variable 'j' refers to the exponet of 10
int temp=a[i]-'0'; //covert a[i] to a interger
for(j=size-i-1;j>0;j--) //sum
temp=temp*10;
sum+=temp;
}
}
printf("The result of convertion:%d\n",sum);
return sum;
}
int main()
{
int i=0;
char* a;
printf("Please input your string,such as 123\n");
scanf("%s",a);
strtoi(a);
sleep(100000);
return 0;
}
--
Thanks
Z.J.
|