-1

I have problem where I don't know the size of the array. When I need to prompt the information in array, I do not know how to limit the size of loop so that it only prompts for what is in the array and exits the loop. Initially, I declare 9999 for array size because I do not know how much information will user enter. Vector and Pointer of array are not allowed in this assignment, is there other way to solve it?

Here is my code

#include <iostream>
#include <fstream>

using namespace std;

void ReadData(int[] , int);

int main()
{
    int product_code[9999];
    int code, num;
    ofstream outdata;
    
    ReadData(product_code , 9999);

    outdata.open("productlist.txt");
    cout << "How many product code?";
    cin >> num;
    for(int i = 0; i < num; i++)
    {
        cout << "Product Code : ";
        cin >> code;
    }
    outdata.close();

    for (int i = 0; i < 9999; i++)
    {
        cout << product_code[i] << endl;
    } 
    return 0;       
}  

void ReadData(int p_code[] , int j)
{
    ifstream indata;
    indata.open("productlist.txt");
    while (indata >> p_code[j])
    {
        j++;
    }
    indata.close();
}

If using my code and the data input by user is 3, 1111, 2222, 3333

The output will be

1111
2222
3333
0
0
0
0
0
0
0
0
0
0
.......
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
  • 1
    Try to learn about dynamic memory allocation, or when you are using c++ about std::vectors – RoQuOTriX Nov 14 '19 at 09:12
  • 2
    Use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) – Wander3r Nov 14 '19 at 09:12
  • You can refer this link also for more detail understanding[variable length array](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard) – Bharat Arya Nov 14 '19 at 10:01
  • Isn't the obvious answer "use num instead of 9999"? – user253751 Nov 14 '19 at 10:09
  • Your call to `ReadData` with the argument for `j` of 9999 makes no sense and you are writing to garbage memory, you should call it with `0` OR start from 0 and use that as a condition in the loop so you don't read too many values. What happens if they want to give 10,000 values? what happens if they specify they want to give -1 values? And you read in the variable `code` from user but don't do anything with it?? – UpAndAdam Aug 22 '23 at 18:19
  • no that won't be the output of your program. go try it. you dont store the input the user enters into the file, you never write anything to the file you open in the main loop. if anything the fact that you open it for writing like that should erase it since you didnt mark it for appending. but perhaps since you dont write any data to it its left alone. – UpAndAdam Aug 22 '23 at 18:22

3 Answers3

2

Why you're running 9999 times the loop? When you are asking the user how many products codes to enter? Just run till < num

for (int i=0; i < num; i++)
{
    cout << product_code[i] << endl;
}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
OMi Shah
  • 5,768
  • 3
  • 25
  • 34
  • 3
    May be, it would be worth to add that program should check `num` against array size. (From my experience, users are bad by definition.) And, a hint about [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants) couldn't hurt as well. ;-) – Scheff's Cat Nov 14 '19 at 09:17
2

If you don't know exactly data size, which can be read from file or other input, use std::vector. It is a dynamically extended data structure which has easy to use interface and it allocated on heap. Don't use static array for this purpose. You allocated memory on stack for 9999 integers, and a lot of array items may stay unused. More over you should hold count of read items apart in this case.

It's really easy to use.

std::vector<int> product_code;
ReadData (product_code);
...

void ReadData(std::vector<int>& p_code)
{
    ifstream indata;
    indata.open("productlist.txt");
    int value{0}
    while (indata >> value)
    {
        p_code.push_back(value);
    }
    indata.close();
}

After you fill in product_code you can get it size:

product_code.size();

And have access to any item by index:

for(size_t idx = 0; idx < product_code.size(); ++idx)
{
    std::cout << product_code[idx] << std::endl;
}

Or through range-based for:

for(int value : product_code)
{
    std::cout << value << std::endl;
}
Malov Vladimir
  • 490
  • 4
  • 11
1

First, your code is seriously flawed as "ReadData (product_code , 9999);" will overflow product_code array.

What you need is to use dynamic allocation because your program doesn't know the number of "product codes" until it loads all of them from the file. Even better, use std::vector as this standard class already implements all that you would have to reinvent otherwise.

demeter
  • 92
  • 1