c ++函数不能调用两次

时间:2012-05-13 08:46:28

标签: c++ function

我有这个代码。从main函数我两次调用sportPrisevinners函数,如果它是第一次调用这个函数它工作正常我恢复正确的结果,但是当我第二次调用它时,我回复不正确的结果,即使我传递这个函数具有相同的参数。请帮助我解决这个问题,虽然它不会让我感到烦恼。

const char* countries[] = {"ru", "gb", "us", "uk", "ch", "de"};
const int countriesCount = 6;
const char* sports[] = {"runing", "swiming", "baseball", "football", "jumping", "kerling"};
const int sportsCount = 6;

enum {
    Empty = 0,
    Bronse,
    Silver,
    Gold
};

struct member {
    char sport[9];
    char country[3];
    int points;
    int medal;
};

struct members {
    member* list;
    int count;
};

string medalToStr(int medal)
{
    switch (medal) {
    case Gold:
        return "Gold";
    case Silver:
        return "Silver";
    case Bronse:
        return "Bronse";
    default:
        return "Empty";
    }
}

void printMembers(members &list)
{
    for (int i = 0; i < list.count ; i++)
        cout << /*i << " " <<*/ medalToStr(list.list[i].medal) << " in "
             << list.list[i].sport << " with " << list.list[i].points
             << " from " << list.list[i].country << endl;
}

void generate()
{
    ofstream file("members.dat", ios::binary|ios::trunc);

    member temp;

    for (int i = 0; i < sportsCount ; i++)
        for (int j = 0; j < countriesCount ; j++)
        {
            int count = rand()%5+5;
            for (int k = 0; k < count ; k++)
            {
                strcpy(&temp.sport[0], sports[i]);
                strcpy(&temp.country[0], countries[j]);
                temp.points = rand()%100;
                temp.medal = Empty;

                file.write((char*)&temp, sizeof(member));
            }
        }

    file.close();
}

members sportPrisevinners(const char* sport)
{
    //reading
    ifstream file("members.dat", ios::binary);
    member* loaded = new member[60];
    int pos = 0;
    while (!file.eof())
    {
        member temp;

        file.read((char*)&temp, sizeof(member));

        static bool reading = false;
        if (strncmp(&temp.sport[0], sport, strlen(sport))==0) {
            reading = true;
            loaded[pos++] = temp;
        } else if (reading) {
            break;
        }
    }
    file.close();

    //sorting
    int count = 3;
    for (int i = 0; i < pos-1 ; i++)
    {
        for (int j = i+1; j < pos ; j++)
            if (loaded[i].points<loaded[j].points)
            {
                member temp = loaded[i];
                loaded[i] = loaded[j];
                loaded[j] = temp;
            }

        if (i<count) {
            static int last = -1;

            if (loaded[i].points==last)
                count++;

            loaded[i].medal = count-i;
            last = loaded[i].points;
        } else break;
    }

    //returning
    members result;

    result.list = new member[count];
    memcpy(result.list, loaded, count*sizeof(member));
    /*for (int i = 0; i < count; i++)
        result.list[i] = loaded[i];*/
    result.count = count;

    delete[] loaded;

    return result;
}

int main(int /*argc*/, char */*argv*/[])
{
    srand(time(0));

    generate();

    members r = sportPrisevinners(sports[4]);
    printMembers(r);
    delete[] r.list;
    members l = sportPrisevinners(sports[5]);
    printMembers(l);
    delete[] l.list;

    system("pause");

    return 0;
}

1 个答案:

答案 0 :(得分:3)

我怀疑它是你函数中的静态局部变量。它们在每次调用函数时都不会具有相同的值,这可能会影响结果。这些变量的初始化只执行一次 - 它们第一次进入范围 - 因此每次调用该函数时,都会获取上次函数运行时这些变量的值。