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.
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:
And here's the .txt file it saves into:
So, ultimately, the values don't match and I'm stuck with this broken program.