0

I meet a 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 what is in the array and exit the loop. Initially, I declare 9999 for array index 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 <windows.h>
#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;
    } 
    system("pause");
    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 ..........

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;
    }

system("pause");
OMi Shah
  • 4,083
  • 2
  • 20
  • 27
  • 2
    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
  • 445
  • 4
  • 9
0

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
  • 82
  • 1