C++ C++;:循环中的向量问题

C++ C++;:循环中的向量问题,c++,function,vector,while-loop,C++,Function,Vector,While Loop,CreateList函数必须: 1.请用户输入杂货店的名称。 2.要求用户输入此杂货店的商品列表,直到用户输入“完成”。 3.在输入项时将其添加到字符串向量中。 4.显示:“已将项目添加到杂货店名称列表。”输入每个项目后,其中“项目”是输入的项目,“杂货店名称”是在上述步骤1中输入的名称 void CreateList() { string store; string item; int count = 0; cout<<"What is the

CreateList函数必须: 1.请用户输入杂货店的名称。 2.要求用户输入此杂货店的商品列表,直到用户输入“完成”。 3.在输入项时将其添加到字符串向量中。 4.显示:“已将项目添加到杂货店名称列表。”输入每个项目后,其中“项目”是输入的项目,“杂货店名称”是在上述步骤1中输入的名称

void CreateList()
{   
    string store;
    string item;
    int count = 0;
    cout<<"What is the grocery store name"<<endl;
    cin>>store;
    vector<string> store;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
    cin>>item;
    store[count] = item; //error saying no conversion?
    count++;
    cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
    bool finished=true;
    while (finished = true)
    {
        cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
        cin>>item;

        if (item == "done")
        break;

        store[count] = item; //error saying no conversion?
        count++;
        cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
}


}
void CreateList()
{   
字符串存储;
字符串项;
整数计数=0;

你可能有很多错误。首先,正如你所看到的,你重复了一大块代码两次。这表明你应该重构你的代码。你对
do{}while()
的直觉是正确的,你肯定应该使用它

其次,您不会像在
store[count]
中那样通过
operator[]
将新项目推送到向量中。您应该使用或。向量是一个动态容器,因此一旦创建,它就不包含任何元素。尝试使用
store[0]访问第一个元素
将导致未定义的行为,很可能是分段错误

第三,正如您所看到的,您也没有真正使用
finished
变量(事实上,您是用
finished=true
分配它,而没有检查它是否为true,这将是
finished==true
)。因为您使用了
中断
,正确退出了循环。因此,您一定要删除它

第四,您命名了store name
store
,并使用了相同的名称来声明store list向量。您不应该在同一代码块中对两种不同类型使用相同的名称

最后,我看到您正在使用类似于
使用名称空间std;
的东西。虽然这对于此类练习来说是可以的,但我认为最好习惯使用
std::
作为标准库类的前缀,或者谨慎地只“包括”您真正需要的名称空间:

using std::string;
using std::cout;
// ...
这主要是因为“污染”全局名称空间的各种原因,我将不在这里介绍

按照上述指导原则,您可以获得类似的结果:

void CreateList() {  
    std::string store;
    std::string item;
    std::cout << "What is the grocery store name" << std::endl;
    std::cin >> store;

    std::vector<std::string> store_list;
    do {
        std::cout << "Enter a list of items for this grocery store one at a time. When you are done, type done." << std::endl;
        std::cin >> item;
        if (item == "done") break;
        store_list.emplace_back(item);
        std::cout << "Added " << item << "to " << store << "list" << std::endl;
    } while (true);
}
void CreateList(){
字符串存储;
std::字符串项;
std::cout商店;
std::向量存储列表;
做{
std::cout项;
如果(项目==“完成”)中断;
存储列表。放置回(项目);

std::cout您有两个变量称为store。一个是字符串,另一个是字符串向量。然后,您似乎混淆了这两个变量,将
分配给
存储[count]
,它描述的是一个字符而不是字符串,然后尝试将产品列表作为单个字符串输出

使变量名有意义应该修复您的代码:

void CreateList()
{   
    std::string storeName;
    std::cout << "What is the grocery store name" << std::endl;
    std::cin >> storeName;

    std::cout << "Enter a list of items for this grocery store one at a time. When you are done, type done." << std::endl;

    std::vector<std::string> products;
    while (true)
    {
        std::string product;
        std::cin >> product;

        if (product == "done")
            break;

        products.push_back(product);

        std::cout << "Added " << product << " to " << storeName << " list" << std::endl;
    }
}
void CreateList()
{   
std::字符串storeName;
std::coutstorename;
std::cout产品;
如果(产品=“完成”)
打破
产品。推回(产品);

std::cout首先,您有两个不同的循环来读取您的值,但这两个循环或多或少都是中断的,实际上应该是一个循环

另外,您在第二个循环中得到了一个赋值,并声明了两个名为
store
的不同变量,一个是
string
,另一个是
vector
。同一范围内不能有两个同名的不同变量

我认为你真正想做的是:

vector<string> CreateList()
{   
    string store;
    string item;
    cout<<"What is the grocery store name"<<endl;
    cin>>store;
    vector<string> store_list;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;

    while (cin>>item && item != "done") // read next item as long as input didn't fail and item isn't "done".
    {
        store_list.pushback(item); // using pushback for adding new item
        cout<<"Added "<< item <<"to "<< store << " list"<<endl;
    }

    return store_list

}
vector CreateList()
{   
字符串存储;
字符串项;

cout嗯,vector是一个动态数组,但与标准数组不同,您可以使用[]运算符访问指定索引的值,因为vector重载了[]运算符。但它返回您需要的常量数据,您不能通过[]添加或修改数据。 您应该使用“store.push_back(item)”将项添加到向量中,而不需要使用变量来保存总计数,vector将为您保存当前计数。您只需调用“store.size()”即可获取总计数。如果您要修改指定索引的数据,可以使用“store.at(index)=value;”修改它。以下是正确的代码:

void CreateList()
{   
    string storename; // use another name because you use store again below.
    string item;
    int count = 0;
    cout<<"What is the grocery store name"<<endl;
    cin>>storename;
    vector<string> store;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
    cin>>item;
    **store.push_back(item);** //error saying no conversion?
    count++;
    cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
    bool finished=true;
    while (finished == true)  // Here is not correct, use == to compare, not =. In fact , you use break to quit the loop, so you can just use while(1) or while(true) here. 
    {
        cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
        cin>>item;

        if (item == "done")
        break;

        **store.push_back(item);** //error saying no conversion?
        count++;
        cout<<"Added "<<item<<" to "<<storename<<" list"<<endl;
    }
}
void CreateList()
{   
string storename;//请使用其他名称,因为您在下面再次使用了store。
字符串项;
整数计数=0;

cout
finished=true
-这是一个赋值,并不是说你实际上在任何地方设置了它,它不会编译,是吗?在同一范围内,你有一个名为store的字符串和一个名为store的向量。错误消息应该非常清楚。你意识到你声明了两次
store
?这会不会使向量成为保存在其中的名称store?在他的例子中,变量根本没有阴影。两个
存储的
变量在同一个作用域块中声明。与您在那里展示的示例不同(其中两个
i
s在
main
代码块中,另一个在
for
循环中。@Jeffrey仍然在跟踪,但ideone的GCC选项将其升级为错误,因此我不必费心重新演示它。此外,我的演示中还有第三个“i”,即“include”和“main”之间的全局“i”。有关升级,请参阅你是说在同一个范围内有两个同名的变量是合法的?我不知道这是否合法——我只是在人们在编译器上侥幸逃脱之前解决了一些问题。
void CreateList()
{   
    string storename; // use another name because you use store again below.
    string item;
    int count = 0;
    cout<<"What is the grocery store name"<<endl;
    cin>>storename;
    vector<string> store;
    cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
    cin>>item;
    **store.push_back(item);** //error saying no conversion?
    count++;
    cout<<"Added "<<item<<"to "<<store<<"list"<<endl;
    bool finished=true;
    while (finished == true)  // Here is not correct, use == to compare, not =. In fact , you use break to quit the loop, so you can just use while(1) or while(true) here. 
    {
        cout<<"Enter a list of items for this grocery store one at a time. When you are done, type done."<<endl;
        cin>>item;

        if (item == "done")
        break;

        **store.push_back(item);** //error saying no conversion?
        count++;
        cout<<"Added "<<item<<" to "<<storename<<" list"<<endl;
    }
}