简体   繁体   English

rand() 和 srand() 未在此 scope 中声明

[英]rand() and srand() are not declared in this scope

I'm required to write a program that gives a person's day and month of birth and will then figure out the person's horoscope sign.我需要编写一个程序,给出一个人的出生日期和月份,然后计算出这个人的星座。 The program should prompt the user for a number of persons.该程序应该提示用户输入一些人。 When the user responds, the program needs to automatically generate day and month and figure out and display the corresponding horoscope sign.当用户响应时,程序需要自动生成日期和月份,并找出并显示相应的星座。 The program also needs to track number of persons for each sign and display that statistics.该程序还需要跟踪每个标志的人数并显示该统计数据。 After that, the program needs to prompt the user if they want to go again.之后,程序需要再次提示用户是否要 go。 If the user responds with either a 'Y' or a 'y' the program needs to repeat the process until the user says that s/he is done.如果用户以“Y”或“y”响应,则程序需要重复该过程,直到用户说他/她完成了。

I have a learning disability and i find it hard to understand sometimes what the questions that are being asked.我有学习障碍,有时我很难理解被问到的问题。 I do not know how to answer them sometimes.有时我不知道如何回答他们。 So please be patience with me.所以请耐心等待我。

#include <ctime>
#include <iomanip>
#include <iostream>

using namespace std;
int main() {
  int month, date, i, nOfPeople;
  i = 0;

  cout << "How many people: " << endl;
  cout << "Born on    Horoscope Sign" << endl;
  cout << "-------------------------" << endl;

  cin >> nOfPeople;

  while (i < nOfPeople) {
    // initialize random seed:
    srand(time(NULL))
    // generate month and date
    month = rand() % 12 + 1;
    date = rand() % 28 + 1;

    if (month == 3 && date >= 21 || month == 4 && date <= 19) {
      cout << "4/19 Aries" << endl;
    } else if (month == 4 && date >= 20 || month == 5 && date <= 20) {
      cout << "5/7 Taurus" << endl;
    } else if (month == 5 && date >= 21 || month == 6 && date <= 21) {
      cout << "Gemini" << endl;
    } else if (month == 6 && date >= 22 || month == 7 && date <= 22) {
      cout << "7/8 Cancer" << endl;
    } else if (month == 7 && date >= 23 || month == 8 && date <= 22) {
      cout << "8/17 Leo" << endl;
    } else if (month == 8 && date >= 23 || month == 9 && date <= 22) {
      cout << "Virgo" << endl;
    } else if (month == 9 && date >= 23 || month == 10 && date <= 22) {
      cout << "Libra" << endl;
    } else if (month == 10 && date >= 23 || month == 11 && date <= 21) {
      cout << "11/3 Scorpio" << endl;
    } else if (month == 11 && date >= 22 || month == 12 && date <= 21) {
      cout << "12/11 Saguittarius" << endl;
    } else if (month == 12 && date >= 22 || month == 1 && date <= 19) {
      cout << "12/22 Capricorn" << endl;
    } else if (month == 1 && date >= 20 || month == 2 && date <= 18) {
      cout << "Aquarius" << endl;
    } else if (month == 2 && date >= 19 || month == 3 && date <= 20) {
      cout << "2/21 Pieces" << endl;
    }

    i++;
  }
  return 0;
}

std::rand and std::srand are defined in <cstdlib> . std::randstd::srand<cstdlib>中定义。 Include that header then you should be able to use those functions.包括 header 那么你应该能够使用这些功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM