找不到解决方案
Can't Find a solution
我使用的是visualstudios,这个程序从一个名为inmpg的txt文件中获取数据,然后打印到一个生成的名为outmpg.txt的文件中。代码没有错误,但当控制台窗口出现时,它只会进入"从文件读取",而不会继续。"outmpg.txt"已创建,但为空。感谢您的帮助。
#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1
float amt2; // # of gallons for fillup 2
float amt3; // # of gallons for fillup 3
float amt4; // # of gallons for fillup 4
float startMiles; // Starting mileage
float endMiles; // Ending mileage
float mpg; // Computed miles per gallon
ifstream inMPG; // Holds gallon amts & mileages. Input
ofstream outMPG; // Holds miles per gall. Output
// Open the files
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
cout << "can't find inmpg.txt" << endl;
return 0;
}
outMPG.open("outmpg.txt");
if (outMPG.fail())
{
cout << "can't create/ open outmpg.txt" << endl;
return 0;
}
// Get data (priming read)
cout << "Reading from file" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
while (!inMPG.eof())
{
// Compute miles per gallon
mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
// Output results
cout << "wrote to file outmpg.txt" << endl;
outMPG << "For the gallon amounts" << endl;
outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
outMPG << "and a starting mileage of " << startMiles << endl;
outMPG << "and an ending mileage of " << endMiles << endl;
outMPG << "the mileage per gallon is " << mpg << endl;
cout << "n Reading the next set of data" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
}
return 0;
}
问题来自于您第一次开始读取inMPG文件时。如果只有一个条目,则循环永远不会执行,并且会跳过它,因为它已经到达文件的末尾。因此永远不会写入outMPG文件。
我所做的是删除while循环上方的两行,并将它们放在while循环内部的开头,还删除了循环内部的最后两行。
另一个问题是.eof()调用。至于为什么它在这种情况下实际上不起作用,我无法理解,但我使用了.peek()。因此,该语句可以更改为while(inMPG.peek()!=EOF)。
这是为我工作的代码片段
#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1
float amt2; // # of gallons for fillup 2
float amt3; // # of gallons for fillup 3
float amt4; // # of gallons for fillup 4
float startMiles; // Starting mileage
float endMiles; // Ending mileage
float mpg; // Computed miles per gallon
ifstream inMPG; // Holds gallon amts & mileages. Input
ofstream outMPG; // Holds miles per gall. Output
// Open the files
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
cout << "can't find inmpg.txt" << endl;
return 0;
}
outMPG.open("outmpg.txt");
if (outMPG.fail())
{
cout << "can't create/ open outmpg.txt" << endl;
return 0;
}
while(inMPG.peek() != EOF)
{
cout << "Reading the next set of data" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
// Compute miles per gallon
mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
// Output results
cout << "wrote to file outmpg.txt" << endl;
outMPG << "For the gallon amounts" << endl;
outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
outMPG << "and a starting mileage of " << startMiles << endl;
outMPG << "and an ending mileage of " << endMiles << endl;
outMPG << "the mileage per gallon is " << mpg << endl;
}
return 0;
}
我的输入文件是这样的;
24.0 25.0 23.0 20.0 30.0 50.0
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- 我想使用 "cout" 命令慢慢打印文本,但我真的找不到任何解决方案
- 如果找不到解决方案,我如何留下递归循环,而不会打印任何东西
- 如何在没有System32的情况下解决"java.lang.UnsatisfiedLinkError:找不到依
- std::runtime_error:解决方法:在 boost::asio 中找不到主机(权威)
- Visual Studio 找不到相对于解决方案的 NuGet 包
- 更新的程序仍然崩溃,似乎找不到解决方案?
- 找不到错误"invalid conversion from char to const char*"的解决方案
- 如何解决 /usr/bin/ld:找不到 -lconfig++ 错误
- 如何解决在使用CMAKE 3.4.1/VS2015的额外模型编译opencv3.0时找不到包含文件的问题
- 找不到 C++ 程序的解决方案
- 找不到解决方案
- c# / c++在相同的解决方案- DllImport找不到DLL
- g++:CreateProcess:没有这样的文件或目录。找不到解决方案
- Visual Studio 2010 找不到除解决方案的局部变量之外的任何内容的定义