-1

The program's purpose is to fill two arrays from data in a file, the first column is department number, and the second is boxes sold. There should be a max of 15 departments, variables departmentNumber and boxesSold should receive data from file before filling array

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    int departmentNumber = 0, boxesSold = 0;

    ifstream inputFile("boxes.txt");

    const int SIZE = 15;
    int departmentArray[SIZE];
    int boxesArray[SIZE];

    int count = 0;

    while (count < SIZE && inputFile);
    {
        inputFile >> departmentNumber;
        inputFile >> boxesSold;

        for (int index = 0; index < count; index++)
        {
            if (departmentArray[index]== departmentNumber)
            {
                boxesArray[index] = boxesArray[index] + boxesSold;
            }
            else
            {
                departmentArray[index] = departmentNumber;
                boxesArray[index] = boxesSold;
                count++;
            }

        }
        inputFile.close();
    }

    //display numbers
    for (int i = 0; i < count; i++)  
    { 
        cout << departmentArray[i] << " "; 
        cout << boxesArray[i];
        cout << endl;   
    }
    system("pause");
}

current output is blank. my for loop searches array to see if departmentNumber already exist, my while loop continues to accept data from file, and the last for loop displays number. Been stuck on this for too long.

 int count = 0;

        while (count < SIZE && inputFile)
        {
            inputFile >> departmentNumber;
            inputFile >> boxesSold;

            for (int index = 0; index < count; index++)
            {
                if (departmentArray[index] == departmentNumber)
                {
                    boxesArray[index] = boxesArray[index] + boxesSold;

                }
                else
                {
                    departmentArray[count] = departmentNumber;
                    boxesArray[count] = boxesSold;

                }
             }
            boxesArray[count] = boxesSold;
             count++;
        }
            inputFile.close();

i updated the code and the following output is given, my guess is the for loop, that or index/count is not declared properly,

-858993460 23
410 17
410 16
120 14
150 32
300 27
410 11
410 10
120 8
150 16
120 2
300 4
410 5
520 6
390 7
Press any key to continue . . .
mattvondu
  • 1
  • 3
  • For starters, you do while (count < SIZE && inputFile); -- should be no semicolon after inputFile.close(); in the loop. And I am so not sure what would while (count < SIZE && inputFile) check on talking about inputFile immediately in expression. – Alexander V Nov 24 '15 at 04:27
  • Yeah, `while (count < SIZE && inputFile);` is an infinite loop. – Weak to Enuma Elish Nov 24 '15 at 04:31
  • Related: http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – Weak to Enuma Elish Nov 24 '15 at 04:34

1 Answers1

0

You're not handling the case when you first start. What happens when count = 0? You never enter the for loop.

I suggest an easier way is to use a std::map<int, int>. And do something like this inside your read loop:

map[departmentNumber] += boxesSold;

This does the following:

  1. If the departmentNumber exists in the map, it increments the boxesSold.
  2. If the departmentNumber does not exists in the map, it adds it with the value equal to 0, and then increments it by boxesSold.
Anon Mail
  • 4,577
  • 1
  • 16
  • 19
  • std would be easier but specifications do not allow it, nested loops and arrays , basic early level stuff – mattvondu Nov 24 '15 at 05:19