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;
Related
So I'm writing a basic application and for some reason when I run the program a bunch of numbers pop up before my intended output. It was working fine until I added the "std::cout" lines to have the outputs only be 2 decimals. The general gist of the application is a program acts as a self-checkout register at a store and lets the user buy 2 items. And yes I know the code probably looks really bad, I'm still super new to C++.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float price1;
float number1;
float price2;
float number2;
float priceofitemplustax1;
float priceofitemplustax2;
float total;
std::cout << std::fixed;
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2;
std::cout << total;
cout << endl << "Please scan your first item." <<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number1;
cout << endl << "How much is that item?"<<endl;
cin >> price1;
priceofitemplustax1 = (number1 * price1) * 1.0875;
cout << endl << "So you want " << number1 << " of this item? Adding tax that will be " << priceofitemplustax1 << "."<<endl;
cin.get();
cout << endl << "Please scan your second item."<<endl;
cin.get();
cout << endl << "How many of that item are you buying? "<<endl;
cin >> number2;
cout << endl << "How much is that item?"<<endl;
cin >> price2;
priceofitemplustax2 = (number2 * price2) * 1.0875;
cout << endl << "So you want " << number2 << " of this item? Adding tax that will be " << priceofitemplustax2 << "."<<endl;
cin.get();
total = priceofitemplustax1 + priceofitemplustax2;
cout << endl << "So your final total for this shopping trip including tax is " << total << "."<<endl;
cin.get();
cout << endl << "Your reciept will print below."<<endl;
cin.get();
cout << setw(14) << right << "Number of Item" << setw(10) << right << "Price" << setw(20) << "Price plus tax" << endl;
cout << setw(14) << right << number1 << setw(10) << right << price1 << setw(20) << priceofitemplustax1 << endl;
cout << setw(14) << right << number2 << setw(10) << right << price2 << setw(20) << priceofitemplustax2 << endl;
cout << endl;
cout << endl;
cout << setw(8) << right << "Total is" << setw(10) << total << price2 << endl;
cin.get();
}
std::cout << std::setprecision(2);
std::cout << price1;
std::cout << price2;
std::cout << priceofitemplustax1;
std::cout << priceofitemplustax2; std::cout << total;
here you write 5 floats
The lines
std::cout << std::fixed; // sets a format
std::cout << std::setprecision(2); // sets a format
set the streams output format.
The lines
std::cout << price1; // outputs a number
std::cout << price2; // outputs a number
std::cout << priceofitemplustax1; // outputs a number
std::cout << priceofitemplustax2; // outputs a number
std::cout << total; // outputs a number
print the variables to the stream.
Just remove the variable output lines. Do not accept this answer - Credit goes to manni66
Just needed some help with my program.
I keep getting an error with my if statement.
Could you help me out please.
This is the code I have written.
#include <iostream>
#include <cstdlib>
#include <iostream>
#include <string>
char carChoice;
int random_integer = rand();
int pricePerDay;
int daysOfHire;
int totalCost;
int age;
int telephoneNumber[30];
char name[30];
char address[30];
char response [4];
using namespace std;
int main()
{
cout << "Royal Rentals" << endl;
cout << "---------------------------------------------"<<"\n" <<endl;
cout << "Current Stock: " <<endl;
cout << "A - Lamborghini Aventador" <<endl;
cout << "B - Lamborghini Huracan" <<endl;
cout << "C - Mercedes Benz AMG GT S" <<endl;
cout << "D - Audi R8 V10" <<endl;
cout << "Which car would you like to rent (A, B, C or D)" << "\n" << endl;
cin >> carChoice;
cout << "How many days would you like to hire the car for" << "\n" << endl;
cin >> daysOfHire;
cout << "How old are you?" << "\n" << endl;
cin >> age;
if (age < 21)
{
cout << "Sorry but you have to over the age of 21 to hire our super cars" << "\n" << endl;
cout << "Please close the program to hire a car suitable for your age" << "\n" << endl;
exit (0);
}
cout << "What is your name?" << "\n" << endl;
cin >> name;
cout << "Have you got a full drivers license?" "\n" << endl;
cin >> response;
if (response == 'Y' //not "Y" but only one 'Y')
{
cout << "blah"<< "\n" <<endl;
}
else if (response == 'N')
{
cout << "blah"<< "\n" << endl;
}
cout << "what is your address?" << "\n" << endl;
cin >> address;
cout << "what is your telephone number?"<< "\n" << endl;
cin >> telephoneNumber;
if (carChoice == 'a')
{
totalCost = daysOfHire * 685;
cout << "You have chosen the following car for hire: Lamborghini Aventador" << endl;
cout << "The price per day is 685.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'b')
{
totalCost = daysOfHire * 585;
cout << "You have chosen the following car for hire: Lamborghini Huracan" << endl;
cout << "The price per day is �585.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'c')
{
totalCost = daysOfHire * 485;
cout << "You have chosen the following car for hire: Mercedes Benz AMG GT S" << endl;
cout << "The price per day is �485.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else if (carChoice == 'd')
{
totalCost = daysOfHire * 445;
cout << "You have chosen the following car for hire: Audi R8 V10" << endl;
cout << "The price per day is �445.00 GBP" << endl;
cout << "The Total Cost is: " << totalCost << endl;
cout << "Invoice Number; " << random_integer << endl;
}
else
{
cout << "You have entered an invalid response" << endl;
}
}
The statement is to verify if you have a valid driving license, if you don't then the program should display a message and close. If they do then it should continue so they can enter the rest of their details.
What am I missing from it and could some correct my if statement.
Many thanks,
Irbaaz
if (response == 'Y')
Response is an array, you cannot compare an array with an integer. If you want to check that the whole string consists of one 'Y' symbol, you should check
Strlen(response) == 1 && response[0] == 'Y'
I'm a beginner who has some questions relating to my homework for school.
With my current code I'm stuck in a neverending loop which I'm assuming is because my condition is never actually met (Pop A being greater than Pop B) and I'm unsure how to move on. I'm also not sure how to properly increment/calculate the years needed for Town A to surpass Town B but this is what I've got so far.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townA;
int townB;
double growthA;
double growthB;
double finalA;
double finalB;
int years = 0;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
}
cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl;
cout << setw(3) << townA << setw(7) << townB << endl;
cout << "Year" << endl;
cout << "--------------------------------------------" << endl;
return 0;
}
You are not updating the values of townA and townB in the loop. Hence, you never get out of the loop.
Also, you need to increment years in the loop. Use:
while (townA <= townB)
{
finalA = ((growthA / 100) * (townA)) + townA;
finalB = ((growthB / 100) * (townB)) + townB;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
// Update the values of the key variables.
++years;
townA = finalA;
townB = finalB;
}
// This needs to be moved from the loop.
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
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! :)
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();