My Very Basic C++ Program Broke and I Have No Idea Why - c++

This is a program I am doing for programming class that that is supposed calculate people's pay depending on overtime and time and a half, but it broke and I have no idea why.
The thing will output "" "" has job 6942646 and worked 6923592 hours" or something like that.
#include <iostream>
#include <iomanip>
using namespace std;
string getName(string, string);
int getJob(int&);
int hoursWorked(int);
double jobCalc(int&, int&, double&);
void display(string, string, int, int, double);
int main()
{
int job;
int hours;
double pay;
string firstname, lastname;
getName(firstname, lastname);
getJob(job);
hoursWorked(hours);
jobCalc(job, hours, pay);
display(firstname, lastname, job, hours, pay);
return 0;
}
string getName(string firstname, string lastname)
{
cout << "Enter your name (First): ";
cin >> firstname;
cout << "Enter your name (Last): ";
cin >> lastname;
return firstname, lastname;
}
int getJob(int job&)
{
cout << "Yo What motha duckin job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
bool jobNo = false;
while (jobNo = false)
{
if (job == 10)
{
jobNo = true;
}
else if (job == 20)
{
jobNo = true;
}
else if (job == 30)
{
jobNo = true;
}
else if (job < 9 || job > 11 || job < 19 || job > 21 || job < 29 || job > 31)
{
cout << "Yo! Please re-enter a correct value. What it is, mostly, Mama! Don't make me shank ya!" << endl;
cout << "Yo What motha' duckin' job number is you? Ya' dig? (10, 20, or 30): ";
cin >> job;
}
}
return job;
}
int hoursWorked(int hours)
{
cout << "Yo! How geezery hours dahd ya work?: ";
cin >> hours;
return hours;
}
double jobCalc(int& job, int& hours, double& pay)
{
if (job == 10)
{
if (hours < 40)
{
pay = hours * 8.75;
}
else if (hours < 60)
{
pay = 8.75 * 40;
hours = hours - 40;
pay = pay + (hours * 13.125);
}
else
{
pay = 8.75 * 40;
pay = pay + (13.125 * 20);
hours = hours - 60;
pay = pay + (17.5 * hours);
}
}
else if (job == 20)
{
if (hours < 40)
{
pay = hours * 12.25;
}
else if (hours < 60)
{
pay = 12.25 * 40;
hours = hours - 40;
pay = pay + (hours * 18.375);
}
else
{
pay = 12.25 * 40;
pay = pay + (18.375 * 20);
hours = hours - 60;
pay = pay + (24.5 * hours);
}
}
else if (job == 30)
{
if (hours < 40)
{
pay = hours * 13.75;
}
else if (hours < 60)
{
pay = 13.75 * 40;
hours = hours - 40;
pay = pay + (hours * 20.625);
}
else
{
pay = 13.75 * 40;
pay = pay + (20.625 * 20);
hours = hours - 60;
pay = pay + (27.5 * hours);
}
}
return pay;
}
void display(string firstname, string lastname, int job, int hours, double pay)
{
std::cout << std::fixed;
std::cout << std::setprecision(2);
cout << firstname << " " << lastname << " has job " << job << " and worked " << hours << " hours, thus earning them $" << setprecision(2) << pay << "." << endl;
}

all of your functions are returning values. where you are storing it?. multiple return is not possible as you are using. try the concept of pointers(references)

Related

Why doesn't my program touch the functions I made?

