3

私は何日もこの宿題に取り組んできましたが、何が間違っているのか正確にはわかりません。

簡単に言うと、input.dat というファイルから学生の名前と 5 つの成績を読み取り、そのデータを書き込んで、output.dat という別のファイルに整理する、単純で基本的なプログラムを C++ で作成しています。

シンプルですね。ただし、出力ファイルを確認すると、単純な整数ではなく、異常に指数関数的に大きな数値が繰り返し表示されます。

私を助けてください。そして、私に気楽に行ってください。私は 18 歳で、これが初めてのプログラミング クラスです。

これが私のコードです:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;
    in_stream.open("input.dat");
    out_stream.open("output.dat");

    double test1, test2, test3, test4, test5;
    string name1, name2;

    cout.precision(2);
    cout.setf(ios::showpoint);

    in_stream >> name1 >> name2 >> test1 >> test2 >> test3 >> test4 >> test5;

    out_stream << "Student Name: " << name1 << name2 <<endl
        << "Test Scores: " << test1 << test2 << test3 << test4 << test5 <<endl
        << "Average Test Score: " << ((test1 + test2 + test3 + test4 + test5)/5);

    in_stream.close( );
    out_stream.close( );

    system("pause");
    return 0;
}
4

1 に答える 1

0

コメントを要約すると、ソリューションは次のようになります。

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    ifstream in_stream;
    ofstream out_stream;
    in_stream.open("input.dat");
    out_stream.open("output.dat");

    double test1, test2, test3, test4, test5;
    string name1, name2;

    cout.precision(2);
    cout.setf(ios::showpoint);

    if(in_stream >> name1 >> name2 >> test1 >> test2 >> test3 >> test4 >> test5)
    {
        out_stream << "Student Name: " << name1 << name2 <<endl
            << "Test Scores: " << test1 << " " << test2 << " " << test3 << " " << test4 << " " << test5 << endl
            << "Average Test Score: " << ((test1 + test2 + test3 + test4 + test5)/5);
    }
    else
    {
        out_stream << "Failed to red input file" << endl;
    }

    in_stream.close( );
    out_stream.close( );

    system("pause");
    return 0;
}
于 2012-11-06T21:44:44.617 に答える