mercredi 3 mars 2021

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

The problem I am facing is that whenever I try to read user input in my simple struct-based database application the programme accepts only 1 character at a time. When I give 2 characters or more as an input in the function fill_the_database() skips one section of this kind:

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

When I try to paste 2 characters or more for the second time I see such a message in the console:

/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

Why does it happen this way?

Here is the code:

#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()
{

}

Aucun commentaire:

Enregistrer un commentaire