#How do I open up the file?

19 messages · Page 1 of 1 (latest)

violet pasture
#

Instructions for assignment:
Write a program that opens the file, reads all the numbers from the file, and calculates the following:

A) The number of numbers in the file
B) The sum of all the numbers in the file (a running total)
C) The average of all the numbers in the file

I have made the code but I don't know how to open up my file name Random.txt, I have tried googling but I can't seem to figure it out. How can I make it so that it doesn't error but instead opens up the file

regal lodgeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

violet pasture
#

!format

regal lodgeBOT
scenic spindle
#

!format

regal lodgeBOT
#

#include <iostream>
#include <fstream>

using namespace std;

int main() {
  ifstream inputFile;

  double number, sum = 0, average, total = 0;

  // Open the File
  inputFile.open("Random.txt");

  if (inputFile.fail()) {
    cout << "Error: The file cannot open!" << endl;
  } else {
    while (inputFile >> number) {
      sum += number;

      cout << number << endl;
    }
    average = sum / total;
    cout << "The sum of all the numbers is:" << sum << endl;
    cout << "The total about of numbers is:" << endl;
    cout << "The average of all the numbers is:" << endl;
  }

  // Close the file
  inputFile.close();
  cout << "Thank you! The file is now closed." << endl;
}
``` !format
Alex
scenic spindle
#

oh well good enough

#

are you sure Random.txt is in the same directory as the executable?

fossil holly
#

what platform are you on, and is Random.txt in the same folder as you are running the program from

violet pasture
fossil holly
#

I am not a windows dev, but yes the file should probably be either at the root of the solution, or in the build folder where the exe is

#

if you right click the project, go to properties and then under Configuration Properties / Debugging there should be a field for "Working Directory" that will tell you where it is running when you hit play

#

if you are doing this for a class they should have explained where the file should be for how they have you set it up

violet pasture
fossil holly
#

I would try putting the text file at the root of the project folder and see if that fixes it

#

and then try what @scenic spindle suggested and the same directory as the exe

violet pasture
regal lodgeBOT
#

@violet pasture Has your question been resolved? If so, type !solved :)

hazy chasm