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 ..........