C++ Data files

Home Forums C Programming C++ Data files

This topic contains 1 reply, has 2 voices, and was last updated by  GWILouisaxwzkla 8 years, 10 months ago.

  • Author
    Posts
  • #2243

    Norbert43W
    Member

    Hi everyone, I am a student at MCC in CT and i need help getting a file named rocket1.dat to be read and then i need to determine the time at which that rocket is at its peak altitude, my HW questions read as follows ” Assume that the rocket1.dat file contains an inital line that specifies the number of actual lines of data that follow. Write a program that reads each of these dat and determines the time at which the rocket begins falling back to Earth. “
    thank you

    #include “stdafx.h”

    #include
    #include
    #include

    using namespace std;

    int main()
    {
    //Declare and initialize objects

    int number_of_items, k;
    double time, velocity, acceleration, altitude,
    previous_time, previous_altitude;
    string filename;
    ifstream rocket1;
    ofstream report;

    cout < < "Enter name of the input file ";
    cin >> filename;

    rocket1.open(filename.c_str());

    if(rocket1.fail())
    {
    cerr < < "Error opening input file " << filename << endl;
    exit (1);
    }

    report.open (“rocket1.dat”);

    rocket1 >> number_of_items;

    rocket1 >> time >> altitude;

    for (k = 1; k < number_of_items; k++)
    {
    rocket1 >> time >> altitude ;

    if (!(rocket1 >= number_of_items) || number_of_items == 0)
    {
    cout < < "no valid data in file" << filename << endl;
    exit (1);
    }

    while (altitude>=previous_altitude &&!rocket1.eof())
    {
    previous_altitude = altitude;
    previous_time= time;
    rocket1 >> time >> altitude >> velocity >> acceleration;
    }

    if (altitude < previous_altitude)
    cout < < "time at which the rocket begins to fall is between "
    < < previous_time << " seconds and " << time << "seconds n"; else
    cout < < "no decrease in altiutde detected in data file. n"; }
    rocket1.close();
    report.close();

    return 0;

    }

  • #3641

    Could you post your data file?

You must be logged in to reply to this topic.

0 Shares
Share
Tweet
Share
Pin
+1