hello

I have some code that loops & takes in a number then should store it in an array.

My problem is that for some reason, the program only stores every 2nd number entered?

while (!cin.fail() && cin>>purchase) {   // while a correct input is input
           
           cin >> purchase;                 // enter recent purchase
           pur[nWeek][counter] = purchase;  // store recent purchase in relevent array element
           cout << "purchase put in array element " << counter << "\n";
           counter++;
     }

Whole function

void enterPurchase(float pur[][25], int nWeek, int counter) {
     
     float purchase = 0;
     
     while (!cin.fail() && cin>>purchase) {   // while a correct input is input
           
           cin >> purchase;                 // enter recent purchase
           pur[nWeek][counter] = purchase;  // store recent purchase in relevent array element
           cout << "purchase put in array element " << counter << "\n";
           counter++;
     }
     
     for (int i=0; i<counter; i++) {
         cout << "Purchase No." << (i+1) << ": $";
         cout << pur[nWeek][i] << "\n";
     }
     
     cout << pur[nWeek][2] << endl;
}

hello

I have some code that loops & takes in a number then should store it in an array.

My problem is that for some reason, the program only stores every 2nd number entered?

while (!cin.fail() && cin>>purchase) {   // while a correct input is input
           
           cin >> purchase;                 // enter recent purchase
           pur[nWeek][counter] = purchase;  // store recent purchase in relevent array element
           cout << "purchase put in array element " << counter << "\n";
           counter++;
     }

You have cin >> purchase in the loop twice, so it immediately overwrites the first entry:

while (!cin.fail() && cin>>purchase) {   // while a correct input is input
           
           cin >> purchase;                 // enter recent purchase
           pur[nWeek][counter] = purchase;  // store recent purchase in relevent array element
           cout << "purchase put in array element " << counter << "\n";
           counter++;
     }

Take the second one out.

This question has already been answered. Start a new discussion instead.