繁体   English   中英

字符串类型向量 C++

[英]String Type Vector C++

在我下面分享的代码中,我想在AddStoreCustomer()部分中使用 output 向量数据类型。 这就是代码的工作方式。 但是代码中有一个错误。 如果我在客户变量中输入一个单词,代码就可以正常工作。 但是当我在这个变量中输入两个单词并且它们之间有一个空格时,代码不起作用。

    #include <iostream>
        #include <vector>
    
        using namespace std;
    
        class Store {
            int StoreID;
            string StoreName;
            string StoreCity;
            string StoreTown;
            string StoreTel;
            vector <string> StoreCustomer;
    
            public:
               Store(){}
               Store(int sid, string sname, string scity, string stown, string stel, string scustomer) {
                  setStoreID(sid);
                  setStoreName(sname);
                  setStoreCity(scity);
                  setStoreTown(stown);
                  setStoreTel(stel);
                  setStoreCustomer(scustomer);
               }
               void setStoreID(int sid) {
                  StoreID = sid;
               }
               void setStoreName(string sname) {
                  StoreName = sname;
               }
               void setStoreCity(string scity) {
                  StoreCity = scity;
               }
               void setStoreTown(string stown) {
                 StoreTown = stown;
               }
               void setStoreTel(string stel) {
                 StoreTel = stel;
               }
               void setStoreCustomer(string scustomer) {
                 StoreCustomer.push_back(scustomer);
               }
               int getStoreID() {
                  return StoreID;
               }
               string getStoreName() {
                 return StoreName;
               }
        
    
        string getStoreCity() {
                return StoreCity;
            }
        string getStoreTown() {
            return StoreTown;
        }
        string getStoreTel() {
            return StoreTel;
        }
        vector <string> getStoreCustomer() {
            return StoreCustomer;
        }
    
        ~Store(){}
    };
    
        Store s2[50];

    void AddStore() {
    int id, menu;
    string name, city, town, tel;
    for (int i = 0; i < 1; i++) {
        cout << "Lutfen Magaza ID Numarasini Girin: ";
        cin >> id;
        s2[i].setStoreID(id);
        cout << "Lutfen Magaza Adini Girin: ";
        cin >> name;
        s2[i].setStoreName(name);
        cout << "Lutfen Magazanin Bulundugu Ili Girin: ";
        cin >> city;
        s2[i].setStoreCity(city);
        cout << "Lutfen Magazanin Bulundugu Ilceyi Girin: ";
        cin >> town;
        s2[i].setStoreTown(town);
        cout << "Lutfen Magazanin Telefon Numarasini Girin: ";
        cin >> tel;
        s2[i].setStoreTel(tel);
        cout << "Magaza Eklendi" << endl;
        cout << "\n\n";
    }
    cout << "Menuye Donmek icin 0 " << endl;
    cin >> menu;
    if (menu == 0) {
        StoreMenu();
    }
    else {
        cout << "Tanımlanamayan Giris!!!" << endl;
        //Menu();
    }
}


int Search2(const int& y) {
    for (int j = 0; j < 100; j++) {
        if (s2[j].getStoreID() == y)
            return j;
    }
    return -1;
}
    
    void AddStoreCustomer() {
        int id, found, menu;
        string customer;
        cout << "Lutfen Musteri Eklemek Istediginiz Magazanin ID Numarasini Girin:";
        cin >> id;
        found = Search2(id);
        if (found == -1)
        {
            cout << "Magaza Bulunamadi" << endl;
        }
        else {
            cout << "Magaza Bulundu.\n" << "Lutfen Magaza Veri Tabanina Eklemek Istediginiz Musterinin Bilgilerini Girin: ";
            cin >> customer;
            s2[found].setStoreCustomer(customer);
            cout << "Girilen Musteri Bilgisi Secilen Magazaya Eklendi";
            cout << "Magaza ID: " << s2[found].getStoreID() << "\n Magaza Adi: " << s2[found].getStoreName() << "\n Magazanin Bulundugu Il: " << s2[found].getStoreCity() << "\n Magazanin Bulundugu Ilce:" << s2[found].getStoreTown() << "\n Magaza Iletisim Numarasi: " << s2[found].getStoreTel() << endl;
            cout << "Musteriler: " << endl;
            for (int i = 0; i < s2[found].getStoreCustomer().size(); i++)
            {
                cout << "\t\t" << s2[found].getStoreCustomer()[i] << endl;
            }
        }
        cout << "Tekrar Giris Yapmak icin 1                    Menuye Donmek icin 0 " << endl;
        cin >> menu;
        if (menu == 0) {
            StoreMenu();
        }
        else if (menu == 1) {
            AddStoreCustomer();
        }

发生这种情况是因为您正在使用cin>>读取输入,该输入一直读取到第一个空白符号。 如果您想阅读整行,请考虑使用std::getline()代替:

getline(cin, customer);

谢谢您的帮助。 我修改了如下代码。 但是,当我运行代码时,它直接输出而不要求我输入。 我想知道我是否以错误的方式使用getline()将其更新为getline(cin,customer)而不是cin>> customer

void AddStoreCustomer() {
    int id, found, menu;
    string customer;
    cout << "Lutfen Musteri Eklemek Istediginiz Magazanin ID Numarasini Girin:";
    cin >> id;
    found = Search2(id);
    if (found == -1)
    {
        cout << "Magaza Bulunamadi" << endl;
    }
    else {
        cout << "Magaza Bulundu.\n" << "Lutfen Magaza Veri Tabanina Eklemek Istediginiz Musterinin Bilgilerini Girin: ";
        //cin >> customer;
        getline(cin, customer);
        s2[found].setStoreCustomer(customer);
        cout << "Girilen Musteri Bilgisi Secilen Magazaya Eklendi";
        cout << "Magaza ID: " << s2[found].getStoreID() << "\n Magaza Adi: " << s2[found].getStoreName() << "\n Magazanin Bulundugu Il: " << s2[found].getStoreCity() << "\n Magazanin Bulundugu Ilce:" << s2[found].getStoreTown() << "\n Magaza Iletisim Numarasi: " << s2[found].getStoreTel() << endl;
        cout << "Musteriler: " << endl;
        for (int i = 0; i < s2[found].getStoreCustomer().size(); i++)
        {
            cout << "\t\t" << s2[found].getStoreCustomer()[i] << endl;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM