我的程序跳过getline(cin,CustInfo),进入CustomerInfo函数中的City.我该怎么修

My Program skips the getline(cin, CustInfo) and proceeds to City in the CustomerInfo function. How can I fix it?

本文关键字:函数 City CustomerInfo 进入 程序 getline CustInfo cin 我的      更新时间:2023-10-16
CustAcc CustomerInfo( ){
    CustAcc CustInfo;                           //Define a local structure
        //Customer Information
        cout<<"Name: ";
        getline(cin, CustInfo.Name);
        cout<<"City: ";
        cin>>CustInfo.Address.City;
        cout<<"Enter the state where you live: ";
        cin>> CustInfo.Address.State;
        cout<<"Enter the ZIP code of your area: ";
        cin>> CustInfo.Address.ZIP;
        cout<<"Enter your telephone number without hyphens and space (e.g. 6060543013) : ";
        cin>> CustInfo.Tel_Num;
        CustInfo.Acc_Balance = 0;
        do{
            cout<<"Enter the balance in your account (balance below 0 is not accepted):";
            cin>>CustInfo.Acc_Balance;
        }while(CustInfo.Acc_Balance < 0);
        cout<<"Enter the date of last Payment. Use no space or article to distinguish between month, date and year.(e.g 092016): ";
        cin>> CustInfo.Date_of_LastPayment;
    return CustInfo;
}

我只是在学习c++。我想,也许,cin.ignore会有所帮助。我想你把它放在getline前面了。

下面是一个示例程序:

 int main()
 {
      char ch;
      int number;
      cout << "Enter a number: ";
      cin >> number;
      cin.ignore();
      cout << "Enter a character: ";
      ch = cin.get();
      cout << "Thank you!n";
      return 0;
      }