如何使用当前时间播种随机数生成器,显示标题并将随机出生日期分配给X个人(C ++)

问题描述 投票:1回答:2

我正在完成一项任务,我必须创建一个程序:

  1. 询问用户的名字
  2. 提示用户选择#
  3. 存储用户输入的值
  4. 如果用户输入负数或字符,则报告错误
  5. 使用当前时间为随机数生成器播种
  6. 显示标题(voterCount = 0; voterCount <numVoters; voter ++)
  7. 使用随机数生成器: 生成bYear(限制在1900年和2000年之间) 生成bMonth 生成bDay(限制1和31)

我有前4下来:

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

    }

我不知道下一步该做什么我试图继续使用我在网上看到的东西,但当我这样做时,我得到一个永无止境的选民#循环

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

}

帮助#5-7

Algorithm.

c++ random generator
2个回答
1
投票
while (numVoters > 0)
{
    cout << "You entered: " <<numVoters << endl;
}
    cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM 
    AGAIN";

上面的代码说,只要numVoters大于0,就无休止地输出numVoters。你需要的是一个if else声明,因为你想说如果选民人数大于0,输出选民人数并继续执行任务5,6和7.否则,你想输出错误。在伪代码中:

if (numVoters > 0) {
    output numVoters
    do tasks 5,6,7
}
else {
    output error
    return 0
}

或者你可以改为,如果numVoters等于或小于0输出错误并退出程序。在if循环之后放置任务5,6,7的代码,因此如果没有错误则执行:

if (numVoters =< 0) { 
    output error
    return 0
}
//code for 5,6,7 after and outside of the if loop

还有(voterCount = 0; voterCount < numVoters; voter++),在我看来是(int voterCount = 0; voterCount < numVoters; voterCount++)

一般来说,我认为你误解了for循环。你应该阅读它们以了解你哪里出错了。


-1
投票

正如有人所说,如果给出有效值,你的while循环是无限的

gettimeofday()信息可以在这里找到http://man7.org/linux/man-pages/man2/gettimeofday.2.html

rand_r()信息可以在这里找到https://linux.die.net/man/3/rand_r

保持这些方便,这些都是未来课程的好参考!

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

using namespace std;
const int MAX_LENGTH = 35;
const int MAX_MONTH = 12;
const int MAX_DAY = 31;
const int MAX_YEAR = 100;
void getName(char name []);
void sayName(char name []);
int getVoters();

int main() {

  int numVoters;
  char name[MAX_LENGTH]; // Variable that stores "Name" of user added max char to be 25

  struct timeval tv;

  getName(name);
  sayName(name);
  numVoters = getVoters();

  unsigned int seed = time(0); // gets current computer time
  gettimeofday(&tv,NULL);

  int day = 0;
  int month = 0;
  int year = 0;
  int voterNumber = 1;
  for (int i = numVoters;i > 0;i--)
  {
    seed = (unsigned int)((tv.tv_sec+tv.tv_usec)*i);
    month = rand_r(&seed)%MAX_MONTH + 1;
    day = rand_r(&seed)%MAX_DAY + 1;
    year = rand_r(&seed)%(MAX_YEAR+1) + 1900;

    cout<<"Voter #"<<voterNumber++<<" DD/MM/YYY DOB: ";
    cout<<day<<"/"<<month<<"/"<<year<<endl;
  }
  return 0;

}



void getName(char name[])
{
    cout<<"Please enter your first name: ";
    cin.getline(name,35);
}

void sayName(char name [])
{
    cout<<"Hello "<<name<<endl;
}

int getVoters()
{
    int voters = -1;
    cout<<"Please enter amount of voters: ";
    cin>>voters;

    if(voters < 0)
    {
        cout << "ERROR! ERROR! WRONG DATA TYPE PLEASE RUN THE PROGRAM AGAIN";
    }
    else if(voters == 0)
    {
        cout<<"Either you entered 0 or an invalid date type";
    }
    else
    {
        cout << "You entered: " << voters << endl;
    }
    return voters;

}
© www.soinside.com 2019 - 2024. All rights reserved.