在 2010 Visual C++ Express 中,我使用 ...
ifstream inFile("inputfile.dat");
double number;
while(inFile >> number)
{
cout << number << endl;
}
...将存储在文件中的8个数字读取到程序中,并在显示器上显示出来。它会根据需要正确显示它们,但我需要将每个单独的数字存储为已指定的 double 。从上到下,
- 客户 1 的身份证明#
- 余额
- 未付款项
- 已购买
那么其他 4 个数字对于不同的客户来说是一样的。我尝试了很多不同的方法来做到这一点,但每种方法都有:
"Run-Time Check Failure #3 - The variable 'variableName' is
being used without being initialized."
几乎所有的变量都会发生这种情况。我已经搜索了任何可以帮助我解决这个问题的东西,但似乎找不到可以帮助我达到我需要的程度的东西。
最佳答案
假设您真的想将这些存储在 8 个不同的变量中,而不是某种聚合数据类型中:
std::ifstream inFile("inputfile.dat");
double number;
if(inFile >> cust1id >> cust1bal >> cust1pay >> cust1purch >>
cust2id >> cust2bal >> cust2pay >> cust2purch) {
std::cout <<
"Customer 1's Identification #: " << cust1id << "\n" <<
"Balance: " < cust1bal << "\n" <<
"Payments outstanding: " << cust1pay << "\n" <<
"Purchases that have been made: " << cust1purch <<
"Customer 2's Identification #: " << cust2id << "\n" <<
"Balance: " < cust2bal << "\n" <<
"Payments outstanding: " << cust2pay << "\n" <<
"Purchases that have been made: " << cust2purch << "\n";
}
关于c++ - 文件流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13003886/