Hey guys im having a problem in structures..
i dont know how to initialize a an array of 10 contact instances.. plz help me out.

this is my question statement..
"Declare a structure Contact that has a name, address, mobile number. Now create and (hardcoded) initialize an array of 10 contact instances. Prompt the user to enter the starting letters of the name to be searched. Your program must now print the details of only those contacts whose name start with the letters specified by the user. E.g. if the user enters Sa (independent of case) then your program will print all that contacts whose name start with sa i.e. saima, sara, saad, sarmad etc. but if user entered sar then only sara and sarmad would be printed "

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


using namespace std;

int main()
{

    struct MyStruct
    {
        string name;
        string address;
        string mobno;

    } ms;           //ms is my instance



    getch();
    return 0;
}

my structure includes.

You're meant to make an array of 10 of them.

MyStruct anArrayOfTen[10];

Firstly ...

please note that it is a bad idea to use

#include <conio.h> 

// and later ... //

getch(); 

if you wish to have portable code that conforms to standard C++

Maybe, by "hard-coding" you were thinking of this ?

Consider:

struct Student
{
    int id;
    string name;
};

// ...

Student studs[] =
{
    { 1, "Sam" },
    { 2, "Joe" },
    { 3, "Pat" )
} ;
const int size = sizeof studs / sizeof *studs;


// ...

for( int i = 0; i < size; ++ i )
    cout << studs[i].id << ", " << studs[i].name << '\n';

You can initialize a structure, or array of structures, with memset(). Example:

#include <stdio.h>
#include <iostream>
struct MyStruct
{
    string name;
    string address;
    string mobno;
};
int main(void)
{
    struct MyStruct aStruct;
    struct MyStruct arrayOfStructs[0];
    ::memset((void*)&aString, 0, sizeof(struct MyStruct));
    ::memset((void*)&arrayOfStructs[0], 10, sizeof(struct MyStruct));
    return 0;
}

@rubberman

Do you forget that this was a C++ program ... and just using C stuff in it :)

http://en.cppreference.com/w/cpp/string/byte/memset

// demo_memset.cpp //

/*
http://en.cppreference.com/w/cpp/string/byte/memset
*/


#include <iostream>
#include <cstring> // re. memset //

const int BUF_LEN = 20;

struct Student
{
    int id;
    char fName[BUF_LEN];
    char lName[BUF_LEN];

    void print() const
    {
        std::cout << id << ": " << lName << ", " << fName;
    }
} ;


int main()
{
    int iAry[3];
    std::memset( iAry, 0, sizeof iAry ) ; // initial all memory to 0 //

    std::cout << "sizeof iAry = " << sizeof iAry << '\n';

    iAry[1] = 1; // give one array element same value //

    for( int i = 0; i < 3; ++ i )
        std::cout << i << ", " << iAry[i] << '\n' ;

    std::cout << "sizeof iAry = " << sizeof iAry << '\n';



    Student myStuds[3];
    std::memset( myStuds, 0, sizeof myStuds );

    std::cout << "sizeof myStuds = " << sizeof myStuds << '\n';


    // give one array element same value //

    myStuds[1].id = 1;
    strcpy( myStuds[1].fName , "Sam" );
    strcpy( myStuds[1].lName , "Jones" );

    for( int i = 0; i < 3; ++ i )
    {
        myStuds[i].print();
        std::cout << '\n';
    }
    std::cout << "sizeof myStuds = " << sizeof myStuds << '\n';
}

Using memset to set every byte of a std::string object to zero is a bad idea. Really bad...

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.