0

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

using namespace std;

int main() {
    ifstream inputFile;
    string fileName;
    int value = 0;
    int numOfNumbers = 0;
    int oddNumbers = 0;
    int evenNumbers = 0;
    double sum = 0.0;
    double average = 0.0;
    int counter = 0;


    //Ask user for file name
    cout << "Enter file name to read: " << endl;
    cin >> fileName;

    //Open file
    inputFile.open(fileName);


    //Calculate information
    if (inputFile.is_open())
    {
      while (inputFile >> value)
      {
          numOfNumbers++;
          sum += value;
      }
    }
    else
    {
    //Display error if file doesn't open
        cout << "Error reading file." << endl;
    }
    if (numOfNumbers > 0)
        average = sum / numOfNumbers;
    else
        average = 0.0;
    if (numOfNumbers > 0)
    {
        while (inputFile >> value)
        {
            counter++;
            if (value % 2 == 0)
                evenNumbers++;
            else
                oddNumbers++;
        }
        cout << "Number of numbers = " << numOfNumbers << endl;
        cout << "Average = " << average << endl;
        cout << "Sum = " << sum << endl;
        cout << "Number of even numbers: " << evenNumbers << endl;
        cout << "Number of odd numbers: " << oddNumbers << endl;
    }
    else
        cout << "Cannot compute values." << endl;
    inputFile.close();
    return 0;
}

I cannot get this program to open my file nor read the contents to do these calculations. I have moved the file into the directory and have tried typing the name with the extension (.txt). I also removed the quotations I previously had placed around fileName in inputFile.open(fileName); Any recommendations?

CloudB
  • 1
  • 1
  • please show a [mre] with example inputs and the available files – Alan Birtles Nov 10 '20 at 17:53
  • Typically this is because of [relative paths](https://en.wikipedia.org/wiki/Path_(computing)#Absolute_and_relative_paths) and not being 100% sure where the [working directory](https://en.wikipedia.org/wiki/Working_directory) is. – user4581301 Nov 10 '20 at 17:54
  • Not sure here - but maybe you need to specify the open mode e.g. ifs.open ("test.txt", std::ifstream::in) – FreudianSlip Nov 10 '20 at 17:56
  • 2
    @FreudianSlip `ifstream` implies an input stream. – user4581301 Nov 10 '20 at 17:56
  • *and have tried typing the name with the extension (.txt)* -- Maybe you are not typing in the name properly? And if there is an issue with opening the file, none of that other code you're showing about getting the average, sum, etc. is really relevant. The code could be as simple [as this](http://coliru.stacked-crooked.com/a/04270c315e156494) – PaulMcKenzie Nov 10 '20 at 17:57
  • 2
    When prompted, type the full, complete path to the file in question, including all parts (full folder chain from the drive folder, path to the file, the file name, and the extension, *exactly*. If that doesn't open the file then it is almost guaranteed to be a permission problem. If it *does* open the file, but specifying just the filename.ext does not, then it's a working directory problem. Either way, you'll know. – WhozCraig Nov 10 '20 at 17:59
  • As noted here: https://stackoverflow.com/questions/17337602/how-to-get-error-message-when-ifstream-open-fails - use `cerr << "Error: " << strerror(errno);` to see why the file wasn't open. – Vlad Feinstein Nov 10 '20 at 18:03
  • And knowing is half the battle. The rest is toy sales. – user4581301 Nov 10 '20 at 18:04

0 Answers0