do-while loops annual increase - c++

I have been working on this assignment for over 4 hours and it is due in 2 hours and I am just getting further back now. I had it actually outputting the first year correct, but all the other years weren't. I have been working on it so much I can't even get it to work now, I am just getting errors.
A health club currently charges $250.50 a year for membership. It has announced that it will increase its membership fee by 2% each year for next 7 years.
Write a program that uses a do-while loop to display the current rate, and then the projected rates for the next 7 years. Start at year=0, meaning the current year. The following should be the display.
Hint 1: Create a double variable charges and initialize it with the first year membership. Inside the loop, update charges by adding 2% to it.
Hint 2: Use header, setprecision(2), fixed and setw() options.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const string ID = "Matthew Valdez - CS1361-D10 - Assignment 25";
double membership,
years = 1,
initialMembership = 250.50,
membershipIncrease = .02;
cout << ID << endl << endl;
cout << "Year Charges" << endl;
cout << "------------" << endl;
{
cout << setprecision(2) << fixed;
cout << left << setw(5) << "0" << initialMembership << right << setw(5) << endl;
}
do
{
cout << left << setw(5) << years++ << initialMembership *= membershipIncrease << right << setw(5) << endl;
} while (years < 8);
return 0;
}
The error I am getting is expression must have integral or unscope enum type and <<: illegal left operand has type "double"

You are running into problems due to operator precedence.
Since << has higher precedence than *=, the line
cout << left << setw(5) << years++ << initialMembership *= membershipIncrease << right << setw(5) << endl;
is equivalent to
(cout << left << setw(5) << years++ << initialMembership) *= (membershipIncrease << right << setw(5) << endl);
which is far from what you intended to do.
Make your code simpler by splitting that statement into two.
initialMembership *= membershipIncrease;
cout << left << setw(5) << years++ << initialMembership << right << setw(5) << endl;
This is a personal preference but you don't need to increment year in the same line. You can use:
initialMembership *= membershipIncrease;
cout << left << setw(5) << years << initialMembership << right << setw(5) << endl;
++years;

Related

Fundamentally misunderstanding alignment and setw?

I'm asking this after googling for 2 hours now. As the title says I think I'm misunderstanding how to use the two things above. I'm attempting to create two distinct columns that show output and are in line with one another. However it seems no matter what I do they won't line up.
My code is as follows
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
const double THEATHERCUT = .80;
const double DISTRIBUTORCUT = .20;
const int CHILDCOST = 6;
const int ADULTCOST = 10;
string movieName;
int childTickets, adultTickets, childGrossRevenue, adultGrossRevenue, totalGrossRevenue, distributorRevenue, totalNetRevenue;
//User Input
cout << "What movie was viewed?" << endl;
getline(cin, movieName);
cout << "How many adult tickets were sold?" << endl;
cin >> adultTickets;
cout << "How many child tickets were sold?" << endl;
cin >> childTickets;
// Maths
childGrossRevenue = (CHILDCOST * childTickets);
adultGrossRevenue = (ADULTCOST * adultTickets);
totalGrossRevenue = (childGrossRevenue + adultGrossRevenue);
distributorRevenue = (totalGrossRevenue * .20);
totalNetRevenue = (totalGrossRevenue * .80);
cout << left << "Movie Name:" << setw(20) << right << movieName << endl;
cout << left << "Adult Tickets Sold:" << setw(20) << right << adultTickets << endl;
cout << left << "Child Tickets Sold:" << setw(20) << right << childTickets << endl;
cout << left << "Gross Box Office Revenue:" << setw(20) << right << totalGrossRevenue << endl;
cout << left << "Amount Paid to Distributor:" << setw(20) << right << distributorRevenue << endl;
cout << left << "Net Box Office Revenue:" << setw(20) << right << totalNetRevenue << endl;
system("pause");
return 0;
}
As far as my understanding goes the first cout line should do the following:
Align "Movie Name:" to the left, setw(20) set a 20 space padding between the "Movie Name:" and movieName. right then justifies movieName to the right. Is this correct? Just for clarification this is how I'd like it to look.
(I'm also well aware using system("pause") is sacrilegious before anyone mentions it.)
setw(20) set a 20 space padding between the "Movie Name:" and movieName. right then justifies movieName to the right. Is this correct?
No.
setw(20) sets the next "field" to be 20-characters wide, triggering the insertion of additional whitespace if the field is shorter (resulting in an "alignment" effect in the output of subsequent fields).
This must come before the field is inserted otherwise you have a temporal paradox.
The field you're trying to pad is the "Movie Name:" part, so move your setws one to the left.
left and right align within a field, which doesn't seem to be what you are after, so drop right.
(live demo*)
* I have killed two unused variables, fixed indentation, remove sacrilegiousness (I literally had to or this demo wouldn't work — evil!), and increased your spacing (since 20 isn't actually enough to fit column 1 in all your rows). Otherwise the changes are only as recommended above.

(C++) Rows in my columns aren't showing up after the first iteration

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.)

