Question

I have made a simple book making program, I've been trying to use a loop in the MAIN function to assign random numbers to the variable "genNum" using rand() in the generateNumbers() function, then pass 'genNum' value to three local variables in the MAIN and then passed to a member function in my BOOKIE class. The first two times the function loops it generates random numbers just fine, but the third time it loops it generates the same number it generated for the second loop. I have looked through all the threads regarding rand() and none answered my question. Here is the code:

This is the MAIN function

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;
}

this is the function for generating the random numbers:

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

This is the member function that receives the random generated values:

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;
}
Was it helpful?

Solution

Your code works fine, though I'd recommend std's new random generators!

Change

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

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

to

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

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

OTHER TIPS

Not a solution to your immediate problem (see @TheOtherGuy's answer for the actual bug fix), but your code is unnecessarily complicated - you can do the initialisation without using a loop, e.g. change:

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;
    }
}

to:

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

and get rid of the variables genNum, genNumber, genCount.

You duplicate the code, you duplicate the result. Try to rearrange your code using an array (or a c++ vector) of structures and avoid wild use of copy&paste. This would prevent you from making silly mistakes and will improve your coding skills.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top