+ 2

How do I pass a variable into an array and return the variable in my function?

In Python, one can easily add variables to a list and return variable in their functions. I have an assignment to write a code that validates Credit card. In C/C++, this is extremely difficult and confusing due to data type of variables and return type of functions. I'm trying to write a function that will input the digits in a "long long" variable into an array "integer" variable and then return the array. https://code.sololearn.com/cDEnSx6u0cc3/?ref=app

4th Apr 2017, 6:28 PM
Chuma Umenze
3 Respuestas
+ 18
I've edited your code and it works. But it has become so complicated and I think you should consider the card number as a string or character array. It'd make the problem simpler. https://code.sololearn.com/cx0pzTHo2BkK/?ref=app
4th Apr 2017, 8:01 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 18
To pass the reference/address of array, so that it can be changed inside that function. Returning array from a function seems critical to me. That's why I used void function. But void function can't modify any variable if we don't pass the reference / memory address of it. Use char array instead, as follows. Using long long will cause more problems later. char card[ ] = "123456789555555465"; for(int i=0; i<strlen(card); i++){ cout<<card[i]<<endl; }
4th Apr 2017, 9:21 PM
Shamima Yasmin
Shamima Yasmin - avatar
+ 2
@Shamima What's the reason for using pointers in the ccDigits function?
4th Apr 2017, 9:07 PM
Chuma Umenze