Why Am I getting a zero instead?

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.

How do I get my total by adding all my subtotals?

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.

C++ seems to stop running at string

I'm new to C++ and development in general. Frankly, I have no idea what is going on. I'm just trying to display a string on one line, but the program is giving me a confusing error.
I would really appreciate any help.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// This program calculates and displays to user
int main()
{
// Constants are state and county taxes.
const float STATE_TAX_RATE = 0.04,
COUNTY_TAX_RATE = 0.02;
// float variables are :
float gross_sales = 0,
net_sales = 0,
county_tax_payment = 0,
state_tax_payment = 0,
total_tax_payment = 0;
// string variable
string month;
// integer variable
int year;
// Get month, year, and sales information from user
cout << "For what month is this? (Please type the name of the month.)\nAnswer: ";
getline(cin, month);
cout << "For what year?\nAnswer: ";
cin >> year;
cout << "How much was total sales at the register?\nAnswer: ";
cin >> gross_sales;
// Calculate the net income
net_sales = (gross_sales)/(1 + STATE_TAX_RATE + COUNTY_TAX_RATE);
// Calculate total taxes paid.
total_tax_payment = (gross_sales - net_sales);
// cout << total_tax_payment; // output test
// Calculate total state taxes paid.
state_tax_payment = (total_tax_payment * (2.0/3.0));
// cout << state_tax_payment; //output test
// Calculate county taxes paid.
county_tax_payment = (total_tax_payment * (1.0/3.0));
//Display the information
cout << "Month: " << month << " " << year << endl;
cout << "--------------------" << endl;
cout << "Total collected:\t $" << fixed << setw(9) << setprecision(2) << right << gross_sales << endl;
cout << "Sales: \t\t\t\t $" << fixed << setw(9) << setprecision(2) << right << net_sales << endl;
cout << "County Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << county_tax_payment << endl;
cout << "State Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << state_tax_payment << endl;
cout << "Total Sales Tax:\t $" << fixed << setw(9) << setprecision(2) << right << total_tax_payment << endl;
return 0;
}
The output looks like this:
For what month is this? (Please type the name of the month.)
Answer: March
For what year?
Answer: 2008
How much was total sales at the register?
Answer: 26572.89
(lldb)
At "(lldb)" The program just stops... and Xcode indicates something I don't understand on "cout << "Month: " << month << " " << year << end;", telling where an issue is, then a lot of complex debugging info. The indicator is green colored.
Thanks again for any help!!!
Because state_tax_payment and total_tax_payment are not initialize state_tax_payment = net_sales / state_tax_payment; and county_tax_payment = net_sales / county_tax_payment; lines can be result in undefined behavior
Initialize the all float variables
Assign some value to state_tax_payment and total_tax_payment
Correct the type mention by ' Thomas Matthews'.
Then your program works fine . May be it exit after execution finish. so you can add something like 'getchar()' , std::cin.get() to pause the console.
The actual problem was identified by Tony D.
The debugger in Xcode had a breakpoint set to the particular line of code. I simply had to drag it out of the gutter. For those who don't know, that the green arrow on the left in of the lines of code is a breakpoint. Drag it to the bottom, out of the code, to remove it.
I'm sure I made a total newbie mistake, since I am one, but lesson learned.