I am having issues with running the following code, it runs but doesn't display anything
#include <iostream>
using namespace std;
class Calorie {
public:
string name;
double height;
double weight;
char gender;
string fname;
string fgroup;
int cal;
double recommendcal;
double temp;
double RecCal();
Calorie(string foodname, string foodgroup, int calories);
Calorie(string n, double h, double w, char g);
};
class Foodgroup : public Calorie {
int rec;
void Grains();
void Proteins();
void Vege();
void fruit();
void Dairy();
};
int main() {
string foodname;
string foodgroup;
int calories;
string name;
double height;
double weight;
char gender;
cout << "Please enter your name:";
cin >> name;
cout << "Please enter your height in inches:";
cin >> height;
cout << "Please enter your weight in pounds:";
cin >> weight;
cout << "Please enter your gender F/M:";
cin >> gender;
Calorie human(name, height, weight, gender);
Calorie RecCal();
cout << "Enter Food name:";
cin >> foodname;
cout << "Enter Food type:";
cin >> foodgroup;
cout << "Enter number of calories:";
cin >> calories;
Calorie food(foodname, foodgroup, calories);
if (foodgroup == "grains") {
Foodgroup Grains();
}
else if (foodgroup == "proteins" || foodgroup == "Proteins") {
Foodgroup Proteins();
}
else if (foodgroup == "fruits" || foodgroup == "Fruits") {
Foodgroup fruits();
}
else if (foodgroup == "vegetables" || foodgroup == "Vegetables") {
Foodgroup Vege();
}
else if (foodgroup == "dairy" || foodgroup == "Dairy") {
Foodgroup Dairy();
}
return 0;
}
Calorie::Calorie(string n, double h, double w, char g) {
name = n;
height = h;
weight = w;
gender = g;
}
Calorie::Calorie(string foodname, string foodgroup, int calories) {
fname = foodname;
fgroup = foodgroup;
cal = calories;
}
double Calorie::RecCal() {
if (gender =='M' || gender == 'm') {
recommendcal = 2500;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 10 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 10 * temp;
recommendcal = recommendcal + temp;
}
}
else if (gender == 'F' || gender == 'f') {
recommendcal = 2300;
if (height <= 70) {
temp = 70 - height;
temp = 25 * temp;
recommendcal = recommendcal - temp;
}
else if (height >= 70) {
temp = height - 70;
temp = 25 * temp;
recommendcal = recommendcal + temp;
}
if (weight <= 165) {
temp = 165 - weight;
temp = 5 * temp;
recommendcal = recommendcal - temp;
}
else if (weight >= 165) {
temp = weight - 165;
temp = 5 * temp;
recommendcal = recommendcal + temp;
}
}
cout << "You're recommended calorie intake per day is: " << recommendcal << " calories." << endl;
return recommendcal;
}
void Foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Proteins() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
void Foodgroup::Vege() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::fruit() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}void Foodgroup::Dairy() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
I am unsure why the this occur, if you could help me resolve the issue it would be greatly appreciated. I have no idea on how to resolve this issue, it all looks like it should work but something isn't clicking. The codes purpose is to calculate the amount of calories on takes and see if they have met their recommended calorie intake. It is also supposed to ask three times, three different foods/drinks since we don't just eat/drink one thing a day.
Replace:
void Human(Calorie name, Calorie height, Calorie weight, Calorie gender)
with:
void Human(std::string name, double height, double weight, char gender)
You have the wrong types in a function signature.
Ron, that's right, it solves this problem but there is another problem :
void foodgroup::Grains() {
rec = recommendcal * .4;
if (cal <= rec) {
cout << "You have not hit the calorie limit for this food group";
}
else {
cout << "You have hit the calorie limit for this food group ";
}
}
[Error] 'recommendcal' was not declared in this scope
There is no declaration of 'recommendcal'

Trying to add while loop to this codeblock

