I'm a bit new to C++ and I'm making this small program to calculate the gross total of movies tickets.
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
int adultTick, childTick;
const int aPrice = 14;
const int cPrice = 10;
float rate() {
const double RATE = .20;
return RATE;
}
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
int main() {
cout << "Box Office Earnings Calculator ....\n" << endl;
cout << "Please Enter the Name of the Movie: ";
string movie_name;
getline(cin, movie_name);
cout << endl << " \" \" " << "adult tickets sold: ";
cin >> adultTick;
cout << " \" \" " << "child tickets sold: ";
cin >> childTick;
cout << endl << setw(10) << left << "Movie Title: " << setw(20) << right << " \" " << movie_name << " \" " << endl;
cout << setw(10) << left << "Adult Tickets Sold: " << setw(20) << right << adultTick << endl;
cout << setw(10) << left << "Child Tickets Sold: " << setw(20) << right << childTick << endl;
cout << setw(10) << left << "Gross Box Office Profit: " << setw(20) << right << "$ " << grossTotal;
}
At the very end, there is where the program its suppose to display the total? I thought the Arithmetic was correct however I don't understand Why it continuously displays a zero? What could I be doing wrong?
It works if I don't create a variable for the Arithmetic "grossTotal" but I have to do further formatting with "setprecision" and "fixed" function.
The code in main doesn't change grossTotal.
The declaration
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
… creates a variable grossTotal with a specified initial value. It does not declare a relationship between the values of these variables.
At the time the initializer expression (to the right of =) is evaluated adultTick and childTick are zero, because as namespace scope variables they have been zero-initialized.
int adultTick, childTick;
The shown code declares these variables in the global scope, and these variables get zero-initialized.
double grossTotal = (aPrice * adultTick) + (cPrice * childTick);
The shown code also declares this variable in the global scope, and the calculated formula computes to 0, so this variable will be set to 0.
cout << setw(10) << left << "Gross Box Office Profit: " << setw(20) << right << "$ " << grossTotal;
And this line in main() displays the value of the grossTotal variable, which is, of course, 0.
It is true that before this line, the preceding code in main() sets adultTick and childTick. Which makes no difference whatsoever, since the value of grossTotal has been initialized, already.
You need to change your code so that main() calculates the value of grossTotal, after these other variables are set.
Related
I seem to be having a problem with a C++ coding question. It involves mathematical arithmetic and I seem to be getting all of my outputs correct except the final one. In addition to this, the decimal point format of my answers seem to be incorrect. The answers should contain two decimal places but only two out of my four decimal point answers seem to have two decimal places. When I try to use the precision() function, the answers go into scientific notation which I do not want.
Here is the question and answer:
Here is my code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float principal;
float interest_rate;
float times_compounded;
cout << "Hello, please enter a value for your principal: ";
cin >> principal;
cout << principal << endl;
cout << "Please enter a value for your interest rate: ";
cin >> interest_rate;
cout << interest_rate << "%" << endl;
cout << "Please enter the number of times the interest is compounded during the year: ";
cin >> times_compounded;
cout << times_compounded << endl << endl;
float interest = interest_rate * 10.197647;
float amount = principal * pow((1 + (interest_rate/times_compounded)), times_compounded);
cout << "Interest Rate: " << setw(19) << interest_rate << "%" << endl;
cout << "Times Compounded: " << setw(17) << times_compounded << endl;
cout << "Principal: " << setw(17) << "$ " << setw(7) << principal << endl;
cout << "Interest: " << setw(20) << "$ " << interest << endl;
cout << "Amount in Savings: " << setw(9) << "$ " << amount;
return 0;
}
Here are my three inputs:
1000, 4.25, 12
Any feedback would be appreciated, thank you for your time.
First, the last value is wrong because you're using the interest rate as a normal number in the formula although it's actually a percentage. So you'd need to divide it by 100:
float amount = principal * pow((1 + ((interest_rate / 100) /times_compounded)), times_compounded);
Now for the precision, you can use std::fixed in conjunction with std::setprecision to set the default floating point printing precision when using std::cout. We can use a macro to make it more readable, like:
#define FIXED_FLOAT(x, p) std::fixed<<std::setprecision(p)<<(x)
So, the full output section would look like:
cout << "Interest Rate: " << setw(19) << FIXED_FLOAT(interest_rate, 2) << "%" << endl;
cout << "Times Compounded: " << setw(17) << FIXED_FLOAT(times_compounded, 0) << endl;
cout << "Principal: " << setw(17) << "$ " << setw(7) << FIXED_FLOAT(principal, 2) << endl;
cout << "Interest: " << setw(20) << "$ " << FIXED_FLOAT(interest, 2) << endl;
cout << "Amount in Savings: " << setw(9) << "$ " << FIXED_FLOAT(amount, 2);
Also, that interest = interest_rate * 10.197647 seems fishy. Interest should just be the amount minus the principal.
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.
I'm 2 days news to programming and this is my first post, so I'd greatly appreciate your help and patience. :)
My current assignment is to have a user input 2 items bought from a store, including price and quantity, to generate a receipt. For some reason, I can't get any of my code to display after the first item's info gets displayed.
#include <iostream>
#include <iomanip> // For column organization
#include <string> // For item names
using namespace std;
const float TAX = 0.08675;
int main()
{
string itemOne, itemTwo;
double priceOne, priceTwo;
int countOne, countTwo;
cout << "Hello, what is the first item that you are purchasing today?" << endl;
cout << "Please enter the item below." << endl;
getline(cin, itemOne);
cout << endl << "Thank you." << endl;
cout << "Now enter the price and then the quantity of " + itemOne + "(s) purchased, separated by a space." << endl;
cin >> priceOne >> countOne;
cin.ignore();
cout << endl << "What is the second item that you are purchasing today?\n";
cout << "Please enter the item below." << endl;
getline(cin, itemTwo);
cout << endl << "Thank you." << endl;
cout << "Now enter the price and then the quantity of " + itemTwo + "(s) purchased, separated by a space." << endl;
cin >> priceTwo >> countTwo;
/* Calculations for the Receipt */
float subTotal, finalPriceOne, finalPriceTwo, salesTax, finalTotal;
finalPriceOne = countOne * priceOne;
finalPriceTwo = countTwo * priceTwo;
subTotal = finalPriceOne + finalPriceTwo;
salesTax = subTotal * TAX;
finalTotal = subTotal + salesTax;
/* Receipt */
cout << endl << "Your receipt has been calculated and is for your viewing below..." << endl << endl;
cout << "---------------------------------------------------------------\n";
cout << left << setw(15) << "Item";
cout << right << setw(15) << "Quantity";
cout << right << setw(15) << "Price";
cout << right << setw(15) << "Ext. Price";
cout << endl;
cout << "---------------------------------------------------------------\n";
cout << setprecision(2) << fixed;
cout << left << setw(15) << itemOne;
cout << right << setw(15) << countOne;
cout << right << setw(15) << priceOne;
cout << right << setw(15) << finalPriceOne;
cout << endl;
cout << left << setw(15) << itemTwo;
cout << right << setw(15) << countTwo;
cout << right << setw(15) << priceTwo;
cout << right << setw(15) << finalPriceTwo;
cout << endl;
cout << left << setw(15) << "Tax";
cout << right << setw(15) << salesTax;
cout << endl;
cout << left << setw(15) << "Total";
cout << right << setw(15) << finalTotal;
cout << endl;
return 0;
}
On my computer (Windows g++ CodeBlocks) everything is fine. The problem surely comes from your IDE. Here is your code output on my screen :
Just for information, try to use C++ functionalities as POO to dsign your objects. It will be easier for you when coding bigger applications.
I think your execution is just paused at that breakpoint on the endl for the second item. If you step past it (or just remove the breakpoint), does the whole line for the second item appear? If so, it's because of something called "line buffering", where the program doesn't actually output as soon as you tell it to, but instead it collects up things until it sees a line-ending and then it outputs the whole line.
(Breakpoints, in case it's something you did by accident, are a feature of interactive debugging systems where you can have the whole program pause before executing a given line of code. They're often set in IDEs via a right-click menu or a click in the margin next to the line of code.)
I have my program doing 90% of what I want all that is left to do is get the total by adding all my subtotals and outputting to a file. It my be something simple but I can't seem to find a way to get the total of all my subtotals added together. To be honest, even though I need to make it output into a text file I have not attempted anything yet because I was trying to figure a way to get my total. Would someone mind finding me a solution and explaining it so I get a better understanding.
//Libraries
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
ifstream theFile("input.txt");
string name;
int units;
double price, subtotal;
cout << "\n" <<string(80, '*') << endl;
cout.width(66); cout << "Inventory Report For Jane Doe International Hardware" << endl;
cout << string(80, '*') << "\n" << endl;
cout << left << setw(20) << "ITEM";
cout << right << setw(20) << "NUMBER OF UNITS";
cout << right << setw(20) << "UNIT COST ($)";
cout << right << setw(20) << "TOTAL VALUE ($)" << endl;
cout << string(80, '-') << "\n" <<endl;
cout << fixed;
cout << setprecision(2);
while (theFile >> name >> units >> price) {
subtotal = units*price;
cout << left << setw(20) << name << right << setw(15) << units << right << setw(20) << price << right << setw(20) << subtotal <<endl;
}
cout << "\n" <<string(80, '-') << endl;
cout <<left << setw(20) << "Inventory Total ($)" << right << setw(55) << "total" <<endl;
return 0;
}
My input text file
Chisel 50 9.99 Hammer 30 15.99 Nails 2000 0.99
Bolts 200 2.99 Nuts 300 1.99 Soap 55 1.89
You need to sum up all subtotals. However, each subtotal is only accessible in its iteration, afterwards, it's lost since you reassign subtotal.
Hence, declare a variable total outside of your while loop, then add the subtotal to the total in each iteration. Thus, add the following line
subtotal = units*price;
total += subtotal;
Now you can print total later on.
I have been working on this program for awhile, but it refuses to cooperate on this last little stretch. The point of the program is to sift a data file into three arrays, sort the arrays, then print them out into a table. The problem I'm having appears to be with the table. The program is divided into four functions, and when I attempt to debug, it won't show the productName array in the function.
The malfunctioning segment of code looks like this:
void printReport (string productName[], int numberinStock[], float price[], int number_of_products)
{
float totalPrice;
cout << setw(18) << " " << "Friendly Grocer Store Inventory" << setw(17) << " " << endl;
cout << setw(18) << "Inventory Item" << setw(16) << "Number in Stock" << setw(16) << "Unit Price" << setw(16) << "Total Sales" << endl;
for (int count=0; count <number_of_products-1; count++)
{
cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << setw(16) << std::setprecision(2) << price[count] << setw(16) << std::fixed << std::setprecision(2) << price[count]*numberinStock[count] << endl;
}
cout << "Total Price: " << totalPrice;
}
It will print everything else, but not the productName.
Some debugging statements outside of the for loop like cout << productName[1] will print out the proper productName but it's completely blank on the actual report.
After some debugging it seems like after printing the productName in the for loop every item after that overwrites the product name.
For example just leaving cout << setw(18) << productName[count] << setw(16) << numberinStock[count] << endl;
will produce
" 3s"
" 10h"
" 2a"
The product names there are Mangoes, Sandwich, and pizza.
I'm at a loss. Where did I mess up?
You might have screwed up passing the data into the function. If you set up test arrays in the function it should be printing correctly.
To pass arrays in C++ use the arrayname.
eg
int main ()
{
string productName[] = {"mango"};
...
printReport(productName, numofProduct);
return 0;
}