0

我无法从 inData.txt 中获取 # 并将值输出到 outData.txt 我的 inData.txt 中的值是:10.20 5.35

The values that appear in my outData.txt are: Rectangle: Length= -92559631349317830000000000000000000000000000000000000000000000.00, Width= -92559631349317830000000000000000000000000000000000000000000000.00, Area= 8567285355521621000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.00, Perimeter= -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;

    }
4

1 回答 1

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;
}
于 2012-10-05T06:04:13.307 回答