我面临的问题是,每当我尝试在基于结构的简单数据库应用程序中读取用户输入时,程序一次只接受 1 个字符。当我在函数中输入 2 个或更多字符作为输入时,会fill_the_database()跳过这样的一个部分:
std::cout << "What is the product's name? ";
std::cin >> (*database).name[i];
Run Code Online (Sandbox Code Playgroud)
当我第二次尝试粘贴 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
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
这是代码:
#include <iostream>
#include <cstdio>
struct product
{
std::string name;
std::string category;
std::string size;
std::string price;
std::string discounted;
};
const int quantity = …Run Code Online (Sandbox Code Playgroud)