I have a char array s[n]={1-3 2 5 6}
and I want to convert it in Int array N[n] in c++ such that N[1] holds the value 1 and N[2] hold the value -3 and so on.
I have tried the function atoi
but it passes all the value of array into single Int. Please help me.
Asked
Active
Viewed 133 times
-6

SurvivalMachine
- 7,946
- 15
- 57
- 87

sunidhi chaudhary
- 21
- 1
- 2
- 10
-
Don't use `atoi`, it's broken. Use `std::stoi`. – Christian Hackl Jan 06 '18 at 08:43
-
What should be the value of N[0]? – Killzone Kid Jan 06 '18 at 08:44
-
1[Why shouldn't I use atoi()?](https://stackoverflow.com/q/17710018/995714) – phuclv Jan 06 '18 at 08:48
-
How can -3 be a member of the char array? -3 is 2 characters! – QuIcKmAtHs Jan 06 '18 at 08:49
-
@XcoderX why not? – Killzone Kid Jan 06 '18 at 08:54
-
1 element of a char array can only store 1 char. -3 is 2 chars long – QuIcKmAtHs Jan 06 '18 at 08:54
-
char arr[1000]; cin >> arr[0]; cout << arr[0]; If input is -3, output is - – QuIcKmAtHs Jan 06 '18 at 08:56
-
@XcoderX: `-` and `3` are two individual `char` objects. Note that string literals in C++ are char arrays, e.g. `"foo-3"` is of type `char const[6]` (including the invisible `'\0'` at the end). – Christian Hackl Jan 06 '18 at 08:56
-
Yes, that was my point. One char element cannot store 2 chars. Check out my above comment – QuIcKmAtHs Jan 06 '18 at 08:58
-
@XcoderX: btw, even if the individual char object is the number -3, it's still not a problem. Remember, a `char` is not much different from an `int`, it's just smaller; but just like `int`, it's a *number*, and that number can be negative if the `char` is signed. – Christian Hackl Jan 06 '18 at 08:58
-
@XcoderX signed char is -128...127 so -3 is a valid value. – Killzone Kid Jan 06 '18 at 09:02
-
@KillzoneKid: That is, if `char` is actually signed on that implementation. The standard also allows `char` to be unsigned. See also http://en.cppreference.com/w/cpp/types/numeric_limits/is_signed – Christian Hackl Jan 06 '18 at 09:07
-
@ChristianHackl If that so, OP's example would cause UB, but I suppose we assume that the data provided is valid. – Killzone Kid Jan 06 '18 at 09:12
-
@ChristianHackl Anyway it is not clear what OP is asking, so I withdraw my answer – Killzone Kid Jan 06 '18 at 09:16
-
Construct an `std::string` from your char array and then follow [this answer](https://stackoverflow.com/a/22366643/5376789). – xskxzr Jan 06 '18 at 13:53
1 Answers
0
I am using the simple approach of type casting from character to integer as, N[i] = (int)s[i] -'0';
Here N is integer array , s is the character array .I simply type cast the character array into integer , it will treat it as a ASCII code of s[i]. so I minus the '0' character (this show the number must be in original value not in ascii code).
You can try the below code! Also I am attaching the screenshot of the output of code.
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;
int main()
{
//character array 's' with length of 5
char s[5] = { '1', '3', '2', '5', '6' };
//before converting , prints the character array
cout << "\t\tCharacter Array" <<endl;
for (int i = 0; i < 5; i++)
{
cout << s[i] << "\t";
}
//integer array 'N' with length of 5
int N[5];
cout << "\n\tConverting Character Array to Integer Array" << endl;
//convert the character array into integer type and assigning the value into integer array 'N'
for (int i = 0; i < 5; i++)
{
N[i] = (int)s[i] - '0';
}
cout << "\t\tInteger Array" << endl;
//after converting , prints the integer array
for (int i = 0; i < 5; i++)
{
cout <<N[i]<<"\t";
}
_getch();
return 0;
}

Usman
- 1,983
- 15
- 28
-
But this method doesn't work in case of negative numbers for example if I have -3 4 -5 in my character array then it is showing the wrong answer. – sunidhi chaudhary Jan 07 '18 at 07:07