Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Compile and run salaryIncrease.cpp. You will be prompted to enter a positive flo

ID: 3673946 • Letter: C

Question

Compile and run salaryIncrease.cpp. You will be prompted to enter a positive floatingpoint number. Do so and then discuss the output with your partner. Make sure both of you understand what the program does. In this section you will modify salaryIncrease.cpp so that most of the output is stored in a file, rather than printed to the screen. (The user prompt "Enter the original salary……" still needs to be printed to the screen so that the user knows what information to enter. But the remaining output will be stored in a file.) The features needed in order to produce output files are contained in the fstream library. Add the necessary include directive to make sure we can use files in this program. Like many languages, C++ needs to associate two names with a file. The first, the actual file name is the name of the file as it is stored on the storage unit (e.g. hard drive, thumbdrive, etc.). For instance, results.txt might be a reasonable choice for such a file name. The second, the internal file name is a symbolic name used by your program to refer to the output stream which will be stored in your file. This file name is an identifier, subject to the usual rules for naming identifiers (we discussed these rules ealier in the course, on week 2). A typical name for such a file might be outfile or fout. Decide on a name you want your output file to be called when it is stored on permanent storage (hard drive, thumb drive, etc.). Now create an output file stream so you can use it inside your program to refer to the output stream that is going to the file. Statements that inform the user what the program does or that prompt the user for input should direct output to the console. All others output statements should direct output to your file instead of the console. Decide which statements should direct output to a file and make the necessary changes. After all output has been directed to the output file, it is good programming practice to close the file. This can be done with the close() method. Run and test the program. Make modifications as necessary so that it works properly. When it works properly, the user should be prompted for inputs, but should see no other activity on the screen. All other output formerly directed to the screen should now be directed to a file. Find the output file and examine its contents. Run the program again, with different user inputs. Examine the output file’s contents. What has happened to your original output file?

Explanation / Answer

//pre-processor
#include<iostream>
#include<iomanip>
#include<string>
#include<fstream>
using namespace std ;
//the main function
int main() {
   const int INCREASE_PERCENT = 5 ;
   double salary ;
   // fstream object
   ofstream outFile;
   // opening output file
   outFile.open("result.txt");
     
   cout<<"This program calculates a salary raise"<<endl ;
   cout<<"Enter the original salary (e.g. 10500.50):";
   cin >> salary ;
   double newSalary = salary + salary * INCREASE_PERCENT/100.0;
   double difference = newSalary - salary ;
   cout<<fixed<<setprecision (2) ;
   cout<<endl ;
   //writing to console
   cout<<setw (26)<<left<<"Original salary:"<< setw(10)
           <<right<<salary <<endl;
       // writing to file
       outFile<<setw (26)<<left<<"Original salary:"<< setw(10)
           <<right<<salary <<endl;
       //writing to console
       cout<<setw (26)
           <<left<<"New salary after 5% raise:"<<setw (10)
           <<right<<newSalary<<endl;
       //Writing to file
       outFile<<setw (26)
           <<left<<"New salary after 5% raise:"<<setw (10)
           <<right<<newSalary<<endl;
       //writing to console
       cout<<setw (26)<<left<<"Difference:"<<setw (10)
           <<right<<difference<<endl;
       //writing to file
       outFile<<setw (26)<<left<<"Difference:"<<setw (10)
           <<right<<difference<<endl;
       //closing fstream object
       outFile.close();
       return 0 ;
}

/*

Output:

Original salary: 123.45
New salary after 5% raise: 129.623
Difference: 6.1725

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote