taking the integers | SoloLearn: Learn to code for FREE!

0

taking the integers

suppose input 1+2+3. Now I've to get the 1,2,3 to be included into an int array.how do i do it?

6/15/2018 2:35:10 PM

Tanvir Hasan

2 Answers

New Answer

+3

I do not know how it works with C language. But in C++ it is like this: int* A = new int(3); A[0] = 1; A[1] = 2; A[2] = 3; Or you can create a function: void CreateShowArray(int n){ int* A = new int(n); for(int i = 0; i < n; i++){ cout << "Enter integer: "; cin >> A[i]; }; cout << "Your array is:" << endl; cout << "[ "; for(int i = 0; i < n; i++){ cout << A[i] << ", "; }; cout << "]" << endl; }; Now, if input is with plus symbol. You can create a string and check each element of it, like this: int A[5]; cin.getline(A,5); int* B = new int(3); for(int i = 0; i < n; i++){ if(A[i] != char(43)){ B[i] = A[i]; }; }; And finally, you will get the array with the integers. Now, you can set a high range of elements into the string, you have to check until the last element avoiding the blanks. And, the "for loop" need the amount of elements, so, you have to create another function to count them.

+1

Now, all these steps are for understanding to code. Obviously, there is a simpler way. Have a nice day ^^