What could be wrong with this code?:
When I output the supposedly converted array, it's outputs some crazy numbers.Code:int arr[10]; char arr2[10]; for (int i=0; i<10; i++) { arr[i]=(int)arr2[i]; }
???
What could be wrong with this code?:
When I output the supposedly converted array, it's outputs some crazy numbers.Code:int arr[10]; char arr2[10]; for (int i=0; i<10; i++) { arr[i]=(int)arr2[i]; }
???
For one thing, you have not initialised arr2.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
any random characters including spaces.
Okay, so what is so crazy about the numbers? Don't you get the (presumably) ASCII values as you intended?
You might want to post the smallest and simplest compilable program that demonstrates the problem. Post your sample input, expected output, and actual output.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Try this , you will get to know the problem.
Code:#include<stdio.h> #include<string.h> main() { int arr[10]; char arr2[10]="abcdefghi"; // Initialize the array int i; for (i=0; i<10; i++) { arr[i]=(int)arr2[i]; // Converting to int .(ASCII value) } for (i=0; i<10; i++) { printf("===> %d\n",arr[i]); } }
Sorry, but your program does not explain the problem. On the other hand, you are not TheUmerOriginally Posted by karthigayan
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
arr2 is only initialised with 9 values in the last example by the way
No, it is correctly initialised with 10 values, including the null character.Originally Posted by rogster001
Personally, I thought that this might be a "convert digit to integer" problem, but the "any random characters including spaces" comment makes me think otherwise.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
@karthigayan:
I ran your code and it gives the 'segmentation fault' error.
@laserlight:
Input string is: [p 1 (a tab) 0]
And it outputs: 80 49 9 48 0 0 0 0 0 0
I want to get 1 and 0 as integers.
?
Ah. In that case, you do want to convert a digit to an integer. Since you want to leave those characters that are not digits untouched, it implies that you need some logic in the loop, e.g., check if arr2[i] >= '0' && arr2[i] <= '9'. If it is, then you subtract '0' from arr2[i] to get the integer value. Otherwise, you just assign the value as per normal.Originally Posted by TheUmer
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Alright. The problem is solved to some extend. But a little more problem:
For the given string [P 1 (tab) 0] it outputs fine: 1 0
But when I have a string [P 2 (tab) 34] it outputs: 2 3 (no four.)
What could be the problem?
I wonder why you never reply with complete answer? How can I solve this problem?
What is your current code?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
for a single 34, it takes 3 but not 4.Code:int arr[10]; char arr2[10]; for (int i=0; i<10; i++) { if (arr2[i]>=0 && arr2[i]<=9) { arr[i]=(int)arr2[i]-48; } }