我无法从 inData.txt 中获取 # 并将值输出到 outData.txt 我的 inData.txt 中的值是: 10.20 5.35
出现在我的 outData.txt 中的值是: 长方形: 长度= -925596313493178300000000000000000000000000000000000000000000000.00,宽度= -925596313493178300000000000000000000000000000000 00000000000000.00,面积= 85672853555216210000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000.00,周长= -3702385253972713200000000000000000000000000000000000000000000000.00
这是我的代码(现在我只是在输出长度、宽度、面积和周长)
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
// Filestream Variable declaration
ifstream inFile;
ofstream outFile;
// Variable Declaration
double length, width, areaOfRectangle, perimeter, radius, areaOfCircle, beginningBalance, interestRate, pi,
circumference, endingBalance;
string firstName, lastName;
int age;
char ch;
// Opening Filestream Variables
inFile.open("inData.txt");
outFile.open("outData.txt");
// Data Manipulation
outFile << fixed << showpoint;
outFile << setprecision(2);
cout << "Processing Data..." << endl;
// Variable Association
inFile >> length >> width;
outFile <<"Rectangle:" << endl;
areaOfRectangle = length * width;
perimeter = (length * 2) + (width * 2);
outFile <<"Length= " << length << ", Width= " << width << ", Area= " << areaOfRectangle << ", Perimeter= " << perimeter << endl;
// Closing Filestream Variables
inFile.close();
outFile.close();
return 0;
}
最佳答案
这将检查您的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main ()
{
// Filestream Variable declaration
ifstream inFile;
ofstream outFile;
// Variable Declaration
double length, width, areaOfRectangle, perimeter, radius, areaOfCircle, beginningBalance, interestRate, pi,
circumference, endingBalance;
string firstName, lastName;
int age;
char ch;
// Opening Filestream Variables
inFile.open("inData.txt");
outFile.open("outData.txt");
if(inFile.fail())
{
cerr << "Error opening inData.txt" << std::endl;
return -1;
}
if(outFile.fail())
{
cerr << "Error opening outData.txt" << std::endl;
return -1;
}
// Data Manipulation
outFile << fixed << showpoint;
outFile << setprecision(2);
cout << "Processing Data..." << endl;
// Variable Association
if(!(inFile >> length >> width)
{
cerr << "Failed to read values." << std::endl;
return -1;
}
outFile <<"Rectangle:" << endl;
areaOfRectangle = length * width;
perimeter = (length * 2) + (width * 2);
outFile <<"Length= " << length << ", Width= " << width << ", Area= " << areaOfRectangle << ", Perimeter= " << perimeter << endl;
// Closing Filestream Variables
inFile.close();
outFile.close();
return 0;
}
关于c++ - outData.txt 没有输出正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12740078/