Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I have a small alignment issue in my program.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(5) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name" << endl << endl;
cout << setw(5) << 1 << setw(15) << "January" << setw(15) << "Abhilash" << endl;
cout << setw(5) << 2 << setw(15) << "Februaury" << setw(15) << "Anandan" << endl;
cout << setw(5) << 3 << setw(15) << "March" << setw(15) << "Abhilash" << endl;
cout << setw(5) << 4 << setw(15) << "April" << setw(15) << "Anandan" << endl;
return 0;
}
In the output I get, the names of the months are not right justified.
Sl. No: Month Name
1 January Abhilash
2 Februaury Anandan
3 March Abhilash
4 April Anandan
What seems to be the problem?
The string Sl. No: is 7 wide and you are trying to fit it into a 5 wide column. That pushes the first row over by 2 columns. Try making your first column 7 wide rather than 5 wide instead:
cout << setw(7) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name"
<< endl << endl;
cout << setw(7) << 1 << setw(15) << "January" << setw(15) << "Abhilash"
<< endl;
//...
When you want to use setw, you have to count from the end of the output string, int, etc. So when you say
cout << setw(15) << "January";
It'll format 8 spaces in between since January is 7 characters. So in your example, you want to have
cout << setw(23) << "January";
That obviously depends if you keep your first output of '1' in the same place.
You can also use "\t" before setw() to make sure your not eating into the space.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setw(5) << "Sl. No:" << setw(15) << "Month" << setw(15) << "Name" << endl << endl;
cout << setw(5) << 1 << "\t" << setw(15) << "January" << "\t" << setw(15) << "Abhilash" << endl;
cout << setw(5) << 2 << "\t" << setw(15) << "Februaury" << "\t" << setw(15) << "Anandan" << endl;
cout << setw(5) << 3 << "\t" << setw(15) << "March" << "\t" << setw(15) << "Abhilash" << endl;
cout << setw(5) << 4 << "\t" << setw(15) << "April" << "\t" << setw(15) << "Anandan" << endl;
return 0;
}
Oh you need to use spacing in front of the "month"
" month"
not
"month"
the results will look like this
Month
January
you may have to adjust the amount of spacing you use.
Related
I'm trying to make a receipt, andbalways want the " kg" to be ONE SPACE after the weight, and also "$" just before both 'costperkg' and 'totacost' Initially using setw to format the output, could not get it to work, got it done with ostringstream. I Can anyone explain why does pushing double quote string does not work?
This one does not work :
int main()
{
string item = "A" ;
double weight = 2.00 ;
double costperkg = 1.98 ;
double totalcost = 3.96 ;
cout << fixed << showpoint << setprecision(2);
cout << setw(14) << left << "ITEM" << setw(16) << "WEIGHT" << setw(18) << "COST/kg"
<< setw(14) << "COST" << endl ;
cout << setw(14) << left << item << setw(16) << weight << "kg" << setw(18) << "$"
<< costperkg << setw(14) << "$" << totalcost << endl << endl ;
}
This one works:
ostringstream streamweight, streamcostperkg, streamtotalcost;
streamweight << fixed << showpoint << setprecision(2) << weight ;
streamcostperkg << fixed << showpoint << setprecision(2) << costperkg ;
streamtotalcost << fixed << showpoint << setprecision(2) << totalcost ;
string strweight = streamweight.str() + " kg" ;
string strcostperkg = "$" + streamcostperkg.str() ;
string strtotalcost = "$" + streamtotalcost.str() ;
cout << setw(14) << left << item << setw(16) << strweight << setw(18) << strcostperkg
<< setw(14) << strtotalcost << endl << endl ;
The expected result is :
ITEM WEIGHT COST/kg COST
A 2.0 kg $1.98 $3.96
What I got instead is :
ITEM WEIGHT COST/kg COST
A 2.00 kg$ 1.98$ 3.96
Why does the setw one not work? and also for those viewing on phone, the first character from first and second life of every word should align on the first letter (A, 2, $, $)
OP suspected the std::setw() not to work. IMHO, OP is not aware that the setw() does exactly what's expected but the formatting considers as well the std::left manipulator which makes all following output left aligned. (The left alignment becomes effective in combination with setw() only.)
Example:
#include <iostream>
#include <iomanip>
// the rest of sample
int main()
{
std::cout << '|' << std::setw(10) << 2.0 << "|kg" << '\n';
std::cout << std::left << '|' << std::setw(10) << 2.0 << "|kg" << '\n';
// done
return 0;
}
Output:
| 2|kg
|2 |kg
Live Demo on coliru
(A possible fix is exposed in the question by OP her/himself.)
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
I need to write a program for a homework assignment to calculate tuition costs and the output should be formatted like so with the dot padding and space padding after the dollar sign:
Student Name: Name goes here
Address: Address goes here
Number of credits: .......... 5
Cost per credit hour: ............ $ 50
My code so far is:
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
double const REG_FEE = 700, STUDENT_ASSEMBLY_FEE = 7.19, LEGAL_FEE = 8.50, STUDGOV_FEE = 1.50,
LATE_FEE_PERCENTAGE = 0.015;
int main()
{ double num_credits, cost_per_credit, tuition_total, late_charge, amount_due;
string student_name;
string student_address;
string student_city_state_ZIP;
ifstream info;
info.open ("info.txt");
getline (info, student_name);
getline (info, student_address);
getline (info, student_city_state_ZIP);
info >> num_credits;
info >> cost_per_credit;
tuition_total = num_credits * cost_per_credit + REG_FEE + STUDENT_ASSEMBLY_FEE + LEGAL_FEE
+ STUDGOV_FEE;
late_charge = tuition_total * LATE_FEE_PERCENTAGE;
amount_due = tuition_total + late_charge;
cout << "Tuition and Billing Program by Neal P." << endl
<< setw(18) << "Student Name:" << student_name << endl
<< setw(18) << "Address:" << student_address << endl
<< left << setfill('.') << endl
<< setfill(18) << "Number of Credits:" << setw(5) << "$" << num_credits << endl
<< setfill(18) << "Cost per Credit Hour:" << setw(5) << "$" << cost_per_credit << endl
<< setfill(18) << "Tuition Cost:" << setw(5) << "$" << tuition_total << endl
<< setfill(18) << "Registration Fee:" << setw(5) << "$" << REG_FEE << endl
<< setfill(18) << "MSA Fee:" << setw(5) << "$" << STUDENT_ASSEMBLY_FEE << endl
<< setfill(18) << "Legal Services Fee:" << setw(5) << "$" << LEGAL_FEE << endl
<< setfill(18) << "Student Government Fee:" << setw(5) << "$" << STUDGOV_FEE << endl;
return 0;
}
When I compile I get a very long error, something like : "In function ‘int main()’:
/Users/nealp/Desktop/machine problem 2.cpp:41: error: no match for ‘operator<<’ in ‘((std::basic_ostream >*)std::operator<< [with _CharT = char, _Traits = std::char_traits](((std::basic_ostream >&)((std::basic_ostream >*)((std::basic_ostream >*)((std::basic_ostream >*)std::operator<<" that continues.
Is this an issue with my use of both setw and setfill together? I know that setfill only has to be declared once and setw is only effective on the next line output, but I defined setfill each time because I was using setw before it.
std::setfill is a function template and it's template argument gets deduced based on the type of the argument you pass it to. It's return type is unspecified - it's some implementation defined proxy type that sets fill character on the stream object and enables further chaining of operator<< calls. It's also dependent on the type that setfill gets instantiated with.
The type of literal 18 is int so setfill returns some unrelated type for which there's no operator<< avaliable.
I'm not sure what you meant with setfill(18), I'm guessing you've mistaken it for setw(18).
Just replace setfill(18) in your code, that is the reason for compilation error.
Here is the improved printing portion to fit the requirements,
cout << "Tuition and Billing Program by Neal P." << endl
<< "Student Name:" << student_name << endl
<< "Address:" << student_address << endl
<<endl<<endl<< "Number of Credits:" << setfill('.') << setw(5) <<"$" << num_credits
<< endl<< "Cost per Credit Hour:"<<setfill('.')<<setw(5)<< "$" << cost_per_credit <<
endl<< "Tuition Cost:" << setfill('.')<<setw(5)<< "$" << tuition_total << endl
<< "Registration Fee:" << setfill('.')<<setw(5) << "$" << REG_FEE << endl
<< "MSA Fee:" << setfill('.')<<setw(5) << "$" << STUDENT_ASSEMBLY_FEE << endl
<< "Legal Services Fee:" << setfill('.')<<setw(5) << "$" << LEGAL_FEE << endl
<< "Student Government Fee:" << setfill('.')<<setw(5) << "$" << STUDGOV_FEE << endl;