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