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.
Related
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'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.
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 given and assignment to read a file of data listed below:
Turn on the Bright Lights
Interpol
9.49
House of Jealous Lovers
The Rapture
1.29
Fever to Tell
Yeah Yeah Yeahs
6.99
Desperate Youth, Blood Thirsty Babes
TV on the Radio
8.91
The Fragile
Nine Inch Nails
12.49
Input this data into C++ and then display in a matrix and output total price of the cd's and how many cd's were sold. So far I can only get the first line to display properly and cannot figure out what I need to change to get the rest to display. Here is what I have written thus far. I have not started on the output code and feel as though I will not have an issue with that.
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
ifstream inputFile, processLine;
ofstream outputFile;
string title, band;
double price, total = 0;
inputFile.open("orders.txt");
while (!inputFile.eof())
{
getline(inputFile, title);
getline(inputFile, band);
inputFile >> price;
total += price;
cout << "Welcome to Megan McCracken's Online Music Store" << endl;
cout << "You have submitted the following order:" << endl;
cout << "***************************************************************************" << endl;
cout << "Title" << setw(46) << "Artist" << setw(24) << "Cost" << endl;
cout << title << setw(28) << band << setw(22) << fixed << setprecision(2) << price << endl;
cout << title << setw(30) << band<< setw(19) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << title << setw(20) << band << setw(20) << fixed << setprecision(2) << price << endl;
cout << "--------------------------------------------------------------------------" << endl;
cout << "Total Due:" << setw(75) << fixed << setprecision(2) << total << endl;
cout << "==========================================================================" << endl;
}
/* getline(inputFile, title);
cout << left << title;
getline(inputFile, band);
cout << setw(23) << band;
inputFile >> price;
cout << setw(10) << fixed << setprecision(2) << price << endl; */
system("pause");
return 0;
}
There are multiple problems with this code.
while (!inputFile.eof())
This is always a bug. See the linked article for more information.
But this is not the only problem.
getline(inputFile, band);
inputFile >> price;
Do not use both std::getline() and operator>> on the same input stream. operator>> has certain, narrowly-constrained, semantics when it comes to handling whitespace. Here, operator>> is not going to consume the trailing newline, so on the next loop iteration the first getline() is going to go off the rails.
Although there are some band-aid fixes that are often suggested, in this case, the simplest solution is to simply not use operator>>.
Use getline() a third time, to read the third line into a std::string. Then using it to construct an independent std::istringstream, and use operator>> with that. Problem solved.
After fixing that, there are some logical errors that need to be fixed. The header of the report should likely be displayed before the loop, and inside the loop the only thing that should be done is displaying a single line of the receipt, and adding up the totals, then displaying the total after the end of file was received.
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;