C++ How Do I Write A 2Dimensional Array To A Text File? : I Am Trying To Write A Program That Receives Input From The User And Makes A 2 Dimensional Array With The Provided Input. It Saves The Values Into The Array Properly But The Program Isn't Able To Perform The Saving Properly.

Please Pardon My Cardinal Sins, Mistakes In The Code, And Disgusting Looking And Bad Looking Code. I'm A Beginner And Only 14 Years Old.

Anyways, It Saves Wrong Values. Here's The Code:

    #include <iostream>
    #include <fstream>
    #include <unistd.h>
    using namespace std;
    
    int main()
    {
    
     int num;
     int productId =1;
    
    
         cout << "Welcome To The Store Manager Registry! \n ";
    
         cout << "How Many Products Would You Like To Add To The Registry?\n";
    
         cin >> num;
    
     if (num <= 0)
            cout << "Please Enter A Valid Input More Than 0";
    
     int a[num-1][2]; //creates a two dimensional array for items
     for (;productId-1<num;productId++)
        {
             cout << "\nPlease Enter The Cost Price For Product Id "<< productId << " (ONLY NUMBERS) \n";
             cin >> a[productId-1][0];
             cout << "\nPlease Enter The Selling Price For Product Id "<< productId << " (ONLY NUMBERS) \n";
             cin >> a[productId-1][1];
             a[productId-1][2]=a[productId-1][1]-a[productId-1][0];
        } //Receives Input And Saves Values To Array
        cout << "Saving Data...";
        ofstream outputfile;
        outputfile.open("Statistics.txt");
    
    
    
     for (int b = 1;b<=num;b++){
            outputfile <<a[b-1][0]<<","<<a[b-1][1]<<","<<a[b-1][2]<<endl;//saves values to file
     }
            outputfile.close();
    
    
        /* Saves Array In This Format:
        Product Id       Cost Price          Selling price          Profit
        1                    10                   20                  10
        2                    20                   20                  0
        3                    30                   10                  -20
    
        But, Prints In This Format
    
            Product Id       Cost Price          Selling price          Profit
        1                    10                   20                     20
        2                    20                   20                     30
        3                    30                   10                    -20
    
        */
    
    
    }


Here's The Input:[![enter image description here][1]][1]
And Here's The .txt File It saves Into:[![enter image description here][2]][2]
So, Ultimately, The Values Don't Match And I'm Stuck With This Broken Program.
Please Help. 

  [1]: https://i.stack.imgur.com/df8KF.png
  [2]: https://i.stack.imgur.com/Hdo0V.png