Results always returning 0 and accessing classees - c++

I am having a couple problems with my code.
First off, with the code like it is, No matter what information I put in, It always returns 0, Any suggestions on where to fix this and how? I believe it has something to do with my Class Employee. How would I go about fixing this?
Second, How do I access the information in int total()? I need to access it for the last bit of code.
Also if you notice anything else that I can do to optimize my program, I welcome your suggestions. I am learning C++ as I go and will always be a Student.
// Datamax.cpp
// Created by Kennith Adkins
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
string eName;
float eHours;
float eWage;
float ePay;
float eOvertimeHours;
float eOvertimePay;
float eTotalPay;
float eTotalBaseHours;
float eTotalSalary;
float eTotalOvertimeHours;
int Overtime ()
{
if (eHours > 40)
{
eOvertimeHours = (eHours - 40);
eOvertimePay = (eOvertimeHours * (eWage * 1.5));
ePay = ((eHours - eOvertimeHours) * eWage);
eTotalPay = ePay + eOvertimePay;
}
else
{
ePay = (eHours * eWage);
}
}
int total()
{
eTotalBaseHours = (employee1.eHours - employee1.eOvertimeHours) + (employee2.eHours - employee2.eOvertimeHours) + (employee3.eHours - employee3.eOvertimeHours);
eTotalSalary = (employee1.eTotalPay + employee2.eTotalPay + employee3.eTotalPay);
eTotalOvertimeHours = (employee1.eOvertimeHours + employee2.eOvertimeHours + employee3.eOvertimeHours);
}
} employee1, employee2, employee3;
// Start the main program here
int main()
{
// Gretting
cout << "Welcome to the Employee Pay Center\n";
// Employee1 information
cout << "Enter the employee name: ";
cin >> employee1.eName;
cout << "Enter the hours worked: ";
cin >> employee1.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee1.eWage;
cout << endl; // Adding a blank line to space the information out
// Employee2 information
cout << "Enter the employee name: ";
cin >> employee2.eName;
cout << "Enter the hours worked: ";
cin >> employee2.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee2.eWage;
cout << endl; // Adding a blank line to space the information out
// Employee3 information
cout << "Enter the employee name: ";
cin >> employee3.eName;
cout << "Enter the hours worked: ";
cin >> employee3.eHours;
cout << "Enter his or her hourly wage: ";
cin >> employee3.eWage;
cout << endl; // Adding a blank line to space the information out
// Returning the information to the Employeer
cout << "Employe Name ............ = " << employee1.eName << "\n";
cout << "Base Pay................. = " << employee1.ePay << "\n";
cout << "Hours in Overtime........ = " << employee1.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee1.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee1.eTotalPay << "\n\n";
cout << "Employe Name ............ = " << employee2.eName << "\n";
cout << "Base Pay................. = " << employee2.ePay << "\n";
cout << "Hours in Overtime........ = " << employee2.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee2.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee2.eTotalPay << "\n\n";
cout << "Employe Name ............ = " << employee3.eName << "\n";
cout << "Base Pay................. = " << employee3.ePay << "\n";
cout << "Hours in Overtime........ = " << employee3.eOvertimeHours << "\n";
cout << "Overtime Pay Amount...... = " << employee3.eOvertimePay << "\n";
cout << "Total Pay................ = " << employee3.eTotalPay << "\n\n";
cout << "*******************************************************\n";
cout << "*****************EMPLOYEE SUMMARY DATA*****************\n";
cout << "*******************************************************\n";
cout << "** Total Employee Salaries............ " << "**\n";
cout << "** Total Employee Hours............... " << "**\n";
cout << "** Total Overtime Hours............... " << "**\n";
cout << "*******************************************************\n";
cout << "*******************************************************\n";
return 0;
}
Hey Guys, Thanks for the help. I have most of it done now. It is displaying all the information. I am now just working on getting it to display the Employee Summary Data. I revamped my code to make it cleaner because I was trying every suggestion given to me as I learn best by hands on.

That's what you get for using non-initialized variables. You have set no value to your class members, you can't expect the compiler to guess what is your employee's name or total pay.
You need to use the form:
object name.member name = value

Of course, you should call the functions before outputting results that are supposed to be produced by these functions:
employee1.Overtime();
employee2.Overtime();
employee3.Overtime();

