【问题标题】:use datas introduced in the same line C++使用同一行 C++ 中介绍的数据
【发布时间】:2018-10-16 06:43:24
【问题描述】:

我正在尝试获取和比较不同的数据。 我必须带上产品名称和价格 (橙汁,5) 但我的问题是我不知道如何为超过 1 种产品做到这一点。 我使用 getline 来获取数据,但我不知道他们会推出多少产品,也不知道如何停止循环。

(橙汁,5;牛奶,7;)

while (?????????) {

    getline(cin, product, ',');
    getline(cin, price, ';');
    products[num] = product;
    proces[num] = atoi(proce.c_str());

    num++;


}

【问题讨论】:

  • 如果您不知道大小甚至其他更适合您任务的容器,请使用std::vector
  • 如果你不知道用户会插入多少产品,你必须问我如何给用户。您有不同的选择:在询问每个产品之前询问数字,或同意使用意味着“我结束了,没有更多产品可插入”(例如“输入 X 结束”)或其他可能更多的东西方便使用的。在任何情况下,使用 std:vector 代替 Product 普通数组 (Product[]) 将简化您的生活
  • 欢迎来到 Stack Overflow。很抱歉人们这么快就对你的问题投了反对票。您可以通过添加 minimal reproducible example 来改进回复。

标签: c++ loops while-loop cin getline


【解决方案1】:

如果您不知道大小并退出某个单词,您可以接受无限的用户输入。这是一个示例代码。请注意,就在getline(cin, product, ',') 之后,我放置了一个if 语句。如果用户此时输入exit,,程序退出。

我也使用了向量。向量类似于数组,但它们的大小可以在运行时更改,因此您可以在其中存储无限(与内存一样多)数据。

最后一部分是显示输出。

这是解决问题的示例方法,您可以应用任何您喜欢的方法。

#include <iostream>
#include <string>
#include <vector>

std::string product;
std::string price;
std::vector<std::string> products;
std::vector<int> prices;

int main()
{
    unsigned num = 0;
    while (true)
    {
        getline(std::cin, product, ',');
        if(product == "exit")
            break;
        getline(std::cin, price, ';');

        products.push_back(product);
        prices.push_back(atoi(price.c_str()));

        num++;
    }

    for(unsigned i = 0; i < products.size(); i++)
    {
        std::cout << "Product: " << products.at(i) << "\n";
        std::cout << "Price  : " << prices.at(i) << "\n";
    }
}

我使用过的输入:

orange juice,5;milk,7;exit,

产生的输出:

Product: orange juice
Price  : 5
Product: milk
Price  : 7

【讨论】:

  • using namespace std; stackoverflow.com/questions/1452721/…
  • 他可能正在做作业,在小项目或学校使用它没有坏处。在生产中,命名空间可能会发生冲突并导致不好的结果,因此应该避免。我认为这里使用它没有问题。
  • 我不同意。家庭作业等是为了学习人们如何做正确的事情。教他们坏习惯会导致未来的坏程序员(已经有很多了)。
  • 好的,我将编辑std:: 版本的答案。挂在那里的小猫。编辑完成,做一个优秀的程序员大卫。 :)
【解决方案2】:

试试

bool check=false;
if(!getline(cin, price, ';'))check=true;
...
if(check)break;

你应该在这里使用std::vector而不是array

【讨论】:

    【解决方案3】:

    我会先查看标准输入缓冲区,看看该行是否已终止(通过 enter = \n。)

    #include <iostream>
    #include <string>
    #include <vector>
    int main()
    {
        std::string product;
        std::string price;
        std::vector<std::pair<std::string, int>> product_price;
    
        while (std::cin.peek() != '\n')
        {
            std::getline(std::cin, product, ',');
            std::getline(std::cin, price, ';');
            product_price.push_back(make_pair(product,std::stoi(price)));
        }
    
        for (auto& item : product_price)
        {
            std::cout
                << "Product: " << item.first << "\n"
                << "Price  : " << item.second << "\n";
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-10-20
      • 2018-07-10
      • 2014-03-12
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多