C++不会从数据转换字符串
C++ doesn't convert string from data
我想写一个应该在超市使用的小程序。一切都是虚构的,只是为了学习。然而,该工具为每一篇新文章生成一个新的数据。数据中有两行,名称和价格。该数据被命名为产品的货号。因此,用户输入一个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
超出范围时,让文件关闭。 - 为什么
warename
和productname
不同? - 不要将数据存储在
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
- 当你想说
- 您忘记设置
return
值
'n'
时,千万不要用endl
。每个endl
操纵符都调用flush
,这可能非常昂贵。(c++的iostream endl惨败是什么?)试试这个:
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;
}
}
相关文章:
- wcstombs_s(),转换字符串的长度
- 使用 CStringW/CStringA 和 CT2W/CT2A 转换字符串有什么区别?
- 无法转换字符串插入函数的参数
- 转换字符串向量:基于先前的值跳过元素
- 从中间器转换字符串不起作用,出了什么问题?
- 失败的证明转换字符串到枚举类
- 如何使用正则表达式和提升转换迭代器标记和转换 c 字符串
- 使用java jni时转换字符串类型
- 如何避免在转换字符串令牌流时重复istringstream构造
- 阿托伊未正确转换字符串
- C++,转换字符串,使连续下划线序列变为单个下划线
- 使用长算术转换字符串
- 转换字符串十进制数时的精度
- 将词法转换字符串提升为双精度
- 使用marshal_as函数转换字符串的性能
- C++不会从数据转换字符串
- 无论如何,在c++ /Qt中转换字符串到结构中的字段
- C++ 隐式类型转换字符串 -> int?
- 转换字符串到浮点数,c++实现
- 用于转换字符串大小写的函数