C++不会从数据转换字符串

C++ doesn't convert string from data

本文关键字:转换 字符串 数据 C++      更新时间:2023-10-16

我想写一个应该在超市使用的小程序。一切都是虚构的,只是为了学习。然而,该工具为每一篇新文章生成一个新的数据。数据中有两行,名称和价格。该数据被命名为产品的货号。因此,用户输入一个articlenumber,该工具查找带有该数字的数据,如果找到了,它将读取这2行并启动变量。但是由于某些原因,它不能正确地转换和复制字符串。下面是加载数据的部分。

int ware::load()
{    
    string inhalt;
    cout << "please insert article number" << endl;
    cin  >> articlenumber;
    productname.open(articlenumber, ios::in);
    if (!productname.is_open())
    {
        cout << "can't find the product." << endl;
        return 1;
    }
    if (productname.is_open())
    {
        while (!productname.eof())
        {
            getline(productname, inhalt);
            strcpy(name,inhalt.c_str());
            getline(productname, inhalt); 
            price = atoi (inhalt.c_str());
            cout << inhalt << endl;
        }
        warenname.close();    
    }
    cout << endl << endl <<
        "number: " << inhalt <<
        "  preis: " << price <<
        "  name: " << name <<
        endl << endl; //this is a test and will be deleted in the final
}

希望你能帮助我!

下面是类:

    class ware{
private:
    char     articlenumber[9];
    char     name[20];
    int      price;
    fstream  warennamefstream;
    ifstream warenname;
public:
    void newarticle(); //this to make a new product.
    void scan();       //this to 'scan' a product (entering the article number ;D)
    void output();     //later to output a bill
    int  load();       //load the datas.

};

希望现在一切都好。

首先,您的代码中有一个using namespace std;。这偶尔会导致一些微妙的错误。删除它。(使用std命名空间)

int ware::load()
{    
    string inhalt;
    cout << "please insert article number" << endl;
    cin  >> articlenumber;

articlenumber类型错误。申报为std::string,而不是char[]。(什么是缓冲区溢出,如何引起缓冲区溢出?)

productname.open(articlenumber, ios::in);

没有理由让ifstream躺在那里等待使用。另外,提供ios::in也没有意义——它是默认的。只需使用ifstream构造函数的单参数形式。

if (!productname.is_open())
{
    cout << "can't find the product." << endl;
    return 1;
}

不要检查文件是否打开了。你的用户并不关心文件是否存在,他们关心的是文件是否存在,以及你是否检索到了必要的数据。

if (productname.is_open())
{
    while (!productname.eof())
    {
        getline(productname, inhalt);
        strcpy(name,inhalt.c_str());
        getline(productname, inhalt); 
        price = atoi (inhalt.c_str());
        cout << inhalt << endl;
    }
    warenname.close();    
}

这个循环是错误的。

  • 不要调用eof()。它不会像你想象的那样工作,而且会导致bug。
  • 为什么这是一个循环?文件里不是只有两行吗?
  • 调用close没有意义。当istream超出范围时,让文件关闭。
  • 为什么warenameproductname不同?
  • 不要将数据存储在char[]中。这是21世纪。使用std::string

.

    cout << endl << endl <<
        "number: " << inhalt <<
        "  preis: " << price <<
        "  name: " << name <<
        endl << endl; //this is a test and will be deleted in the final
    当你想说'n'时,千万不要用endl。每个endl操纵符都调用flush,这可能非常昂贵。(c++的iostream endl惨败是什么?)
  • 您忘记设置return

试试这个:

int ware::load()
{    
    // This declaration should be local
    std::string articlenumber;
    cout << "please insert article number" << endl;
    cin  >> articlenumber;
    // This declaration should be local
    std::ifstream productname(articlenumber.c_str());
    // These declarations can be class members:
    std::string name;
    int price;
    std::string number;
    if(getline(productname, name) &&
        productname>>price &&
        productname>>number)
    {
        cout << "nn" <<
            "number: " number <<
            "  preis: " << price <<
            "  name: " << name <<
            "nn"; //this is a test and will be deleted in the final
        return 0;
    } else {
        cout << "can't find the product." << endl;
        return 1;
    }
}