i am very new to c++ and have to create a program that will manage the sales of 40 different stores. I was told to use 2 (parralel) arrays(not a two dimensional array). I have no idea how to add the proper data i need becuase the file i am using to get the data only has sales for one day and has more than one data for each individual store. i somehow have to add all the sales of each indidual store into an array for the store number and that stores sales.

the file i have to get my data looks like this(notice it is random and the store numbers repeat themselves)

//store number--store sales
1 25
2 15
3 42
2 34
1 12
...

here is what i have doen so far i know i will have to use some type of searching function to find repeating store data and then add the sum but the data i am getting is not right.

int LinearSearch(int NumerToFind, int Size, int a[]);

int main ()
{

int index;
int arraysize;
int StoreID;
int ProductsSold;
int ID[40];
int Sales[40];

arraysize=40;

ifstream indata;
indata.open("boxes.dat");


indata>>StoreID;
indata>>ProductsSold;

for(int size=0; size<40; size++)
{
StoreID=Sales[size];
index = LinearSearch(StoreID, arraysize, ID);
if (index<0)
{
ID[size]=StoreID;
Sales[size]=ProductsSold;
size++;
}
else
Sales[size] += ProductsSold;
}
for(int c=0; c<40 ; c++)
cout<<ID[c]<<"------"<<Sales[c]<<endl;

return 0;
}

int LinearSearch(int toFind, int aSize, int a[])
{
for (int i=0; i<aSize; i++)
	{
	if (a[i]==toFind)
	return i;
	}
return -1;
}

Recommended Answers

All 5 Replies

You're off to a right start on the reading,but you need to use a different method of stopping the loop, and getting next data. Your code:

indata>>StoreID;
   indata>>ProductsSold;

   for(int size=0; size<40; size++)
   {
      StoreID=Sales[size];
      index = LinearSearch(StoreID, arraysize, ID);
      if (index<0)
      {
         ID[size]=StoreID;
         Sales[size]=ProductsSold;
         size++;
      }
      else
         Sales[size] += ProductsSold;
   }

is set to run for 40 items from the file, no matter how few or many there are. You need to stop when the data runs out. In the loop, why do you set StoreID to the contents of Sales? That makes no sense. The if/else code looks mostly ok, except you don't want to add to Sales, you want Sales[index]. Now, where do you get the next data items to process?

Here's an improvement:

int size = 0;
   while( size < 40 && indata >> StoreID )
   {   
      indata >> ProductsSold;

      index = LinearSearch(StoreID, arraysize, ID);
      if ( index < 0 )
      {
         ID[size] = StoreID;
         Sales[size] = ProductsSold;
         size++;
      }
      else
         Sales[index] += ProductsSold;
   }

   for(int c = 0; c < size ; c++)
      cout << ID[c]<<"------" << Sales[c] << endl;

Please put some space between operators and their operands - it helps readability.

Val

i understand what was wrong with my previous code but i dont quite understand exactly what your recomended code is doing? the data i am inputing is is not lined up with the correct data file and i dont know where in this code i am inputting the data from the file

  int size = 0;
   while( size < 40 && indata >> StoreID )//<-----what exactly does this condition mean?
   {   
      indata >> ProductsSold;

      index = LinearSearch(StoreID, arraysize, ID);
      if ( index < 0 )
      {
         ID[size] = StoreID;//i dont think i am getting the right data for StoreID
         Sales[size] = ProductsSold;
         size++;
      }
      else
         Sales[index] += ProductsSold;
   }

   for(int c = 0; c < size ; c++)
      cout << ID[c]<<"------" << Sales[c] << endl;

Val

while( size < 40 && indata >> StoreID )

the condition first checks that you have space left in your array to add an item ( size < 40 ).
If this is true, the input action occurs, reading a value into StoreID.
The read action will have a value upon completion. If the value is 0, the read failed, and there's no more data, so skip the loop.

Hmmm, for this particular problem, that may not be a fully correct solution, as you might read in other data that matches previous stores, without adding more to the array(s).

If you are guaranteed that the data file will contain no more than 40 unique store IDs, you can simply use:

while( indata >> StoreID )
ID[size] = StoreID;//i dont think i am getting the right data for StoreID
         Sales[size] = ProductsSold;
         size++;

These are correct. Variable size indicates not only how many stores have been added to the arrays, but also the position of the next available array position.

Val

ok i think i have everything figured out i just have not been able to figure out how to move on to a new line in the file

for(int i=0;  i < 40 ; i++)
{
    indata >> StoreID >>ProductsSold ; 
    //i am not getting new data everytime i do this loop 
    //instead i keep getting the same data
    ...
    ...
    ...
}

Please post a complete copy of the code as you have it now. The input statements, inside the loop, should be working correctly.

Val

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.