For a project in my Intro to c++ class we are using char arrays of size 140 initialized with the following function:
void InputText(char A[140], string prompt)
{
cout << prompt;
cin.ignore();
cin.getline(A,140);
}
I made this function to count the number of filled elements in an array:
int numElements(char A[140]) //searches for the first '\0' in an array and returns the index, if none are found, returns 140
{
int numEl = 140;
for (int i = 0; i <= 140; i++) { if (A[i] == '\0') { numEl = i; break; } }
return numEl;
}
It works fine when I use it on arrays filled with InputText(), but when I tested it with another array like this:
char A[] = {'1', '2', '3', '4', '5'};
cout << numElements(A);
It will print out 13 instead of 5. I don't understand why this is happening.