Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Hi , are someone know or find any mistake in my cording cause i cant display my data back to the output.txt file

+4 votes
asked Jul 10 by nurul hanisah (220 points)
#include <iostream>
#include <string>
#include <fstream>
//#include <conio>
using namespace std;

struct spinachleave
{
    float temp;
    int experimentseries[15];
};

spinachleave Record[16];

const int NUMBER_OF_ROWS=5;

int main()
{
    float mean[16]={0,0};
    int max[16]={0,0};
    int maximum;
    
    
    
    //b
    //write to files
    fstream results;
    results.open("results.txt",ios::out);
    if(!results)
    {
        cout<<"File not created!";
    }
    else
    {
        cout<<"file created successfully!";
        results<<"10.0,10,11,11,11,8,8,11,11,11,11,11,7,10,10,10"<<endl;
        results<<"20.0,9,9,11,11,9,9,10,10,9,9,8,9,9,9,9"<<endl;
        results<<"30.0,4,4,4,3,5,4,4,5,5,3,3,6,5,4,4"<<endl;
        results<<"40.0,5,4,4,3,5,4,4,4,5,3,3,4,5,4,4"<<endl;
        results<<"50.0,4,4,4,3,5,3,3,5,5,3,3,3,5,3,3";
        
        results.close();
    }
    
    
    ifstream infile;
    infile.open("results.txt",ios::in);
    cout<< "\nReading from the file" << endl;
    
    for(int i=0;i<NUMBER_OF_ROWS;i++)
    {
        infile>>Record[i].temp;
        infile>>Record[i].experimentseries[i];
        mean[i]=mean[i]+Record[i].experimentseries[i];
    }
    
    
    
    fstream output;
    output.open("output.txt",ios::out);
    if(!results)
    {
        cout<<"File not created!";
    }
    else
    {
        output<<"SUMMARY OF RESULTS"<<endl<<endl;
        output<<"TEMPERATURE\tMEAN"<<endl;
        
        for(int i=0;i<NUMBER_OF_ROWS;i++)
        {
            output<<Record[i].temp<<"\t"<<mean[i]<<endl;
        }
        output<<endl;
        output<<"THE MAXIMUM MEAN --->\t"<<maximum;
        
        output.close();
    }

}

1 Answer

0 votes
answered Jul 11 by Peter Minarik (34,450 points)
selected Jul 11 by nurul hanisah
 
Best answer

Normally, when you read from the file, you only skip white space, not commas. So to make your content valid to be read, do not add any commas in your results.txt file.

The mean value is calculated by taking the middle element from the sorted list. That didn't seem what you were doing.

Your code a bit refactored and having fixed the above issues is below:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>

static const char * resultsFileName = "results.txt";
static const char * outputFileName = "output.txt";
static const int NUMBER_OF_RECORDS = 5;
static const int NUMBER_OF_EXPERIMENTS = 15;

struct spinachleave
{
    float temp;
    std::vector<int> experimentseries;
};

static spinachleave Record[NUMBER_OF_RECORDS];
static int mean[NUMBER_OF_RECORDS];
static int maximumMean;

void CreateResults()
{
    std::fstream results(resultsFileName, std::ios::out);
    if(!results)
    {
        std::cout << "File not created: " << resultsFileName << std::endl;
        return;
    }

    results << "10.0 10 11 11 11 8 8 11 11 11 11 11 7 10 10 10" << std::endl;
    results << "20.0  9  9 11 11 9 9 10 10  9  9  8 9  9  9  9" << std::endl;
    results << "30.0  4  4  4  3 5 4  4  5  5  3  3 6  5  4  4" << std::endl;
    results << "40.0  5  4  4  3 5 4  4  4  5  3  3 4  5  4  4" << std::endl;
    results << "50.0  4  4  4  3 5 3  3  5  5  3  3 3  5  3  3";
    results.close();
    std::cout << resultsFileName << " successfully created" << std::endl;
}

void ReadResults()
{
    std::ifstream infile(resultsFileName, std::ios::in);
    std::cout << "Reading from file: " << resultsFileName << std::endl;
    
    maximumMean = 0;
    for (int i = 0; i < NUMBER_OF_RECORDS; i++)
    {
        infile >> Record[i].temp;
        Record[i].experimentseries.reserve(NUMBER_OF_EXPERIMENTS);
        for (int j = 0; j < NUMBER_OF_EXPERIMENTS; j++)
            infile >> Record[i].experimentseries[j];

        std::sort(Record[i].experimentseries.begin(), Record[i].experimentseries.end());
        mean[i] = Record[i].experimentseries[Record[i].experimentseries.size() / 2];
        if (mean[i] > maximumMean)
            maximumMean = mean[i];
    }
}

void CreateOutput()
{
    std::fstream output(outputFileName, std::ios::out);
    if(!output)
    {
        std::cout << "File not created: " << outputFileName << std::endl;
        return;
    }

    output << "SUMMARY OF RESULTS" << std::endl << std::endl;
    output << "TEMPERATURE\tMEAN" << std::endl;
    
    for (int i = 0; i < NUMBER_OF_RECORDS; i++)
        output << Record[i].temp << "\t\t\t" << mean[i] << std::endl;
 
    output << std::endl;
    output << "THE MAXIMUM MEAN --->\t" << maximumMean;
    output.close();
    std::cout << outputFileName << " successfully created" << std::endl;
}

int main()
{
    CreateResults();
    ReadResults();
    CreateOutput();
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...