Hi,

I have to create a structure for student record and save their data, but I am having trouble in storing that data.

This is what I have:

struct student
{
       char id[7];
       char firstname[20], lastname[20];
       char phoneno[15];
       char address[100];
       dobtype dateofbirth;
};

int main(int argc, char *argv[])
{
    int choice;
    student stdrec[200];
    int i=0;
    cout<<"************** Address Book Menu *********** "<<entries<<" ****"<<endl;
         cout<<"1. Add an Entry"<<endl;
         cout<<"2. Display all Entries"<<endl;
         cout<<"3. Search an Entry"<<endl;
         cout<<"4. Delete an Entry"<<endl;
         cout<<"5. Exit\n>>"<<endl;
    
         cin>>choice;
    
         switch(choice)
         {
                  case 1:
                       cout<<"Enter ID: ";
                       cin>>stdrec[i].id;
                       cin.ignore(200,'\n');
                       cout<<"Enter Firstname: ";
                       cin.getline(stdrec[i].firstname,20);
                       cin.ignore(200,'\n');
                       cout<<"Enter Lastname: ";
                       cin.getline(stdrec[i].lastname,20);
                       cin.ignore(200,'\n');
                       cout<<"Enter phoneno: ";
                       cin.getline(stdrec[i].phoneno,15);
                       cin.ignore(200,'\n');
                       cout<<"Enter address: ";
                       cin.getline(stdrec[i].address,100);
                       cin.ignore(200,'\n');
                       cout<<"Enter Date of Birth: ";
                       cout<<"Month: ";
                       cin>>stdrec[i].dateofbirth.month;
                       cout<<"Day: ";
                       cin>>stdrec[i].dateofbirth.day;
                       cout<<"Year: ";
                       cin>>stdrec[i].dateofbirth.year;
                       break;
                       
                  case 2:
                       break;                       
         }
    }

The problem is when I enter 7 digits for Id it works, but when I enter 20 character for firstname, it then just displays the rest cout statements and the program terminates.
I tried to debug it and what I found was when I input firstname, first 9 characters are concatenated with the Id and other 9 characters are saved in firstname, after that I doesnt let the user enter anything...

Recommended Answers

All 5 Replies

>>The problem is when I enter 7 digits for Id it works

No it does not work because the ID field of the structure is not large enough to hold a string of 7 characters plus the NULL terminating character. You need to increase the size of id to 8 char id[8];

Thanks for your reply.

I did change my code as you said and when I debug it its not concatenating the firstname with the Id but I still have the problem that it doesnt let me input lastname, phoneno.......
As the length of firstname is 20, I tried entering 19 character but it still waits for the 20th character...

cin.ignore() is not needed after calling getline() -- delete them

Ok, this is now what I have. Now it is getting the right data but the only issue is when I enter the address, of characters less than hundred, I have to press the enter key twice to go to the next option...

cout<<"Enter ID: ";
                       cin>>stdrec[i].id;
                       cin.ignore(200,'\n');

                       cout<<"Enter Firstname: ";
                       cin>>stdrec[i].firstname;
                       cin.ignore(200,'\n');

                       cout<<"Enter Lastname: ";
                       cin>>stdrec[i].lastname;
                       cin.ignore(200,'\n');

                       cout<<"Enter phoneno: ";
                       cin>>stdrec[i].phoneno;
                       cin.ignore(200,'\n');

                       cout<<"Enter address: ";
                       cin.getline(stdrec[i].address,100);
                       cin.ignore(200,'\n');

                       cout<<"Enter Date of Birth: \n";
                       cout<<"Month: ";
                       cin>>stdrec[i].dateofbirth.month;

                       cout<<"Day: ";
                       cin>>stdrec[i].dateofbirth.day;

                       cout<<"Year: ";
                       cin>>stdrec[i].dateofbirth.year;
                       cin.ignore(100,'\n');
                       i++;

Thanks for helping!

delete all those lines with cin.ignore(). It's only useful after entering an number with >> operator.

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.