Hi,
I hav defined an array of n elements.Each element represents a structure. Like,

typedef struct 
{
int dd;
int mm;
int yyyy;
}srik;
srik a[n];

Then how to assign a date (eg:02/07/1984) as a string("02071984") to the first array element(means the first structure) in the array.
I thought .

scanf("%s",a[0]);

/*here the input given is "02071984"*/

But it is not working......

if i print( printf("%s",a[0] ) it is showing garbage vlue

Could U please suggest me the right procedure for the problem.

Thanks & Regards.

you have to break the string apart and convert each part to an integer. There is no easy or quick solution to that problem because how to do it depends on the format of the string. If you are certain the string is in the format MMDDYYYY and the string is "02071984"

a[0].dd = atoi("07");
a[0].mm = atoi("02");
a[0].yy = atoi("1984");

Your only real problem is how to create the three strings "07", "02" and "1984" from the original string.

This works:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    char *t1 = "1234567";
    int  mm,dd,yy;
   
    sscanf(t1, "%2d%2d%d", &mm,&dd,&yy);
    cout << mm << " " << dd << " " << yy << endl;
   
    return (0);
}

The other oprion is stringstream I suppose.

then tell me friend how to give iputs to the array elements a[0],a[1]........during the run time from key board........

cant we giv the input directly as a string to the arry elements like "01231987".

Please ask a full question. What do you need now? Do you have any more code?

cant we giv the input directly as a string to the arry elements like "01231987".

Of course you can -- see fgets() function or for c++ see iostream's cin class.

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.