I have a program to write that takes in the date in ##/##/## format. I then write the month day, year output. What I'm totally lost on is how to get from reading the char array to integer numbers?? Big time Lost! Need help!

Dani AI

Generated

Two compact, beginner-friendly options that work well alongside ’s stream-based idea are shown below. One uses C-style parsing (handy with a C char array), the other uses std::string slices and std::stoi. Both include a simple two-digit-year → 20xx mapping and basic validation guidance.

// sscanf-style (works from a C-string buffer)
char buf[] = "12/25/21";
int month=0, day=0, year=0;
char junk;
if (sscanf(buf, "%2d/%2d/%2d%c", &month, &day, &year, &junk) == 3) {
    if (year < 100) year += 2000;      // map "21" -> 2021
    const char* names[] = { "January","February","March","April","May","June",
                            "July","August","September","October","November","December" };
    if (month >= 1 && month <= 12) {
        printf("%s %02d, %d\n", names[month-1], day, year);
    }
}
// std::string + substr + stoi (pure C++ string handling)
std::string s = "12/25/21";
size_t p1 = s.find('/'), p2 = s.find('/', p1+1);
if (p1!=std::string::npos && p2!=std::string::npos) {
    int month = std::stoi(s.substr(0,p1));
    int day   = std::stoi(s.substr(p1+1,p2-p1-1));
    int year  = std::stoi(s.substr(p2+1));
    if (year < 100) year += 2000;
    // validate as below...
}

Validation notes (important): verify month is 1..12, check day is 1..daysInMonth(month,year) and handle February leap years (leap if (y%4==0 && (y%100!=0 || y%400==0))). For strict parsing with no extra trailing characters, the sscanf trick above detects leftovers; with std::string, ensure the find/substring positions match exactly. These snippets complement ’s approach and give a straightforward path for to convert an input string into integers, validate them, and print the full month name with a 20xx year.

Recommended Answers

All 5 Replies

Is this C or C++?

c++, guess I should have included that.

Here's a start:

#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
  istringstream in ( "03/30/05" );
  int m, d, y;

  in>> m;
  if ( in.peek() != '/' ) {
    cerr<<"Invalid format"<<endl;
    return EXIT_FAILURE;
  }

  in.ignore();
  in>> d;
  if ( in.peek() != '/' ) {
    cerr<<"Invalid format"<<endl;
    return EXIT_FAILURE;
  }

  in.ignore();
  in>> y;

  cout<< m <<' '<< d <<' '<< y <<endl;
}

Thanks for the code, however, I'm really at a loss to how it works. We've not yet gone this far in C++, only arrays & some work with strings. Can I put a cout prompt & cin to input the date this way? ex.

cout << "Input date in format mm/dd/yy :\n";
char date[9];
cin >> date;

As I have to error check the date, and later output it in a function converting to monthname dd, 20##.

>We've not yet gone this far in C++
Oh, so this is homework? In that case you'll need to show some effort in the form of an existing attempt, or detailed examples of what steps you went through while trying to figure out a solution and what you learned in the process. And posting here doesn't count.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.