Related

Math results in zero. New to coding

I'm trying to complete an assignment but I'm having difficulty with the math expressions and variables in general. I'm trying to make a program that takes user info on groceries and then outputs a receipt. Here is my code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
//user input
string firstItem, secondItem;
float firstPrice, secondPrice;
int firstCount, secondCount;
double salesTax = 0.08675;
double firstExt = firstPrice * firstCount;
double secondExt = secondPrice * secondCount;
double subTotal = firstExt + secondExt;
double tax = subTotal * salesTax;
double total = tax + subTotal;
//user input
cout << "What is the first item you are buying?" << endl;
getline(cin, firstItem);
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
cin.ignore();
cout << "What is the second item you are buying?" << endl;
getline(cin, secondItem);
cout << "what is the price of the " << secondItem << "?" << endl;
cin >> secondPrice;
cout << "How many " << secondItem << "s?" << endl;
cin >> secondCount;
// receipt output
cout << "1st extended price: " << firstExt << endl;
cout << "2nd extended price: " << secondExt << endl;
cout << "subtotal: " << subTotal << endl;
cout << "tax: " << tax << endl;
cout << "total: " << total << endl;
return 0;
}
The program output either 0 for all or negatives.
Your calculations must go after you read in the values, not before. You're making your calculations based on uninitialized variables.
A declaration and initialisation like
double firstExt = firstPrice * firstCount;
initialises firstExt to be the product of the current values AT THAT POINT of firstPrice and firstCount.
It doesn't set up some magic so that the value of firstExt is recalculated whenever the values of firstPrice or firstCount are changed.
In your case, firstPrice and firstCount are uninitialised variables when you do this. Accessing values of uninitialised variables of type int gives undefined behaviour.
What you need to do is something like
cout << "What is the price of the " << firstItem << "?" << endl;
cin >> firstPrice;
cout << "How many " << firstItem << "s?" <<endl;
cin >> firstCount;
firstExt = firstPrice*firstCount; // do the calculation here
If the value of firstExt is not needed until this point, you can declare it here instead;
double firstExt = firstPrice*firstCount; // do the calculation here
which means any earlier use of firstExt will give a compiler diagnostic.

Calculation not being done right

I'm fairly new to c++, I have been given an assignment to do a fairly basic program that users can use to buy tickets but I am having some issues with the calculation.
This is my code so far.
#include <iostream>
using namespace std;
int main()
{
double type_ticket, num_tickets, price1, price2, price3, total_price, decision;
cout << "Welcome to the ticket kiosk.";
cout << "\n";
cout << "\n";
cout << "1. VVIP - RM 200";
cout << "\n";
cout << "2. VIP - RM 150";
cout << "\n";
cout << "3. Normal - RM 100" << endl;
cout << "\n";
do
{
cout << "Please select the category of ticket you would like to purchase: ";
cin >> type_ticket;
cout << "\n";
if (type_ticket == 1)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price1 = num_tickets * 200;
cout << "The price is: RM " << price1 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 2)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price2 = num_tickets * 150;
cout << "The price is: RM " << price2 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else if (type_ticket == 3)
{
cout << "How many would you like: ";
cin >> num_tickets;
cout << "\n";
price3 = num_tickets * 100;
cout << "The price is: RM " << price3 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin >> decision;
cout << "\n";
}
else
{
cout << "You have entered an invalid input, please try again. " << endl;
cout << "\n";
}
}
while (decision == 1);
total_price = price1 + price2 + price3;
cout << "The grand total is: RM " << total_price << endl;
cout << "\n";
cout << "Thank you for using this service today, we hope you enjoy the show." << endl;
cout << "\n";
}
The problem that I am having is when the user buys tickets from vvip and/or vip, the calculation for total_price is not being done right. When a price 3 has been entered however, the calculation works fine.
User buys vvip and/or vip = calculation not done right.
User buys normal and vvip and/or vip = calculation done right.
Any help would be very much appreciated.
FYI, this code is not yet complete, but for now, this is what I have.
You seem not to initialize priceN (where N is one of 1, 2, 3) variables before calculation of:
total_price = price1 + price2 + price3;
in case of only one type of the ticket, so the result is unpredictable because variables contain garbage.
You should start with :
double price1 = 0;
double price2 = 0;
double price3 = 0;

