0

This is a project to test some of c++'s functions and algorithms and I am unable to see the output from the console to the designated file.

#include <fstream> ///file stream
#include <iostream>

using namespace std;

const double INCREASE = 0.076;

int main()
{

    double oldSalary, salary, monthlySalary, retroSalary;

    //declare outdata to an output file variable
    ofstream outData;

    //open file
    outData.open("Output.txt");

    cout << "Enter current annual salary" << endl
        << "New annual salary, monthly salary, and retroactive pay will be returned: " << endl;
        cin >> oldSalary; //current sallary

        //calculate new annual salary
        salary = oldSalary * (1+INCREASE);

        //calculate new monthly salary
        monthlySalary = salary/12;

        //calculate retroactive pay

        retroSalary = (salary - oldSalary)/2;

        //print screen
        cout << "New annual salary: " << salary << endl;
        cout << "New monthly salary: " << monthlySalary << endl;
        cout << "Retroactive salary due: " << retroSalary << endl;

        //print data on file
        outData << "New annual salary: " << salary << endl;
        outData << "New monthly salary: " << monthlySalary << endl;
        outData << "Retroactive salary due: " << retroSalary << endl;

        //close file
        outData.close();

        cout <<endl<<endl<<endl;
        return 0;
}
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 1
    Zero error checking in this code. If you don't check for and handle errors, you're gonna get bugs. – user4581301 Jan 27 '22 at 02:55
  • 1
    Please add the exact result you get and the expected result. For example, does Output.txt exist, but is empty? If you delete output.txt, is it recreated? – user4581301 Jan 27 '22 at 02:56
  • 1
    You need error checking -- that said -- your code works fine... You are having a problem holding the terminal window open most likely. Add `cin.get();` right before the `return` and try again. (you will have to press **Enter** an extra time for the program to complete) I ran your code. The same output to the terminal is also present in `Output.txt` (minus the 3 extra newlines) – David C. Rankin Jan 27 '22 at 02:58

1 Answers1

0

By any chance, are you using an IDE like CLion? If so then the issue most likely has nothing to do with your code, rather it has to do with how IDE's like CLion manage their working directories.

In an IDE like CLion, you will find your output files inside the working directory (this can be changed as well in order to avoid future issues with the location of your output files).

Open your cmake-build-debug directory, and your output file should be there. OUTPUT FILE

For more information about changing the working directory, please see: How do I change the working directory for my program

Mark F.
  • 13
  • 4