I am trying to extract data from one file and add each line to alternating lines. Example .txt file:
123 Main
500
555 Acorn
1500
23 Smithville
1540
I have an address array to hold the strings and amountDue array to hold integers. Ideally address[0] holds "123 Main" and amountDue[0] holds 500, etc.
The function I have made for this uses getline to get the string and works for the first loop but the inputFile >> amountDue[0] recieves more than the 500 integer. It takes 500 555, then the next getline takes "Acorn" and the next inputFile >> amountDue[1] takes 1500 23.
void loadArrays(string address[], double amountDue[], const int SIZE)
{
//Open file for data laoding
ifstream inputFile;
inputFile.open("Prog3Input.txt");
for (int i = 0; i < SIZE; i++)
{
getline(inputFile,address[i]);
inputFile >> amounDue[i];
}
}
I expected:
string address[0] is "123 Main"
int amountDue[0] is 500
address[1] is "555 Acorn"
amountDue[1] is 1500
ETC.
I am getting:
address[0] is "123 Main"
amountDue[0] is 500 555
address[1] is "Acorn"
amountDue[1] is 1500 23
ETC