{"id":27257,"date":"2022-12-23T08:09:39","date_gmt":"2022-12-23T02:39:39","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/"},"modified":"2022-12-23T08:09:39","modified_gmt":"2022-12-23T02:39:39","slug":"solved-payroll-program-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/","title":{"rendered":"[Solved] Payroll program in C++"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-44581510\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"44581510\" data-parentid=\"44580249\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Initialize <code>employeeCounter<\/code> and it&#8217;ll work:<\/p>\n<pre><code>int employeeCounter = 0;\n<\/code><\/pre>\n<p>Frankly, this code simply is not presentable at all. You need to at least indent it consistently. There are a lot of things that you need to look at.<\/p>\n<p>You should go through the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md#c-core-guidelines\">C++ Core Guidelines<\/a>.<\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md\">https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md<\/a><\/p>\n<p><strong>Edited &#8212; Updated Code<\/strong><\/p>\n<p>Here&#8217;s your cleaned up and functional code:<\/p>\n<pre><code>#include&lt;fstream&gt;\n#include&lt;iostream&gt;\n#include&lt;iomanip&gt;\nusing namespace std;\n\nclass employee\n{\npublic:\n    double salary, hourlyRate, taxRate, taxAmount, grossPay, netPay, otPay;\n    int hours, otHours;\n\n    int payStat;\n    int employeeID;\n    string firstName;\n    string lastName;\n\npublic:\n    void setVariables( int empID, string fName, string lName, int stat, int rate, int hrs )\n    {\n        employeeID = empID;\n        firstName = fName;\n        lastName = lName;\n        payStat = stat;\n\n        if( payStat == 1 )\n        {\n            hourlyRate = rate;\n        }\n        else\n        {\n            salary = rate;\n        }\n\n        hours = hrs;\n    }\n\npublic:\n    virtual double calculateGrossPay() = 0;\n\n    double calculateTaxAmount()\n    {\n        taxRate = .30;\n        taxAmount = grossPay*taxRate;\n        return taxAmount;\n    }\n\n    double calculateNetPay()\n    {\n        netPay = grossPay - taxAmount;\n        return netPay;\n    }\n\n    void printData()\n    {\n        cout &lt;&lt; setprecision(2) &lt;&lt; setiosflags(ios::fixed | ios::showpoint);\n        cout &lt;&lt; firstName &lt;&lt; setw(6) &lt;&lt; lastName &lt;&lt; setw(6) &lt;&lt; employeeID &lt;&lt; setw(10) &lt;&lt; hours &lt;&lt; setw(3)\n             &lt;&lt; otHours &lt;&lt; setw(8) &lt;&lt; grossPay &lt;&lt; setw(8) &lt;&lt; netPay &lt;&lt; setw(8) &lt;&lt; otPay &lt;&lt; endl;\n    }\n};\n\nclass employeeSalary : public employee\n{\npublic:\n    double calculateGrossPay()\n    {\n        double regPay = hours*hourlyRate;\n        double hourlyRate = ((salary\/52)\/40);\n        if (hours &gt; 40)\n        {\n            otHours = (hours - 40);\n            otPay = (otHours * hourlyRate);\n            grossPay = (regPay + otPay);\n        }\n        else if (hours &lt;= 40)\n        {\n            otHours = 0; otPay = 0; grossPay = regPay;\n        }\n\n        return grossPay;\n    }\n};\n\nclass employeeHourly : public employee\n{\npublic:\n    double calculateGrossPay()\n    {\n        const double regPay = (40 * hourlyRate);\n\n        if ( hours &gt; 40 )\n        {\n            otHours = (hours - 40);\n            otPay = (otHours * hourlyRate * 1.5);\n            grossPay = (regPay + otPay);\n        }\n        else\n        {\n            otHours = 0;\n            otPay = 0;\n            grossPay = regPay;\n        }\n\n        return grossPay;\n    }\n};\n\nint main()\n{\n    int employeeCounter = 0;\n    int totalEmployeeCount = 0;\n\n    string fName, lName;\n    int empID = 0, stat = 0, rate = 0, hrs = 0;\n\n    cout &lt;&lt; \"enter # of employees you want to process:  \";\n    cin &gt;&gt; totalEmployeeCount;\n\n    employee* employee[100];\n\n    while( employeeCounter &lt; totalEmployeeCount )\n    {\n        cout&lt;&lt;\"Is employee \"&lt;&lt; employeeCounter+1 &lt;&lt; \" hourly or salary? (enter 1 for hourly \/ 2 for salary):\";\n        cin&gt;&gt;stat;\n\n        if (stat == 1)\n        {\n            cout &lt;&lt; \"Instantiating and HOURLY employee object inherited from base class employee\" &lt;&lt; endl &lt;&lt; endl;\n\n            cout&lt;&lt;\"Enter employee's ID: \";\n            cin&gt;&gt;empID;\n            cout&lt;&lt;\"Enter employee's first name: \";\n            cin&gt;&gt;fName;\n            cout&lt;&lt;\"Enter employee's last name: \";\n            cin&gt;&gt;lName;\n            cout&lt;&lt;\"Enter employee's hourly wage: \";\n            cin&gt;&gt;rate;\n            cout&lt;&lt;\"Enter employee's hours for this week: \";\n            cin&gt;&gt;hrs;\n\n            employee[employeeCounter] = new employeeHourly();\n            employee[employeeCounter]-&gt;setVariables( empID, fName, lName, stat, rate, hrs );\n            employee[employeeCounter]-&gt;calculateGrossPay();\n            employee[employeeCounter]-&gt;calculateTaxAmount();\n            employee[employeeCounter]-&gt;calculateNetPay();\n            cout&lt;&lt;endl&lt;&lt;endl;\n            employeeCounter++;\n        }\n        else\n        {\n            cout&lt;&lt;\"instantialting a SALARY employee object in herited from base class employee\"&lt;&lt;endl&lt;&lt;endl;\n            cout&lt;&lt;\"Enter employee's ID: \";\n            cin&gt;&gt;empID;\n            cout&lt;&lt;\"Enter employee's first name: \";\n            cin&gt;&gt;fName;\n            cout&lt;&lt;\"Enter employee's last name: \";\n            cin&gt;&gt;lName;\n            cout&lt;&lt;\"Enter employee's hourly wage: \";\n            cin&gt;&gt;rate;\n            cout&lt;&lt;\"Enter employee's hours for this week: \";\n            cin&gt;&gt;hrs;\n            employee[employeeCounter] = new employeeHourly();\n            employee[employeeCounter]-&gt;setVariables(empID, fName, lName, stat,\n                                                    rate, hrs);\n            employee[employeeCounter]-&gt;calculateGrossPay();\n            employee[employeeCounter]-&gt;calculateTaxAmount();\n            employee[employeeCounter]-&gt;calculateNetPay();\n            cout&lt;&lt;endl&lt;&lt;endl;\n            employeeCounter++;\n        }\n    }\n\n    cout &lt;&lt; \"-----------------------------------------\\n\";\n\n    for ( int i = 0; i &lt; employeeCounter; ++i )\n    {\n        employee[ i ]-&gt;printData();\n    }\n\n    cout &lt;&lt; \"-----------------------------------------\\n\";\n\n    return 0;\n}\n<\/code><\/pre>\n<p>You had global variables to store the data and in your print function you were printing those globals. That means you&#8217;d only get the latest stored values.<\/p>\n<p>You <em>really<\/em> need to go through the fundamentals of OOP to get those things right.<\/p>\n<p>Always indent your code.<br \/>\nInitialize your variables.<br \/>\nAnd, don&#8217;t comment each line of code.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">5<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Payroll program in C++ <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Initialize employeeCounter and it&#8217;ll work: int employeeCounter = 0; Frankly, this code simply is not presentable at all. You need to at least indent it consistently. There are a lot of things that you need to look at. You should go through the C++ Core Guidelines. https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md Edited &#8212; Updated Code Here&#8217;s your cleaned &#8230; <a title=\"[Solved] Payroll program in C++\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\" aria-label=\"More on [Solved] Payroll program in C++\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-27257","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Payroll program in C++ - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Payroll program in C++ - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Initialize employeeCounter and it&#8217;ll work: int employeeCounter = 0; Frankly, this code simply is not presentable at all. You need to at least indent it consistently. There are a lot of things that you need to look at. You should go through the C++ Core Guidelines. https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md Edited &#8212; Updated Code Here&#8217;s your cleaned ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-23T02:39:39+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Payroll program in C++\",\"datePublished\":\"2022-12-23T02:39:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\"},\"wordCount\":133,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\",\"name\":\"[Solved] Payroll program in C++ - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-23T02:39:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Payroll program in C++\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Payroll program in C++ - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Payroll program in C++ - JassWeb","og_description":"[ad_1] Initialize employeeCounter and it&#8217;ll work: int employeeCounter = 0; Frankly, this code simply is not presentable at all. You need to at least indent it consistently. There are a lot of things that you need to look at. You should go through the C++ Core Guidelines. https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md Edited &#8212; Updated Code Here&#8217;s your cleaned ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-12-23T02:39:39+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Payroll program in C++","datePublished":"2022-12-23T02:39:39+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/"},"wordCount":133,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/","name":"[Solved] Payroll program in C++ - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-23T02:39:39+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-payroll-program-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Payroll program in C++"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27257","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=27257"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/27257\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=27257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=27257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=27257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}