为什么 std::string 变量在 C++ 中一次只接受 1 个字符?

Why do std::string variables accept only 1 character at a time in C++?

我面临的问题是,每当我尝试在基于结构的简单数据库应用程序中读取用户输入时,程序一次只接受 1 个字符。 当我在函数 fill_the_database() 中输入 2 个或更多字符时,会跳过此类部分:

        std::cout << "What is the product's name? "; 
        std::cin >> (*database).name[i];

当我第二次尝试粘贴 2 个或更多字符时,我在控制台中看到这样的消息:

/home/keith/builds/mingw/gcc-9.2.0-mingw32-cross-native/mingw32/libstdc++-v3/include/bits/basic_string.h:1067: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator[](std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::reference = char&; std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::size_type = unsigned int]: Assertion '__pos <= size()' failed

为什么会这样?

代码如下:

#include <iostream>
#include <cstdio>

struct product 
{
    std::string name;
    std::string category;
    std::string size;
    std::string price;
    std::string discounted;
};

const int quantity = 3;
product database[quantity];

void display_menu();

void fill_the_database();
void show_the_database();
void search_for_an_item();
void sort_the_database();
void save_the_database();

int main()
{
    std::cout << "Hello! Your database can contain as many elements as " << quantity << "! \n";

    while(true)
    {
        display_menu();
    }

    std::cout << "\n"; system("pause"); return 0;
}

void display_menu()
{
    std::cout << "Fill the database - 1. \n";
    std::cout << "Show the database - 2. \n";
    std::cout << "Look for an item in the database - 3. \n";
    std::cout << "Sort the database - 4. \n";
    std::cout << "Save the database - 5. \n";
    std::cout << "Exit the database interface - 6. \n";

    int choice = 0;

    std::cout << "Your choice: "; std::cin >> choice;

    switch (choice)
    {
        case 1:
            fill_the_database();
        break;

        case 2:
            show_the_database();
        break;

        case 3:
            search_for_an_item();
        break;

        case 4:
            sort_the_database();
        break;

        case 5:
            save_the_database();
        break;

        case 6: 
            exit(0);
        break;
    
        default:
            std::cout << "You didn't enter a valid option. \n";
        break;
    }
}

void fill_the_database()
{
    std::string if_discounted;

    for(int i = 0; i < quantity; i++)
    {
        std::cout << "What is the product's name? "; 
        std::cin >> (*database).name[i];

        std::cout << "What is the prodyct's category? ";
        std::cin >> (*database).category[i];

        std::cout << "What is the product's size(S / M / L)? ";
        std::cin >> (*database).size[i];

        std::cout << "What is the product's price? ";
        std::cin >> (*database).price[i];

        std::cout << "Is the product discounted(YES / NO)? "; 
        std::cin >> if_discounted;

        if(if_discounted == "YES")
        {
            (*database).discounted[i] = true;
        }

        else if(if_discounted == "NO")
        {
            (*database).discounted[i] = false;
        }

        else 
        {
            std::cout << "You didn't enter a valid option. \n";
        }
    }
}

void show_the_database()
{

}

void search_for_an_item()
{

}

void sort_the_database() 
{

}

void save_the_database()
{

}

你混淆了 [i] 应该在的地方。目前,您正在访问数组中的第一个 product,然后尝试访问每个字符串中的第 i 个字符(此时可能不存在)。

它应该看起来像这样(所有其他读取也类似):

std::cin >> database[i].name;

另外,打折读取有问题。您应该将 product 中的 discounted 更改为 bool 类型,或者将字符串 "YES"/"NO" 存储在该变量中。