0

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!

2
Contributors
5
Replies
6
Views
12 Years
Discussion Span
Last Post by Narue
0

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;
}
0

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

0

>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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.