【问题标题】:String Type Vector C++字符串类型向量 C++
【发布时间】:2022-01-16 16:33:14
【问题描述】:

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

    #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();
        }

【问题讨论】:

    标签: c++ arrays class vector output


    【解决方案1】:

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

    getline(cin, customer);
    

    【讨论】:

    • 更准确地说,“发生这种情况是因为您使用 operator>> 来读取输入”。 std::cin 本身并不关心输入是什么。像operator&gt;&gt;std::getline() 这样的东西为std::cin(或任何std::istream)提供给它们的数据提供了意义和语义。
    • 小注:不仅有\s还有换行符(\n)
    • 感谢良好的编辑
    • 感谢您的帮助。我修改了代码。但是,当我运行代码时,它直接输出而不要求我输入。我想知道我是否以错误的方式使用getline() 将其更新为getline(cin,customer) 而不是cin&gt;&gt; customer
    猜你喜欢
    • 2019-08-09
    • 2012-02-15
    • 1970-01-01
    • 2011-06-28
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 2011-03-01
    相关资源
    最近更新 更多