Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8823705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:25:32+00:00 2026-06-14T06:25:32+00:00

my program is supposed to use a .txt file as input (time in seconds

  • 0

my program is supposed to use a .txt file as input (time in seconds 1 through 10) to calculate the distance of a falling object. The text file reads as follows:

1
2
3
4
5
6
7
8
9
10

And here is my code so far.

#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;

//function prototype
double fallingDistance (int);

void main()
{
    ifstream inputFile;
    int time;
    double distance;

    //open the file
    inputFile.open("05.txt");
    inputFile >> time;

    {
        distance = fallingDistance (time);
        cout << time << "\t\t" << distance << endl;
    }
}
double fallingDistance (int time)
{
    double distance, gravity=9.8;
    distance = static_cast<double>(0.5 * gravity * pow(time,2));
    return distance;
}

And this is what my program compiles:

1 4.9
press any key to continue...

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:25:33+00:00Added an answer on June 14, 2026 at 6:25 am
    cout << time << "\t\t" << distance << endl;
    

    You first read an int from the input file. Next you initialize distance using time. Then you’re printing the value of time, two tabs, and lastly, the value of distance.

    After this line executes main returns and your program exits. Why would you expect it to print anything else?

    If you need to grab more values from the file then you need to use a loop, wrapping that entire process in a loop which reads from the file until it gets through the whole thing. Something like:

    inputFile.open("05.txt");
    int time;
    while(inputFile >> time) {     
        distance = fallingDistance (time);
        cout << time << "\t\t" << distance << endl;
    }
    

    On a side note, main is defined by the standard to have a return type of int, not void. Omitting the arguments (int argc and char *argv[]) as you have done is fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The program is supposed to take a file, say data.dat, filled with a list
My program is supposed to read a text document with a bunch of numbers
So my program is supposed to recursively go through a directory and then for
I'm working on a program that takes a redirected file as input. For example,
The program is supposed to take as input a list and return the index
I am making a simple program which suppose to accept txt file data from
So my question is on this: This program is supposed to calculate the area
I'm working on a program that is supposed to use with multiple users. I'm
I'm creating a program counter that is supposed to use only unsigned numbers. I
I want to use multithreading with Ruby and ActiveRecord. The program is supposed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.