mercredi 4 août 2021

how to fix a program jumping cin.getline() function [duplicate]

I want to insert the name with space include
for eg
Enter Name : jacob john
and i use cin.getline(name,20) but my program
jump instead of accpting data in name section
Please help me
what is the problem
and how to fix this isssue
i tried my best but it didn't work
i also tried cin.get(name,20) but same it didn't work
the only thing it works if it is cin>>name
but i dont want cin>>name b/c i doesn't allow and space

#include <iostream>
#define SIZE 100
using namespace std;


int displaycounter=0;

   class Record{
    private:
        char name[20];
        int age;
        char sex;
        int id;
    public:
        void addrecord();
    
   };
   
   void Record::addrecord(){
        displaycounter++;
        cout<<"\tData :"<<displaycounter<<endl;
        cout<<"\tEnter Name :";
        cin.getline(name,19);
        cout<<"\tEnter Age :";
        cin>>age;
        cout<<"\tEnter Sex :";
        cin>>sex;
        cout<<"\tEnter ID :";
        cin>>id;
        
        
        cout<<endl;
       
       }
   
int main(){
      int choice,n,searchID,idvalue;
      Record data[SIZE];
      do{
         cout << "\n\t PERSONEL RECORDS" << endl;
    cout << "\n\t---------------------------------------" << endl;
    cout << "\n\t\t1-Add New Record" << endl;
    cout << "\n\t\t2-Search Record" << endl;
    cout << "\n\t\t3-Edit Record" << endl;
    cout << "\n\t\t4-Delete Record" << endl;
    cout << "\n\t\t5-Display All Records" << endl;
    cout << "\n\t\t0-EXIT" << endl;
    cout << "\n\t---------------------------------------" << endl;
    cout << "\n\t\tEnter Your choice:";
    cin >> choice;
    if(choice==1)
    {
       cout<<"\n\tHow many records do you want to enter :";
       cin>>n;
       
       for(int i=0;i<n;i++)
       {
         
        
        
           data[i].addrecord();     
    
       }
       displaycounter=0;    
    }
      }while(choice !=0);
}

Aucun commentaire:

Enregistrer un commentaire