I am trying to use setw to clean up the output of my program. I want the empty spaces in between "total number of spools to be ordered" and the output.
EDIT this is what im going for:
and this is what I get
here is what I have so far:
UPDATED CODE
/********************************************/
// Name: results /
// Description: Print results /
// Parameters: N/A /
// Reture Value: N/A /
/********************************************/
void results(int spoolnumber, int subtotalspool, float shippingcost, float totalcost)
{
cout << left << setw (45) << "Total number of spools to be ordered is: " << right << spoolnumber << endl << endl;
cout << left << setw (45) << "The subtotal for the spools is:" << right << "$" << subtotalspool << endl << endl;
cout << "The shipping cost is: $" << shippingcost << endl << endl;
cout << "The total cost is: $" << totalcost << endl << endl;
return;
}
You can also do
cout << left << setw (45) << "Total number of spools to be ordered is: " << spoolnumber << endl << endl;
to choose which side the padding goes. The default is left.
EDIT: using stringstream
stringstream ss;
ss << "$" << spoolnumber
I think you can fix the right end by adding another setw. So:
cout << left << setw (45) << "Total number of spools to be ordered is: " << right << setw(5) << ss.str() << endl << endl;
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.
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
I am having trouble keeping the ":" and the "$" aligned with whichever input the user implements through the cin command which is "numTShirts". It stays aligned if implemented 10 and under but discount goes out of place with any other input.
![implemented with 10] http://prntscr.com/gzms3m
![implemented with other] http://prntscr.com/gzmsjx
cout << "\n" << endl;
cout << fixed;
cout << "Thank you for your purchase.\n" << endl;
cout << "You bought " << numTShirts << " T-shirts\n" << endl;
cout << "Subtotal Total" << setw(5) << ": $ " << right << setprecision(2) << subTotal << "\n" << endl;
cout << setprecision(0) << "Discount(" << discountPCT << "%)" << setw(7) << ": $ " << right << showpoint << setprecision(2) << discount << "\n" << endl;
cout << setfill('-') << setw(35) << "-\n" << endl;
cout << setfill(' ');
cout << "Total" << setw(14) << ": $ " << right << showpoint << setprecision(2) << totalPrice << endl;
This is happening because the amount of discount is variable and according to it the space increases.
As you show in the example when the discount is of one digit the aligning is the desired one, otherwise it is different.
As the discoutn can be maximum of three digits (100%) then I'd suggest you two make three formatting of the output.
one for 1 digit discount, 2 digit and 3 digit.
by using a conditional statement you can show the respective in the output.
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.)
finishing up a long project and the final step is to make sure my data lines up in the proper column. easy. Only I am having trouble with this and have been at it for longer than i wish to admit watching many videos and can't really grasp what the heck to do So here is a little snippet of the code that I'm having trouble with:
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
cout << "Student Grade Summary\n";
cout << "---------------------\n\n";
cout << "BIOLOGY CLASS\n\n";
cout << "Student Final Final Letter\n";
cout << "Name Exam Avg Grade\n";
cout << "----------------------------------------------------------------\n";
cout << "bill"<< " " << "joeyyyyyyy" << right << setw(23)
<< "89" << " " << "21.00" << " "
<< "43" << "\n";
cout << "Bob James" << right << setw(23)
<< "89" << " " << "21.00" << " "
<< "43" << "\n";
}
which works for the first entry but the bob james entry has the numbers all askew. I thought setw was supposed to allow you to ignore that? What am i missing?
Thanks
It doesn't work as you think. std::setw sets the width of the field only for the next insertion (i.e., it is not "sticky").
Try something like this instead:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
cout << "Student Grade Summary\n";
cout << "---------------------\n\n";
cout << "BIOLOGY CLASS\n\n";
cout << left << setw(42) << "Student" // left is a sticky manipulator
<< setw(8) << "Final" << setw(6) << "Final"
<< "Letter" << "\n";
cout << setw(42) << "Name"
<< setw(8) << "Exam" << setw(6) << "Avg"
<< "Grade" << "\n";
cout << setw(62) << setfill('-') << "";
cout << setfill(' ') << "\n";
cout << setw(42) << "bill joeyyyyyyy"
<< setw(8) << "89" << setw(6) << "21.00"
<< "43" << "\n";
cout << setw(42) << "Bob James"
<< setw(8) << "89" << setw(6) << "21.00"
<< "43" << "\n";
}
Also related: What's the deal with setw()?
The manipulators << right << setw(23) are telling the ostream that you want
the string "89" set in the right-hand edge of a 23-character-wide field.
There is nothing to tell the ostream where you want that field to start,
however, except for the width of the strings that are output since the
last newline.
And << "bill"<< " " << "joeyyyyyyy" writes a lot more characters to the output
than << "Bob James" does, so the 23-character-wide field on the second line
starts quite a bit to the left of the same field on the first line.
Stream manipulators affect the next input/output value being streamed, and then some manipulators (including setw()) reset afterwards. So you need to set the width and alignment BEFORE you output a text string, not afterwards.
Try something more like this:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
void outputStudent(const string &firstName, const string &lastName,
int finalExam, float finalAvg, int letterGrade)
{
cout << setw(40) << left << (firstName + " " + lastName) << " "
<< setw(6) << right << finalExam << " "
<< setw(6) << right << fixed << setprecision(2) << finalAvg << " "
<< setw(7) << right << letterGrade << "\n";
}
int main()
{
cout << "Student Grade Summary\n";
cout << "---------------------\n\n";
cout << "BIOLOGY CLASS\n\n";
cout << "Student Final Final Letter\n";
cout << "Name Exam Avg Grade\n";
cout << "--------------------------------------------------------------\n";
outputStudent("bill", "joeyyyyyyy", 89, 21.00, 43);
outputStudent("Bob", "James", 89, 21.00, 43);
cin.get();
return 0;
}
Output:
Student Grade Summary
---------------------
BIOLOGY CLASS
Student Final Final Letter
Name Exam Avg Grade
--------------------------------------------------------------
bill joeyyyyyyy 89 21.00 43
Bob James 89 21.00 43