C++/Pointers

Advertisement


Question
Hello,

i'm trying to write a code for this question, its not working out though,

write a program that reads a string of day in format
20, june 1977
and prints it on screen in another formate
05/20/77
it should work for any date;

this is the code that i wrote, its functinioning, plz help me, i have a final this sunday, and pointers are killing me

#include <iostream.h>
#include <string.h>

void date_format( char *, int );
void date( char *, char * );
void month( char *, char * );
void year( char *, char * );

void main()
{

  const int size = 20;
  char date_format1[size] = "20, june 1977";
  char date_format2[size] = {'\0'};
  char *tokenptr1, *tokenptr2;

  tokenptr1 = date_format1;
  cout << tokenptr1 << endl;

  tokenptr1 = strtok( date_format1, " " );
  date( tokenptr1, tokenptr2 );

     for( int i = 1; i <= 3; i++ ){
        tokenptr1 = strtok( NULL, " " );
        if( i == 1 )
        month( tokenptr1, tokenptr2 );
        if( i == 2 )
        year( tokenptr1, tokenptr2 );
     }

int q;
cin >> q;

}

void date( char *ptr1, char *ptr2 ){

  char a[5];

  ptr2 = strtok( ptr1, "," );
  strcat( a, ptr2 );
  strcat( a, "/" );
  cout << a << endl;



}


void month( char *ptr1, char *ptr2 ){

  char a[10];
  cout << ptr1 << endl;
  ptr2 = strtok( ptr1, " " );
  ptr2 = strtok( NULL, " " );
  cout << ptr2 << endl;

     switch( ptr2 ){

        case 'january':
         ptr2 = "01";
         break;
        case 'february':
         ptr2 = "02";
         break;
        case 'march':
         ptr2 = "03";
         break;
        case 'april':
         ptr2 = "04";
         break;
        case 'may':
         ptr2 = "05";
         break;
        case 'june':
         ptr2 = "06";
         break;
        case 'july':
         ptr2 = "07";
         break;
        case '08':
         ptr2 = "august";
         break;
        case '09':
         ptr2 = "september";
         break;
        case 'october':
         ptr2 = "10";
         break;
        case 'november':
         ptr2 = "11";
         break;
        case 'december':
         ptr2 = "12";
         break;

        default:
         cout << "no" << endl;
     }
  cout << ptr2 << endl;
}


void year( char *ptr1, char *ptr2 ){

ptr2 = strtok( ptr1, "\0" );
cout << ptr2 << endl;

cout << ptr2 << endl;

}  

Answer
To get a quick answer please give me more details - what is the output you get, and where do you suspect is the problem, and anything else which might help me in answering your question quickly. Another remark: your program is more C, than C++. One of the reasons C++ was invented is so that you use less of pointers. Here you should have used std::string in place of char*.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.