lundi 4 mars 2019

How do I seed the random number generator with the current time, display header and assign random birth dates to X amount of people (C++)

I'm working on an assignment where I have to create a program that:

  1. Ask user for their name
  2. Prompts the user for # of voters
  3. Store user entered value
  4. Report errors if the user enters a negative number or a character
  5. seed the random number generator with the current time
  6. display header for (voterCount = 0; voterCount < numVoters; voter++)

  7. use random number generator to:

    generate bYear (restricted between 1900 and 2000)

    generate bMonth

    generate bDay (limit 1 and 31)

I have the first 4 down:

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{

    int  numVoters;
    char name[35]; // Variable that stores "Name" of user added max char to be 25
    const int MIN_MONTH = 1;
    const int MAX_MONTH = 12;
    int bMonth;


    cout << "Please enter your first name: ";
    cin.getline(name,35); // used getline because without getline I kept getting a single char

    cout << "Hello " << name << endl;
    cout << "Please enter amount of voters: ";
    cin  >> numVoters;
    while (numVoters > 0)
    {
        cout << "You entered: " <<numVoters << endl;
    }
        cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM 
        AGAIN";

    return 0;

    }

I don't know what to do next I've tried to continue by using what I see online but when I do I get a never-ending loop of # of voters

#include < iostream > 
#include < cstdlib > 
#include < time.h >

  using namespace std;

int main() {

  int numVoters;
  char name[35]; // Variable that stores "Name" of user added max char to be 25
  const int MIN_MONTH = 1;
  const int MAX_MONTH = 12;
  int bMonth;

  cout << "Please enter your first name: ";
  cin.getline(name, 35); // used getline because without getline I kept getting a single char

  cout << "Hello " << name << endl;
  cout << "Please enter amount of voters: ";
  cin >> numVoters;
  while (numVoters > 0) {
    cout << "You entered: " << numVoters << endl;
  }
  cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM AGAIN";

  unsigned seed = time(0); // gets current computer time
  srand(seed); // seeds the random number gen

  for (numVoters = 0; numVoters < 0; numVoters++)

  {

    bMonth = rand() % (MAX_MONTH - MIN_MONTH + 1) + MIN_MONTH;
    cout << "Random month: " << bMonth << endl;
  }
  return 0;

}

Help with #5-7

Algorithm.




Aucun commentaire:

Enregistrer un commentaire