2

我制作了一个简单的书籍制作程序,我一直在尝试使用 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;
}
4

3 回答 3

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"
于 2013-08-15T07:05:45.050 回答
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);

并摆脱变量genNum, genNumber, genCount.

于 2013-08-15T07:00:17.893 回答
1

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

于 2013-08-15T07:18:48.183 回答