Question

In: Computer Science

Why my net pay is always 0, and how can I fix it ? here's the...

Why my net pay is always 0, and how can I fix it ?

here's the code

#include <iostream>

#include <fstream>

#include <iomanip>

#include <cmath>

using namespace std;

int main() {

ofstream out;

out.open("myData.txt");

string fname;

  

cout << "name?" << endl;

cin >> fname;

double salary;

cout << "salary?" << endl;

cin >> salary;

  

double fedTax = salary * 15 / 100;

double stateTax = salary* 3.5 / 100;

double SST = salary * 5.75 / 100;

double medicare = salary* 2.75 / 100;

double pension = salary * 5 / 100;

double insurance = 75.00;

double net = salary - fedTax - stateTax - SST - medicare - pension - insurance;

  

out << "Gross Salary " << salary << endl;

out << "Federal Tax " << fedTax << endl;

out << "State Tax " << stateTax << endl;

out << "Social Security " << SST << endl;

out << "Medicare Tax " << medicare << endl;

out << "Pension Plan " << pension << endl;

out << "Health Insurance " << insurance << endl;

out << "Net Pay = " << net << endl;

  

out.close();

  

cout << fixed << setprecision(2);

  

ifstream in;

string taxName;

string secondName;

double num;

in.open("myData.txt");

  

  

cout << fname << endl;

for (int i =1; i <=8; i ++) {

in >> taxName >> secondName >> num;

  

  

cout << setfill('.') << setw(10) << left << taxName

<< setfill('.') << setw(10) << left << secondName

<< setfill('.') << setw(10) << right << num << endl;

}

  

in.close();

cout << salary - fedTax - stateTax - SST - medicare - pension - insurance << endl;

  

return 0;

}

Solutions

Expert Solution

The reason you are getting net pay 0 is because you were writing the net pay into myData.txt in a wrong fashion.

The following line is used to write net pay:

out << "Net Pay = " << net << endl;

Hence, the net pay written into myData.txt would be like "Net Pay = 605.0"

Now, you are printing the data in the following fashion.

in >> taxName >> secondName >> num;

In the case of net pay, taxName would be "Net", secondName would be "Pay" and num would be "=" instead of 605.0. Since "=" is not a double, it prints 0.

The correct code snippet is given below.

CODE

///////////////////////////////////////////////////////////////

#include <iostream>

#include <fstream>

#include <iomanip>

#include <cmath>

using namespace std;

int main() {

ofstream out;

out.open("myData.txt");

string fname;

cout << "name?" << endl;

cin >> fname;

double salary;

cout << "salary?" << endl;

cin >> salary;

double fedTax = salary * 15 / 100;

double stateTax = salary* 3.5 / 100;

double SST = salary * 5.75 / 100;

double medicare = salary* 2.75 / 100;

double pension = salary * 5 / 100;

double insurance = 75.00;

double net = salary - fedTax - stateTax - SST - medicare - pension - insurance;

out << "Gross Salary " << salary << endl;

out << "Federal Tax " << fedTax << endl;

out << "State Tax " << stateTax << endl;

out << "Social Security " << SST << endl;

out << "Medicare Tax " << medicare << endl;

out << "Pension Plan " << pension << endl;

out << "Health Insurance " << insurance << endl;

out << "Net Pay " << net << endl;

out.close();

cout << fixed << setprecision(2);

ifstream in;

string taxName;

string secondName;

double num;

in.open("myData.txt");

cout << fname << endl;

for (int i =1; i <=8; i ++) {

in >> taxName >> secondName >> num;

cout << setfill('.') << setw(10) << left << taxName

<< setfill('.') << setw(10) << left << secondName

<< setfill('.') << setw(10) << right << num << endl;

}

in.close();

cout << salary - fedTax - stateTax - SST - medicare - pension - insurance << endl;

return 0;

}

OUTPUT

///////////////////////////////////////////////////////////////


Related Solutions

