setw() not working properly on my code - c++

I'm having problem with my code not sure if it's a bug or there is something wrong with my code.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << setfill('*') << setw(80) << "*";
cout << setw(21) << "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;
return 0;
}
Adding manual spaces works but I want to add spaces programmatically but when I tested the application this what it looks like :

setw does not move the text but sets the minimum width it should take
To achieve what you have in mind you should experiment with a bigger value since your string is longer than 21 characters, e.g.
cout << setfill('*') << setw(80) << "*" << endl;
cout << setfill(' ') << setw(56) << "Mt.Pleasant Official Billing Statement" << endl;
cout << setfill('*') << setw(80) << "*" << endl;
Output:
********************************************************************************
Mt.Pleasant Official Billing Statement
********************************************************************************

Related

Printing random number

#include <iostream>
using namespace std;
int main() {
char gun;
char att;
cout << "Guns" << endl;
cout << "[1] AK" << endl;
cout << "[2] MP5" << endl;
cout << "[3] M2" << endl;
cout << "[4] SAR" << endl;
cout << "[5] Tommy" << endl;
cout << "[6] Custom" << endl;
cin >> gun;
cout << "[A] Holosight" << endl;
cout << "[B] Simplesight" << endl;
cout << "[C] Silencer" << endl;
cout << "[D] Muzzel Boost" << endl;
cout << "[E] Muzzel Break" << endl;
cout << "[F] 8x Zoom Scope" << endl;
cout << "[G] 16x Zoom Scope" << endl;
cout << "[H] Lasersight" << endl;
cin >> att;
switch (gun) {
case'1':
if (gun = 1)
cout << 'test' << endl;
else
cout << "Wrong Command" << endl;
break;
}
}
When I run this and select 1 (code is only half-built) it prints "1952805748". I have tried adding more cases and it still has this problem.
Can someone please help me with this problem.
You have several issues here:
if (gun = 1)
cout << 'test' << endl;
that's not a comparison; gun is always assigned the value 1, and the if condition is always true. The comparison operator is ==.
Also, you are printing out 'test', which is why you are getting the weird number. You need to print "test".
Also note that gun is a char, so you might want to compare against the char '1'.
Also, avoid using namespace std;

C++ special characters displays as "?"

I want to print a box using special characters like this
cout << "╔═══╗" << endl;
cout << "║ ║" << endl;
cout << "║ ║" << endl;
cout << "╚═══╝" << endl;
but it displays like this
?????
? ?
? ?
?????
How can I fix this?
You could try this one:
cout << (char)201 << (char)205 << (char)187 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)186 << " " << (char)186 << endl;
cout << (char)200 << (char)205 << (char)188 << endl;
I tested it and prints what you want
Find out what character set the terminal you are viewing the program output on is using, then use escape codes to put those characters in your strings
There are several types and editor you are programming with may be using a different kind the program displays with.
https://en.wikipedia.org/wiki/Box-drawing_character

Having trouble with iomanip, columns not lining up the way I expect

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

How to use setw and setfill together in C++ to pad with blanks AND chars?

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;

C++ - How to reset the output stream manipulator flags [duplicate]

This question already has answers here:
Restore the state of std::cout after manipulating it
(9 answers)
Closed 4 years ago.
I've got a line of code that sets the fill value to a '-' character in my output, but need to reset the setfill flag to its default whitespace character. How do I do that?
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
I thought this might work:
cout.unsetf(ios::manipulatorname) // Howerver I dont see a manipulator called setfill
Am I on the wrong track?
Have a look at the Boost.IO_State_Savers, providing RAII-style scope guards for the flags of an iostream.
Example:
#include <boost/io/ios_state.hpp>
{
boost::io::ios_all_saver guard(cout); // Saves current flags and format
cout << setw(14) << " CHARGE/ROOM" << endl;
cout << setfill('-') << setw(11) << '-' << " " << setw(15) << '-' << " " << setw(11) << '-' << endl;
// dtor of guard here restores flags and formats
}
More specialized guards (for only fill, or width, or precision, etc... are also in the library. See the docs for details.
You can use copyfmt to save cout's initial formatting. Once finished with formatted output you can use it again to restore the default settings (including fill character).
{
// save default formatting
ios init(NULL);
init.copyfmt(cout);
// change formatting...
cout << setfill('-') << setw(11) << '-' << " ";
cout << setw(15) << '-' << " ";
cout << setw(11) << '-' << endl;
// restore default formatting
cout.copyfmt(init);
}
You can use the ios::fill() function to set and restore the fill character instead.
http://www.cplusplus.com/reference/iostream/ios/fill/
#include <iostream>
using namespace std;
int main () {
char prev;
cout.width (10);
cout << 40 << endl;
prev = cout.fill ('x');
cout.width (10);
cout << 40 << endl;
cout.fill(prev);
return 0;
}
You can manually change the setfill flag to whatever you need it to be:
float number = 4.5;
cout << setfill('-');
cout << setw(11) << number << endl; // --------4.5
cout << setfill(' ');
cout << setw(11) << number << endl; // 4.5
The null character will reset it back to the original state:
setfill('\0')