SOLVED 11/19/2017 # 10:20PM
This is the codeblock I'm working on, and I need to implement a while loop to run it infinitely until the user is satisfied. I'm not sure if gross_pay should be in the condition or if something else needs to be there. I keep on getting this error code. (error C4700: uninitialized local variable 'gross_pay' used)
// GrossPay.cpp : Defines the entry point for the console application.
//int temp = 0;
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main()
{
double hourly_rate;
double hours;
double gross_pay;
while ( gross_pay >= 1 ) {
printf("Please input the hourly rate of the employee: ");
cin >> hourly_rate;
printf("Please input the number of hours worked by the employee: ");
cin >> hours;
if (hours <= 40)
{
gross_pay = hours * hourly_rate;
}
else
{
gross_pay = (40 * hourly_rate) + (hours - 40) * (hourly_rate * 1.5);
}
cout << "The gross pay of this employee is $" << gross_pay << "." << endl;
system("pause");
return 0;
}
}
Solution:
// GrossPay.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
double hourly_rate;
double hours;
double gross_pay = 1;
while (gross_pay >= 1) {
printf("Please input the hourly rate of the employee: ");
cin >> hourly_rate;
printf("Please input the number of hours worked by the employee: ");
cin >> hours;
if (hours <= 40)
{
gross_pay = hours * hourly_rate;
}
else
{
gross_pay = (40 * hourly_rate) + (hours - 40) * (hourly_rate *
1.5);
}
cout << "The gross pay of this employee is $" << gross_pay << "." <<
endl;
}
return 0;
}
Every variable should be initialized before you are using for an operation otherwise the result may not be same as you expected.
Rewrite as double gross_pay = 1;
If you don't specify an initialization value then the value for double gross_pay is undefined. It will set to 0 for global variable.
The error message says everything, you have to initialize gross_pay first, otherwise its value is undefined
double gross_pay = 1;
Or you can change while loop to do-while
do {
printf("Please input the hourly rate of the employee: ");
cin >> hourly_rate;
.....
.........
} while ( gross_pay >= 1 );

C++ Why is this outputting the wrong compound interest?

I've been trying to figure this out for sometime and I think it has something to do with the values I'm using for the calculations. I'm not exactly familiar with compound interest so I'm not sure were I'm going wrong. Any help would be appreciated.
#include <iostream>
#include <cmath>
using namespace std;
double interest_credit_card(double initial_balance, double interest_rate, int payment_months);
// Calculates interest on a credit card account according to initial balance,
//interest rate, and number of payment months.
int main ()
{
double initial_balance, interest_rate;
int payment_months;
char answer;
do
{
cout << "Enter your initial balace: \n";
cin >> initial_balance;
cout << "For how many months will you be making payments?\n";
cin >> payment_months;
cout << "What is your interest rate (as a percent)?: %\n";
cin >> interest_rate;
cout << endl;
cout << "You will be paying: $ " << interest_credit_card( initial_balance, interest_rate, payment_months) << endl;
cout << "Would you like to try again? (Y/N)\n";
cin >> answer;
}while (answer == 'Y' || answer == 'y');
cout << "Good-Bye.\n";
return 0;
}
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double compound_interest, compounding, compounding2, compounding3, compounding4;
while(payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate/100.0);
compounding = (interest_rate /12);
compounding2 = compounding + 1;
compounding3 = interest_rate * (payment_months/12);
compounding4 = pow(compounding2, compounding3);
compound_interest = initial_balance * compounding4;
initial_balance = initial_balance + compound_interest;
payment_months--;
}
return initial_balance;
}
Inputs and expected outputs:
Enter your initial balance: 1000
For how many months will you be making payments?: 7
What is your interest rate (as a percent)?: 9
You will be paying: $1053.70
It looks like you were trying a bunch of things and then left them in. The first solution you tried was almost right, you just forgot "/12":
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
while (payment_months > 0)
{
initial_balance = initial_balance + (initial_balance * interest_rate / 100.0/12);
payment_months--;
}
return initial_balance;
}
With a little better style:
double interest_credit_card(double initial_balance, double interest_rate, int payment_months)
{
double total_payment = initial_balance;
double monthly_rate = interest_rate / 100.0 / 12;
for (int month = 1; month <= payment_months; ++month)
total_payment += total_payment * monthly_rate;
return total_payment;
}

C++ Output error

