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

  • Home
  • SEARCH
  • 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 8647117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:59:09+00:00 2026-06-12T12:59:09+00:00

I’m running through some programming exercises and one involves implementing a date class object

  • 0

I’m running through some programming exercises and one involves implementing a date class object from scratch.

Overall the class design and implementation part of it was a piece of cake but the trouble came along during date input and copying to private data members.

The date input method must be in the format MM/DD/YY and class data members must be three int types (month, day, year) for the exercise.

So my implementation consists of taking input using cin to a character array and then converting those array elements to int data types for the class data members.

Initially I thought to take each number column separately, multiply the tens column by ten and add the ones column in to get the int but that didn’t pan out since your still doing mathematical operands on a character data type.

I did find a solution, but its a kludge and I feel there’s gotta be a better, more eloquent way of doing this.

Also as a side note, I was wondering if someone could tell me why I get a stack overflow error during runtime when I reduce my char array size to 8 from 9. I thought char arrays only needed the size + 1 for the \0 character. By my count I should be good with temp[8] for a total of 9 spots (2 day 2 month 2 year and 2 / and 1 \0 =9) I’m sure I’m missing something here. Anyways i’m using VC++ 2008 for my compiler if that matters.

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
using std::cin;

//---------------------------------------------------------------------------
class date {
private:
    int day, month, year;
public:
    date() : day(0), month(0), year(0) {};
    void getDate(char*);
    void showDate();
    int chartoint(char);
};

int date::chartoint(char a) {
    switch(a) {
        case '0': return 0;
        case '1': return 1;
        case '2': return 2;
        case '3': return 3;
        case '4': return 4;
        case '5': return 5;
        case '6': return 6;
        case '7': return 7;
        case '8': return 8;
        case '9': return 9;
    };
};

void date::getDate(char dArray[]) {

    day=(chartoint(dArray[3])*10+chartoint(dArray[4])); 
    month=(chartoint(dArray[0])*10+chartoint(dArray[1])); 
    year=(chartoint(dArray[6])*10+chartoint(dArray[7])); 


};

void date::showDate()
{
    cout << std::setiosflags(std::ios::fixed | std::ios::showpoint);
    (month<10) ? cout << std::setw(2) << std::setfill('0') << month : cout << month;
    cout << "/";
    (day<10) ? cout << std::setw(2) << std::setfill('0') << day : cout << day;
    cout << "/";
    (year<10) ? cout << std::setw(2) << std::setfill('0') << year : cout << year;
    cout << endl;


};
//---------------------------------------------------------------------------
int main()
{
    date a;
    char temp[9];

    cout << "Enter Date (mm/dd/yy): ";
    cin >> temp;

    a.getDate(temp);

    a.showDate();
    return 0;
}
  • 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-12T12:59:10+00:00Added an answer on June 12, 2026 at 12:59 pm

    Extremely simple solution (although C-style) would be to read int directly from char* by using function sscanf():

    void date::getDate(const char *sDate) // const = "getDate will not change sDate"
    {
        sscanf(sDate, "%d/%d/%d", &month, &day, &year);
    };
    

    C++ solution would use std::string instead of char*, thus body of main should look like this:

    date a;
    std::string temp;
    
    cout << "Enter Date (mm/dd/yy): ";
    cin >> temp;
    
    a.getDate(temp);
    a.showDate();
    

    then your getDate method could take reference to std::string object, that is not going to change, in other words const std::string&. For reading values from std::string you could construct std::istringstream object then:

    #include <sstream>
    ...
    void date::getDate(const std::string& sDate)
    {
        std::istringstream ssDate(sDate);
        ssDate >> month;
        ssDate.ignore();
        ssDate >> day;
        ssDate.ignore();
        ssDate >> year;
    };
    

    Also note that method chartoint is redundant and showDate() could be simple like this:

    void date::showDate()
    {
        cout << std::setw(2) << std::setfill('0') << month << '/';
        cout << std::setw(2) << std::setfill('0') << day << '/';
        cout << std::setw(2) << std::setfill('0') << year << endl;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I am doing a simple coin flipping experiment for class that involves flipping a
I am currently running into a problem where an element is coming back from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

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.