为什么std::string变量在C++中一次只接受1个字符?
我面临的问题是,每当我尝试在基于结构的简单数据库应用程序中读取用户输入时,程序一次只接受 1 个字符。当我在函数中输入 2 个或更多字符作为输入时,会fill_the_database()
跳过这样的一个部分:
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
每个字符串中的第 -th 个字符(当时可能不存在)。
它应该看起来像这样(对于所有其他读取也是如此):
std::cin >> database[i].name;
此外,折扣阅读也存在问题。要么你应该改变discounted
在product
以bool
类型,或者存储字符串"YES"
/"NO"
变量里面。
- use of `std::array<product, quantity>` would prevent invalid use of index operator. Same range for loop.