So according to Eclipse, the getline function stores the entire text line from stream along with the newline and "terminating null character." I don't entirely know what that means.
The output I'm trying to get is printing two parallel arrays using a function. This function is a void function holding 3 parameters, the first array, the second array, and the constant integer array size value.
I'm able to store input into the first array with no problem.
const int SIZE = 5;
double firstArray[SIZE];
// range-based `for` loop to get user input
for (double& floatNum : firstArray)
{
cout << "Enter a floating point number: ";
cin >> floatNum;
}
However, things get weird with the second array, which is a string type.
string secondArray[SIZE];
// `for` loop to get user input
for (int i = 0; i < SIZE; i++)
{
cout << "Enter a name: ";
cin.ignore();
getline(cin, secondArray[i]);
}
My desired output is to print out both arrays using a function call in main(), printParallel(array1, array2, sizeofarray), simultaneously using a while loop inside a function, which I've attempted here:
void printParallel(double arrayOne[], string arrayTwo[], const int NUM)
{
int i = 0;
while (i < NUM)
{
cout << arrayOne[i] << " " << arrayTwo[i] << endl;
i++;
}
}
But the output is slightly off:
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a floating point number: 1.23
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
Enter a name: AA AA
1.23 AA AA
1.23 A AA
1.23 A AA
1.23 A AA
1.23 A AA
Can someone explain what's going on here? The first "A" seems to be overwritten by a null character. Also, is there a better way I should be asking questions in order to solve problems better in the future?