rand()在循环中复制数字

时间:2013-08-15 06:42:53

标签: c++ random srand

我制作了一个简单的书籍制作程序,我一直试图在MAIN函数中使用循环,使用generateNumbers()函数中的rand()将随机数分配给变量“genNum”,然后传递'genNum '对MAIN中的三个局部变量赋值,然后传递给我的BOOKIE类中的成员函数。函数循环的前两次生成随机数就好了,但第三次循环它会生成为第二个循环生成的相同数字。我查看了关于rand()的所有线程,但没有人回答我的问题。这是代码:

这是主要功能

int main()
{
    bool genNumber = false;

    int genCount = 0;
    double genNum;
    double genNum1; double genNum2; double genNum3;
    Bookie bookKeeper;
    srand(time(0));
    bookKeeper.welcomeMessage();

    clientEntry();
    bookKeeper.clientList(firstClient, secondClient, thirdClient);
    while(genNumber == false) {
    generateNumbers(genNum);//This is where the numbers are generated
    genCount++;
    if(genCount == 1) {
        genNum1 = genNum;
    }
    if(genCount == 2) {
        genNum2 = genNum;
    }
    if(genCount == 3) {
        genNum3 = genNum;
        bookKeeper.clientStatus(genNum1, genNum2, genNum3);
        genNumber = true;
      }
   }

  return 0;
}

这是生成随机数的函数:

void generateNumbers(double & number) {
number = 1 + rand() % 500;
}

这是接收随机生成值的成员函数:

void Bookie::clientStatus(double num1, double num2, double num3) {
clientAOwes = num1; clientBOwes = num2; clientCOwes = num3;
string answer;

    enum Status{PAID, LATE};
Status clientAStat;
Status clientBStat;
Status clientCStat;

bool cStatus = false;
while(cStatus == false){
    cout<<clientA <<" owes " <<clientAOwes <<".\n"
        <<"Has this client paid, (Y)es or (N)o?\n"
        <<"::";
    getline(cin, answer);
    if(answer == "Y") {
        clientAStat = PAID;
        Astat = "PAID";
    }
    if(answer == "N") {
        clientAStat = LATE;
        Astat = "LATE";
    }

    //Client 2
    cout<<clientB <<" owes " <<clientBOwes <<".\n"
        <<"Has this client paid, (Y)es or (N)o?\n"
        <<"::";
    getline(cin, answer);
    if(answer == "Y") {
        clientBStat = PAID;
        Bstat = "PAID";
    }
    if(answer == "N") {
        clientBStat = LATE;
        Bstat = "LATE";
    }

    //Client 3
    cout<<clientC <<" owes " <<clientBOwes <<".\n"
        <<"Has this client paid, (Y)es or (N)o?\n"
        <<"::";
    getline(cin, answer);
    if(answer == "Y") {
        clientCStat = PAID;
        Cstat = "PAID";
    }
    if(answer == "N") {
        clientCStat = LATE;
        Cstat = "LATE";
    }
    cStatus = true;
}

cout<<fixed <<setprecision(2);
cout<<setw(4) <<"STATUS" <<setw(21) <<"CLIENT" <<'\n' <<endl;
cout<<setw(4) <<Astat <<setw(21) <<clientA <<'\n' <<endl;
cout<<setw(4) <<Bstat <<setw(21) <<clientB <<'\n' <<endl;
cout<<setw(4) <<Cstat <<setw(21) <<clientC <<'\n' <<endl;
cout<<'\n' <<endl;
}

3 个答案:

答案 0 :(得分:4)

你的代码工作正常,但我推荐std的新随机生成器!

更改

//Client 2
    cout<<clientB <<" owes " <<clientBOwes <<".\n"

//Client 3
    cout<<clientC <<" owes " <<clientBOwes <<".\n"

//Client 2
    cout<<clientB <<" owes " <<clientBOwes <<".\n"

//Client 3
    cout<<clientC <<" owes " <<clientCOwes <<".\n"

答案 1 :(得分:2)

不能解决您的直接问题(请参阅@ TheOtherGuy对实际错误修复的回答),但您的代码不必要地复杂 - 您可以在不使用循环的情况下进行初始化,例如:改变:

while(genNumber == false) {
    generateNumbers(genNum);//This is where the numbers are generated
    genCount++;
    if(genCount == 1) {
        genNum1 = genNum;
    }
    if(genCount == 2) {
        genNum2 = genNum;
    }
    if(genCount == 3) {
        genNum3 = genNum;
        bookKeeper.clientStatus(genNum1, genNum2, genNum3);
        genNumber = true;
    }
}

为:

generateNumbers(genNum1);
generateNumbers(genNum2);
generateNumbers(genNum3);
bookKeeper.clientStatus(genNum1, genNum2, genNum3);

并删除变量genNumgenNumbergenCount

答案 2 :(得分:1)

您复制代码,复制结果。尝试使用结构的数组(或c ++向量)重新排列代码,避免疯狂使用复制和粘贴。这样可以防止你犯错误,并提高你的编码技能。