Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8913525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:29:09+00:00 2026-06-15T04:29:09+00:00

I’m doing an homework that require me to check if the Student is over

  • 0

I’m doing an homework that require me to check if the Student is over 18.
My function is this:

bool Student::isOverEighteen() {
  int date[3]; // D/M/Y
  char *pdata;

  pdata = strtok(Anagrafica::birth, "/"); // Anagrafica is the base class

  for (short i = 0; pdata != NULL; i++) {
    data[i] = pdata;
    pdata = strtok(NULL, "/");
  }

  time_t t    = time(NULL);
  tm *locale  = localtime(&t);

  if (data[0] < locale->tm_mday &&
     (data[1] < locale->tm_mon + 1  || data[1] == locale->tm_mon + 1) &&
     (locale->tm_year + 1900 - data[3] > 18))
  {
    return true;
  } else {
    return false;
  }
}

However, when I display the date of birth it show me the date plus the classroom of the student. For example: 25/06/19944A (4A is the name of the classroom)

The function that I use to register students information:

Student::Student() {
  std::cout << "Name: ";
  std::cin.getline(Anagrafica::name, 101);
  std::cout << "Surname: ";
  std::cin.getline(Anagrafica::surname, 101);
  std::cout << "Birth (XX/XX/XXXX): ";
  std::cin.getline(Anagrafica::birth, 11);
  std::cout << "Classroom: ";
  std::cin.getline(Anagrafica::classroom, 101);
}

The function for displaying them:

void Anagrafica::Show() {
  std::cout << "\nName:" << this->name;
  std::cout << "\nSurname:" << this->surname;
  std::cout << "\nBirth:" << this->birth;
  std::cout << "\nClassroom: " << this->classroom;
  std::cout << std::endl;
}

And they are declared:

char name[100];
char surname[100];
char birth[10];
char classroom[100];

Any solution for making this working?

EDIT (for Nik Bougalis):

Here is the one I use now.
The problems with strings started because I was using c_str; instead of c_str();

bool Entry::IsOverEighteen() {
  int date[3];

  date[0] = std::atoi(this->birth.substr(0, 2).c_str());  // Day
  date[1] = std::atoi(this->birth.substr(4, 2).c_str());  // Month
  date[2] = std::atoi(this->birth.substr(6, 4).c_str());  // Year

  time_t t  = time(NULL);
  tm *local = localtime(&t);

  // Perche' sia maggiorenne occorre che:
  //  Il giorno attuale sia maggiore di quello di nascita
  //  Il mese attuale sia maggiore o uguale a quello di nascita
  //  L' anno attuale - l' anno di nascita sia maggiore o uguale 18
  if (local->tm_mday > date[0] && 
     (local->tm_mon + 1 > date[1] || local->tm_mon + 1 == date[1]) && 
     (local->tm_year + 1900 - date[2] > 18 || local->tm_year + 1900 - date[2] == 18))
  {
    return true;
  } else {
    return false;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T04:29:10+00:00Added an answer on June 15, 2026 at 4:29 am

    The problem is that your char birth[10] is exactly as long as the string 25/06/1994 (i.e. 10 bytes) leaving no space for the NULL terminator, so when printing out the birth string, cout starts reading, continues past the end of birth and onto classroom.

    Also note that you consistently call cin.getline with a buffer size that is one LARGER than the actual buffer. You’re in essence telling getline: “here’s a 10 byte buffer… read 11 bytes into it!” Is that what you really mean?

    All of this, of course, would not have happened if you used std::string instead of char[]. Why didn’t you?

    Now, as far as isOverEighteen is concerned: the function is completely broken, and in fact, the version you show here won’t even compile. Let’s see if we can go about fixing it.

    My first question, of course, would be why not simply have the birthday input as 3 integers directly instead of accepting it as a string? But let’s assume you can’t for your assignment. Try using this instead:

    bool isOverEighteen(const string &s) 
    { // s is in the form DD/MM/YYYY - if it's not, things blow up.  
      int birthday[3], bidx = 0; 
    
      birthday[0] = 0;
      birthday[1] = 0;
      birthday[2] = 0;
    
      for(int i = 0; i != s.length(); i++)
      {
        if(s[i] == '/')
        {
          bidx++;
          continue;
        }
    
        birthday[bidx] = (birthday[bidx] * 10) + (s[i] - '0');
      }
    
      // now birthday[0] is the day of birth as an integer, 
      // birthday[1] is the month of birth as an integer and
      // birthday[2] is the year of birth as an integer. You 
      // can use them.
    
      ...
    }
    

    There are more elegant ways of doing this, and it’s not very “C++” but it works, which is an improvement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.