finding the present value using C++

assignment at school asks me to find the present value using double, and void. i was able to write my code up to a certain degree but the result is not what i was expecting.. i ended up separating the present value into different section so at the end i'd multiply the amount given with the rest.. any tips on how to make the code actually work the way its supposed to?
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double payment,year_term, interest;
double sum;
double Power;
double presentv;
double present;
cout << "Hello, how are you doing?" << endl;
cout << "Please insert a payment amount" << endl;
cin >> payment;
cout << " amount inserted: " << payment << endl;
cout << "Enter number of years" << endl;
cin >> year_term;
cout << " number of years: " << year_term << endl;
cout << "Enter interest rate" << endl;
cin >> interest;
cout << " the interest is: " << interest << "%" << endl;
Presentv = ((1 - (pow((1 + interest),year_term))))/interest;
cout << " the value: " << Presentv << endl;
presentva = payment * Presentv;
cout << " the present value is: " << presentva << endl;
}

C++ functions and loops

I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)

How do I pass data through multiple functions and call them correctly in main?

I'm trying to call userWeight into the double convert() function. How do I do this? I'm running into issues with it no cooperating in main.
#include <iostream>
#include <string>
using namespace std;
// health calc
string name()
{
cout << "Welcome ________ ... uhmmmm, what was your name again? ";
string name1;
cin >> name1;
cout << " " << endl;
cout << " Oh that's right! Your name was " << name1 << ", how could I forget that?!" << endl;
return name1;
}
int height(string name1) //(string name1) is what we are passing into this function
{
//feet and inches to inches
cout << " How tall are you, " << name1 <<"?"<< endl;
cout << " " << endl;
cout << " " << endl;
cout << " Enter feet: ";
int feet;
cin >> feet;
cout << " " << endl;
cout << " Enter inches: ";
int inches;
cin >> inches;
int inchesheight;
inchesheight = (feet * 12) + inches;
cout << " " << endl;
cout << " Your height is equal to " << inchesheight << " inches total." << endl;
if (inchesheight < 65 )
{
cout << " You are shorter than the average male." << endl;
}
else if (inchesheight > 66 && inchesheight < 72)
{
cout << " You are of average height." << endl;
}
else
{
cout << " You are taller than average." << endl;
}
}
double wieght()
{
cout << " How much do you weigh? (In pounds) " << endl;
double userWeight;
cin >> userWeight;
cout << " Ok so your weight in the Imperial System (lbs.), is " << userWeight << endl;
cout << " Would you like to know what your weight is in the Metric System? (kilograms) " << endl;
cout << " please answer as 'yes' or 'no;" << endl;
string response;
cin >> response;
if (response == "yes")
{
cout << " Alright! Let us start converting your weight! " << endl;
}
else if (response == "no")
{
cout << " Too bad! We are going to do it anyway! " << endl;
}
else
{
cout << " That was not a proper response! Way to follow directions!, as consequence, we will do it!" << endl;
}
return userWeight;
}
double convert(double userWeight)
{
cout << " Well since 1 kilogram is equal to 2.2046226218 pounds, we need to divide your weight by that repeating number." << endl;
cout << " Since that number is very long and ugly, we will use 2.2046 for the sake of clarity." << endl;
double kiloWeight = (userWeight / 2.2046);
cout << "Your weight in pounds is " << userWeight << "lbs, divided by 2.2046 gives us" << kiloWeight << "kgs! " << endl;
}
int main()
{
string name1 = name();
height(name1);
weight(userWeight);
convert();
return 0;
}
You are doing it wrong. You should read about function signature and passing arguments.
You have defined weight as a function that doesn't take any arguments
double weight() { //...}
but you are calling it with some parameter userWeight in main function
weight(userWeight);
and this parameter in addition is not defined. ( And no: you cannot call a function with argument being local argument on the stack of function being called from same scope - it is technically possible but this is not what you want).
This should be something like:
int main() {
double userWeight = weight();
double result = convert( userWeight);
// we can see here that local variable named userWeight was assigned value
// from a call to weight() and this result is now being passed to convert
// now you can use a result from calling convert
//...
return 0;
}