JAVA: How do I fix the last "if" statement in my code so that outputs the...
JAVA: How do I fix the last "if" statement in my code so that outputs the SECOND HIGHEST/MAXIMUM GPA out of the given classes? public class app { private static Object minStudent; private static Object maxStudent; public static void main(String args[ ]) { student st1 = new student("Rebecca", "Collins", 22, 3.3); student st2 = new student("Alex", "White", 19, 2.8); student st3 = new student("Jordan", "Anderson", 22, 3.1); student[] studentArray; studentArray = new student[3]; studentArray[0] = st1; studentArray[1] = st2; studentArray[2]...
How do I fix the "error: bad operand types for binary operator '*' " in my...
How do I fix the "error: bad operand types for binary operator '*' " in my code? What I am trying to do: double TotalPrice = TicketPrice * NoOfTickets;       My code: import javax.swing.*; /*provides interfaces and classes for different events by AWT components*/ import java.awt.event.*; import javax.swing.JOptionPane; //TicketReservation.java class TicketReservation { public static void main(String args[]) { /*Declare JFrame for place controls.*/ JFrame f= new JFrame("Movie Ticket Reservation");                                   /*Declare JLabels*/ JLabel...
I am getting 7 errors can someone fix and explain what I did wrong. My code...
I am getting 7 errors can someone fix and explain what I did wrong. My code is at the bottom. Welcome to the DeVry Bank Automated Teller Machine Check balance Make withdrawal Make deposit View account information View statement View bank information Exit          The result of choosing #1 will be the following:           Current balance is: $2439.45     The result of choosing #2 will be the following:           How much would you like to withdraw? $200.50      The...
How would I fix my coding to allow the user to see what they have entered...
How would I fix my coding to allow the user to see what they have entered after they submit it? Where would I plug it into my code to see it? <!Doctype html> <html> <head> <meta charset="UTF-8"> <title>Login and Registeration Form Design</title> <link rel="stylesheet" type="text/css" href="../signintrial.css"> <script> function loginHandler(){ // code for handing login event } function registerHandler() {    //checking phone number    if(phonenumber(document.getElementById('phone').value))    {    //when phone number is valid    document.getElementById('demo').innerHTML = document.getElementById('fname').value + " "...
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is...
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is my code: #importing sqlite and pandas import sqlite3 import pandas as pd #goal print("Welcome! The goal of this assigment is to create a database to find meanings and synonyms for given phrases.") #Connecting database conn = sqlite3.connect("keilavaldez.db") #Creating cursor to remove existing specified tables cur = conn.cursor() #creating tables cur.execute("CREATE TABLE Synsets([SynsetID] INTEGER,[Definition] text)") cur.execute("CREATE TABLE Phrases([SynsetID] INTEGER,[phrase] text)") conn.commit() #opening and reading table...
how can i lower my expences when i know my income is vere good but my...
how can i lower my expences when i know my income is vere good but my expences always ALMOST reaches my income but i know i cant do anything as i know i need this liabilities. what can i do or what strategy can i make in order to lower my expences!!! ?????
Please Typing this My Summary from reading Chapter I need to fix My Words and make...
Please Typing this My Summary from reading Chapter I need to fix My Words and make Clear for understanding Thanks In section 4, it discusses how to adequately deal with your time. Time the board is a significant resource that you have to cause you to have as an understudy. This Part expresses half of understudies experience difficulty dealing with their time in their first year of school. School is a major distinction from secondary school, and a great deal...
I need to make 2 possible suggestions on how Comcast can fix the problem with their...
I need to make 2 possible suggestions on how Comcast can fix the problem with their customer service, and how Comcast can possibly improve the whole business overall while briefly discussing the impacts of these suggestions. Please put in a paragraph with the heading above, I Must come up with 350 words in total.
This is the code I have. My problem is my output includes ", 0" at the...
This is the code I have. My problem is my output includes ", 0" at the end and I want to exclude that. // File: main.cpp /*---------- BEGIN - DO NOT EDIT CODE ----------*/ #include <iostream> #include <fstream> #include <sstream> #include <iomanip> using namespace std; using index_t = int; using num_count_t = int; using isConnected_t = bool; using sum_t = int; const int MAX_SIZE = 100; // Global variable to be used to count the recursive calls. int recursiveCount =...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT