我无法从inData.txt中获取#并将值输出到outData.txt 我的inData.txt中的值是: 10.20 5.35
我的outData.txt中显示的值是: 长方形: 长度= -92559631349317830000000000000000000000000000000000000000000000.00,宽度= -92559631349317830000000000000000000000000000000000000000000000.00,面积= 856728535552162100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00,周长= -370238525397271320000000000000000000000000000000000000000000000.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;
}
答案 0 :(得分:1)
这将检查您的代码:
#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;
}