I have a problem with my program and I would appreciate help.
It's supposed to allow you to enter an account number, a service code, and the number of minutes the service was used. The program then calculates the bill and it varies depending on your service. When I execute the program, it doesn't allow you to enter anything.
Regular service:$10.00 plus first 50 minutes free. Charges for over 50 minutes are $0.20 per minute.
Premium service:
$25.00 plus:
a)
For calls made from 6:00 am to 6:00 pm, the first 75 minutes are free; charges for over
75 minutes are $0.10 per minute.
b)For calls made from 6:00 pm to 6:00 am, the first 100 minutes are free; charges for over 100 minutes are $0.05 per minute.
Here is the program that I've typed.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
int minutes = 0,
day_minutes = 0, //minutes used during the day
night_minutes = 0; //minutes used during the night
string service_code,
account_number;
double final_amount,
final_damount, //final amount for day minutes
final_namount = 0; //final amount for night minutes
cout << "Please enter your account number: ";
cin >> account_number;
cout << "Please enter your service code (r or R for regular service and p or P for premium service): ";
cin >> service_code;
if (service_code == "r")
{
cout << "Please enter the amount of minutes used: " << minutes << endl;
}
if (minutes <= 50)
{
final_amount = 10;
cout << "Your final amount is $: " << final_amount << endl;
}
if (minutes > 50)
{
final_amount = (minutes * 0.20) + 10;
cout << "Your final amount is $: " << final_amount << endl;
}
else if (service_code == "p")
{
cout << "Please enter the amount of minutes used during the day: " << day_minutes << endl;
cout << "Please enter the amount of minutes used during the night: " << night_minutes << endl;
}
if (day_minutes <=75)
{
final_damount = 0;
final_amount = final_damount + final_namount + 20;
}
if (day_minutes > 75)
{
final_damount = day_minutes * 0.10;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes <= 100)
{
final_namount = 0;
final_amount = final_damount + final_namount + 20;
}
if (night_minutes > 100)
{
final_namount = night_minutes * 0.05;
final_amount = final_damount + final_namount + 20;
cout << "Your final amount is: $ " << final_amount << endl;
}
else
cout << "Error, this program does not accept negative numbers.\n";
return 0;
}
Does anyone the problem to my program? Thank you.
In your code you never specify to ask for user input (read from any input streams or files), hence it doesn't ask for any input.
You will probably want to somewhere do something such as cin or cin.readline or any of several other various methods to read the user's input from stdin.
To avoid duplicating other questions I'm not putting further details here.

How do I fix my while Error?

