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

Can someone tell me how to fix warning msg in my code of C ++? I...
Can someone tell me how to fix warning msg in my code of C ++? I got run-time error for this question please help me asap! Errors are: In function 'void bfs(int, int)': warning: comparison between signed and unsigned integer expressions [-Wsign-compare] for(int j = 0; j < adj[pppp].size(); j++){ ^ In function 'int main()': warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result] scanf("%d %d %d %d %d", &a, &q, &c, &N, &m); ^...
In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
I get errors in my merge method and Im not sure how to fix it to...
I get errors in my merge method and Im not sure how to fix it to make the code work public class OrderedApp1 { public static void main(String[] args) {    int maxSize = 100; // array size int searchKey = 55; OrdArray1 arr, a1, a2, a3; // reference to arrays arr = new OrdArray1(maxSize); // create the arrays a1 = new OrdArray1(maxSize); a2 = new OrdArray1(maxSize); a3 = new OrdArray1(maxSize);    //int a3[] =new int [ a1.length + a2.length];...
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...
I want to know how can I fix this program so it can work properly....can someone...
I want to know how can I fix this program so it can work properly....can someone run it and give me the fixed version but do not the entire program bear with this one please and thanks in advance!!!!! import java.util.Scanner; public class ifPractice { public static void main(String[] args) { Scanner KB = new Scanner(System.in); { double option; double option02; double stu_gradeTest01; double stu_gradeTest02; double stu_gradeTest03; double final_grade; { System.out.println("Enter #1 to see what test to grade first, #2...
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!!! ?????
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT