views:

90

answers:

2

i got this really wierd problem, i'm writing my results to an output file, i use functions A B and C i activate them in that order, the results in the file is printed in a different order, first from A than from C and after that from B. i just can't understand why the results printed in a different order than the activation order. thanx.

the code :

void Manager::AddCommand(Command* com, ofstream &ResultFile)
{
    if (com != NULL)
    {
        if (com->ValidCommand(ResultFile) == true)
            commands.push_back(com);
        else
                delete com;
    }
}

bool Command::ValidCommand(ofstream &Result) const
{
    if(func<PrintCityCouncilList || func >HireEmployee){
        Result << "Command:Failed activating function - invalid function number\n";
        return false;}
    if ((func == Command::PrintDepartmentEmployees) || (func == Command::PrintDepartmentExpenses) || (func == Command::PrintDepartmentStatistics)){
        if(dept<Employee::Engineering ||dept>Employee::Sanitation )
        {
            Result << "Command:Failed activating function - invalid department number\n";
            return false;
        }
    }
    return true;
}

void Manager::ActivateCommand(Command* com, ofstream &Result)
{
    if(com != NULL)
    {
        switch(com->GetFunction())
        {
            case (Command::PrintCityCouncilList): pcc->PrintCityCouncilDetails(Result);
                break;
            case (Command::PrintDepartmentEmployees):ActivatePrintDeprtEmployees(com->GetDepartment(), Result);
                break;
            case (Command::PrintEmployeeSalary):ActivateEmployeeSalary(com->GetId(), Result);
                break;
            case (Command::PrintDepartmentExpenses):ActivateDeprtExpenses(com->GetDepartment(), Result);
                break;
            case (Command::PrintCityCouncilExpenses): pcc->AllExpenses (Result);
                break;
            case (Command::PrintDepartmentStatistics):ActivateDeprtStatistics(com->GetDepartment(), Result);
                break;
            case (Command::FireEmployee): pcc->RemoveEmployeeFromCC(NULL,com->GetId(),Result);
                break;
            case (Command::HireEmployee): pcc->AddEmployeeToCC(com->GetId(),com->GetPrivateName(),com->GetSurName(),com->GetDate(),com->GetAddress(),com->GetDepartment(), com->GetStatus(),com->GetSalary(),com->GetPositionPercent(),com->GetPhoneNum(), Result);
                break;
            default:Result<<"Manager:Failed Activating command - invalid function"<<endl;
                break;
        }

    }
}

void Manager::ActivateCommandsList(ofstream &Result)
{
    Command* tmp = NULL;
    if (commands.empty() == false)
    {
        list<Command*>::iterator iter = commands.begin();
        while (iter != commands.end())
        {
            tmp = (Command*)(*iter);
            ActivateCommand(tmp,Result);
            iter++;
        }
    }
}
A: 

My guess is that if this is homework, you are not having a threading issue. Have you tried to flush the output stream from A, B, and C? That might solve your problem.

Dave
That might cause a delay, but it shouldn't cause the output to be reordered.
Matthew Flaschen
+1  A: 

Ok here's the deal, your code maybe having some difficulties if you are running it in visual stdio. You see, it has some bugs when it tries to optimize your code. Turn optimization off. Also flush your stream, that's done when putting endl in cout and \n in printf. There is another possibility and it is Stack-Corruption or Heap-Corruption. Check to see if your dynamic objects are referenced within their boundaries. As long as you don't send the code, like my friend just said we should consult the crystall ball.

Green Code