I'm writing a program in Microsoft Visual Studio with C++ that will retrieve information from a .txt file. In that file there are negative numbers, but when I try to write a while loop that states what to do if there is a negative number, I get several errors.
Can someone please help me with this? Here is my code and I do realize there are errors but I can't figure out how to write the While loop statement to read these values which are hours worked and the hourly rate from the .txt file
Sample text file:
45.0 10.50
-35.0 7.75
50.0 12.00
45.0 -8.50
30.0 6.50
48.0 -10.25
-50.0 10.00
50.0 8.75
40.0 12.75
56.0 8.50
Code:
//*****************************
// This program is to help calculate an employee's weekly gross pay as well as
// the net pay while showing the taxes that were taken off.
// The data that will be shown will be calculated from a .txt file
// that was created and called employee.txt.
// Input: Will be the inFile known as employee.txt
// Output: Gross pay, taxable income, federal tax, state tax, and net pay
// Typed by:
// Date:
//******************************
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
float computeGross(float, float);
void computeTaxes(float, float&, float&, float&);
float computeNetPay (float&, float&, float&, float&);
const float hours = 40; // Regular 40 hour work week
const float ovTime = 1.5; // Overtime if hours go over 40
const float exemption = 200.0; // Exemption if pay goes over 200
const float fedTaxRate = 0.10; // Federal Tax Rate
const float stTaxRate = 0.03; // State Tax rate
ifstream inFile;
ofstream outFile;
int main()
{
inFile.open("employee.txt");
outFile.open("result.txt");
float hours, rate, grossPay, taxableIncome, fedTax, stTax, NetPay;
inFile >> hours >> rate;
while(inFile)
{
if {
(hours <= 0)&& (rate <= 0);
outFile << "Invalid Data";
}
else{
return 0;
}
}
grossPay = computeGross(hours, rate);
computeTaxes (grossPay, taxableIncome, fedTax, stTax);
computeNetPay (grossPay, fedTax, stTax, NetPay);
outFile << fixed << showpoint << setprecision(2);
outFile << "Hours worked = " << hours << endl
<< "Hourly rate = " << rate << endl
<< "Employee's gross pay = " << grossPay << endl
<< "Taxable Income = " << taxableIncome << endl
<< "Federal Taxes = " << fedTax << endl
<< "State Taxes = " << stTax << endl
<< "Net Pay = " << NetPay << endl;
return 0;
}
float computeGross (float h, float r) //Computes for the Gross Pay
{
if (h > hours)
return hours * r + (h - hours) * r * ovTime;
else
return h * r;
}
void computeTaxes(float g, float& taxable, float& fedTax, float& stTax) //Computes both Taxes
{
taxable = g - exemption;
if (taxable > 0.0)
{
fedTax = fedTaxRate * taxable;
stTax = stTaxRate * taxable;
}
else
{
fedTax = 0.0;
stTax = 0.0;
}
}
float computeNetPay (float& grossPay, float& fedTax, float& stTax, float& NetPay)
{
return NetPay = grossPay - fedTax - stTax;
}
In your main function you have:
while(inFile)
{
if ((hours <= 0) && (rate <= 0))
{
outFile << "Invalid Data";
}
else {
return 0;
}
}
When the else is triggered the program finishes, the main function returns. You might want a continue break or nothing here instead, that return statement ends the main function not the While loop.
To get all the data out of the file your read statement ( inFile >> hours >> rate);
will need to be in this or another loop. Say after the IF test for validity, it could be in the Else.
while(inFile)
{
if ((hours <= 0) && (rate <= 0)) {
outFile << "Invalid Data";
}
else {
// call the data functions
// save the returned values
}
//prime hours and rate for the next loop
inFile >> hours >> rate;
}
Well.. my guess is this is what your looking for:
Note that the:
if ((hours <= 0) && (rate <= 0))
is changed to:
if ((hours <= 0) || (rate <= 0))
otherwise it won't ever hit the "invalid data" with your supplied data
//*****************************
// This program is to help calculate an employee's weekly gross pay as well as
// the net pay while showing the taxes that were taken off.
// The data that will be shown will be calculated from a .txt file
// that was created and called employee.txt.
// Input: Will be the inFile known as employee.txt
// Output: Gross pay, taxable income, federal tax, state tax, and net pay
// Typed by:
// Date:
//******************************
#include <iomanip>
#include <fstream>
#include <iostream>
using namespace std;
float computeGross(float, float);
void computeTaxes(float, float&, float&, float&);
float computeNetPay (float&, float&, float&, float&);
const float hours = 40; // Regular 40 hour work week
const float ovTime = 1.5; // Overtime if hours go over 40
const float exemption = 200.0; // Exemption if pay goes over 200
const float fedTaxRate = 0.10; // Federal Tax Rate
const float stTaxRate = 0.03; // State Tax rate
int main()
{
ifstream inFile ("employee.txt");
ofstream outFile ("result.txt");
float hours, rate, grossPay, taxableIncome, fedTax, stTax, NetPay;
if (inFile.is_open())
{
while (! inFile.eof() )
{
inFile >> hours;
inFile >> rate;
if ((hours <= 0) || (rate <= 0))
{
outFile << "Invalid Data";
}
else
{
grossPay = computeGross(hours, rate);
computeTaxes (grossPay, taxableIncome, fedTax, stTax);
computeNetPay (grossPay, fedTax, stTax, NetPay);
outFile << fixed << showpoint << setprecision(2);
outFile << "Hours worked = " << hours << endl
<< "Hourly rate = " << rate << endl
<< "Employee's gross pay = " << grossPay << endl
<< "Taxable Income = " << taxableIncome << endl
<< "Federal Taxes = " << fedTax << endl
<< "State Taxes = " << stTax << endl
<< "Net Pay = " << NetPay << endl;
}
}
}
return 0;
}
The rest is the same
For a start, I think that this:
if {
(hours <= 0)&& (rate <= 0);
outFile << "Invalid Data";
}
Should be this:
if ((hours <= 0) && (rate <= 0)) {
outFile << "Invalid Data";
}
Note that to get code to format properly on StackOverflow, you should only use spaces, not tabs. I think that's whats causing your format issues.