-4

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

Melebius
  • 6,183
  • 4
  • 39
  • 52
Soumitra Shewale
  • 165
  • 1
  • 1
  • 13
  • `int a[num-1][2];` is not even valid `c++`. even if it is. it's wrong. – apple apple Nov 28 '17 at 08:21
  • Picking on your English: You don't have to capitalize every word. It looks like you need `a[3][4]` because you have 3 rows and 4 columns. If your compiler supports variable arrays, you can declare `a[num][4]`. Array index starts at zero and goes up to `num`, example `a[0][0]` -> `array[num-1][0]`. If you are a beginner maybe you should start with 1 dimensional array. – Barmak Shemirani Nov 28 '17 at 08:29
  • 1
    Agree w/ @BarmakShemirani. the array `int foo[5];` has 5 valid indices: `0`, `1`, `2`, `3`, and `4`. Evaluating `foo[5]` reads one `int` past the end of the array, which will be whatever data happens to be in memory. Worse, **assigning** to `foo[5] = 12` overwrites that memory, which might be part of another variable. – lockcmpxchg8b Nov 28 '17 at 08:33
  • heh. you fix your code. I think your array declaration should be `int a[num][3];` but I've only spent a little bit looking. – lockcmpxchg8b Nov 28 '17 at 08:35
  • But That Creates **4** Columns, Not 3 As Needed. – Soumitra Shewale Nov 28 '17 at 08:40
  • 1
    Sigh... please don't post pictures of text, just post the text. – Jabberwocky Nov 28 '17 at 08:44
  • Please do not post screenshots of text. Copy the text, paste it directly into your question (and format it as code). – Melebius Nov 28 '17 at 08:44
  • So, At The End, I Am Not Able To Do The Following To Fix My Code: I Write One Line Of The Array, Write It To the file, Clear The Array, Write The Next Line Into The Array, Print The Next Line Into The File, Clear the array And So On – Soumitra Shewale Nov 28 '17 at 08:46
  • @SoumitraShewale: re: "But That Creates 4 Columns, Not 3 As Needed", that is just not true. `int arr[2][3];` has 2 rows and 3 columns. The valid row-indices are `0` and `1`; the valid column indices are `0`, `1`, and `2`. *As I said* using`arr[2][3]` is wrong, and results in a read outside the bounds of `arr`. – lockcmpxchg8b Nov 28 '17 at 08:50
  • Thanks To @lockcmpxchg8b, I Fixed The Code, Now It Works Fine. – Soumitra Shewale Nov 28 '17 at 08:54

1 Answers1

1

So, Instead Of Declaring With

int a[num-1][2];

I Should have used

int a[num][3]

Because I need 3 columns and num number of rows. With the earlier code, i tried reading values outside the array. Now, The Code doesn't read outside the array.

Soumitra Shewale
  • 165
  • 1
  • 1
  • 13