1

The CreateList function must: 1. Ask the user for a grocery store name. 2. Ask the user to enter a list of items for this grocery store, until the user enters “done”. 3. Add the items to the vector of strings as they are entered. 4. Display: “Added item to grocery store name list.” after each item is entered, where “item” is the item entered and “grocery store name” is the name entered in Step 1 above.

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;
}


}

Had a couple questions on my function, not sure where the no conversion error is coming from, and could this be implemented in a do while loop? Please try to keep your answers as simple as possible, this is my first attempt in C++ while transitioning from python. Thanks for your time.

4

4 回答 4

2

You have a lot of errors in there. First of all as you can see you are repeating a big chunk of your code twice. That is a sign that you should refactor your code. Your intuition about the do {} while() is correct, you should definitely use that.

Second, you don't push new items into a vector via operator[] like you did in store[count]. You should use store.push_back(item) or store.emplace_back(item). A vector is a dynamic container, therefore once created it does not contain any element. Trying to access the first element with store[0] will cause undefined behavior and most likely a segmentation fault.

Third, as you can see you are not really using that finished variable either (in fact you were assigning it with finished = true and not checking if it was true, that would be finished == true). Because you are correctly exiting the loop with a break. Therefore you should definitely remove it.

Fourth, you named the store name store and used the same name to declare the store list vector. You should not use the same name for two different types within the same block of code.

Finally, I've seen that you are using something similar to using namespace std;. While this is ok for this kind of exercises, I think it is a good idea to get used to prefix standard library classes with std:: or, with caution, only "include" the namespaces you really need with:

using std::string;
using std::cout;
// ...

This mainly because "polluting" the global namespace is considered bad practice for all kind of reasons I'll not cover here.

Following the guidelines above you can get something similar to:

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);
}
于 2013-11-01T03:18:19.600 回答
0

你有两个变量叫做 store。一个是字符串,一个是字符串向量。然后,您似乎混淆了两者,分配itemto 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;
    }
}

C++ 继承了 C 编程的一个人工制品,它允许您使用同名的不同变量“隐藏”一个变量。

#include <iostream>

int i = 20;

int main() {
    int i = 10;
    for (int i = 0; i < 5; ++i) {
        std::cout << "loop i = " << i << std::endl;
    }
    std::cout << "i = " << i << std::endl;
}

现场演示:http: //ideone.com/eKayzN

于 2013-11-01T03:29:43.100 回答
0

首先,您有 2 个不同的循环来读取您的值,但两者或多或少都被破坏了,应该是一个循环。

此外,您在第二个循环中得到了一个赋值,并声明了 2 个不同的变量,store其中一个为 a string,另一个为 a vector<string>。在同一范围内不能有 2 个具有相同名称的不同变量。

我认为你真正想做的是这样的:

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

}

您可能还想以某种方式返回商店列表。

于 2013-11-01T03:30:11.697 回答
0

好吧,vector 是一个动态数组,但不像标准数组,您可以使用 [] 运算符访问指定索引的值,因为矢量重载了 [] 运算符。但是它返回的是你需要的const数据,你不能通过[]来添加或修改数据。您应该使用“store.push_back(item)”将项目添加到向量中,并且您不需要使用变量来保存总计数,向量将为您保存当前计数。您只需要调用“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;
    }
}
于 2013-11-01T03:34:18.550 回答