Questions tagged [setw]

`std::setw` is a `C++` function used to sets the field width to be used on output operations.

Defined in the <iomanip> include file. See C++ reference page on setw.

119 questions
0
votes
1 answer

Why is my program continuing on the setw value which I have set for the previous cout statements? And this is happening in a pattern too?

This is my code: #include #include #include using namespace std; int main() { int n1 = 1, n2 = 2; cout << setw(5) << n1 << endl; cout << setw(6) << n1 << " " << n2 << endl; cout << setw(7) << n1 << " "…
Sohaib
  • 15
  • 4
0
votes
1 answer

How does the setw stream manipulator work?

How does the setw stream manipulator (space count) work? When there is one \t, for example, I want to print a with four spaces, so I use \t and I compare \t with setw. The code that I wrote: # include # include int main() { …
0
votes
2 answers

setw() and setfill() not working...what am i doing wrong?

what i am trying to do is print double datatype with precision 2,setw(15),fill spaces with _(underscore) and with prefix - or +.for example if number is 2006.008 output should be _______+2006.01 my code is : cin>>b; if(b>0){ …
0
votes
1 answer

How to make an array of pointers and make the user enter the size of it?

I want to make an array, and inside this array there are pointers, like this: int *arrp[size]; and I want the user to enter the size of it. I tried to do this: #include using namespace std; int main () { int size; cout << "Enter…
MHMD
  • 1
  • 4
0
votes
0 answers

setw outputting one line differently in a loop

I'm doing a college assignment, and for whatever reason the first displayed line of text doesn't line up to the following lines, despite it being from a for loop. void displayInventory(string itemNames[], double itemCost[], int…
0
votes
0 answers

setw() does nothing when in a line with a variable (g++ in Windows terminal)

In the line below, setw(3) is ignored when I compile: cout << setw(3) << sym; I found out that not using a variable works fine: cout << setw(3) << "*"; Below are both versions of the full code (the only difference is in line 13) and an image of…
0
votes
2 answers

How to repeat char input using setw and setfill? the first part repeats but the second doesn't

I need the following output by setw and setfill: aaa______aaa (These underscores represent spacing) (No spaces allowed in code) Link also attached #include #include using namespace std; int main() { char input=0; cout <<…
0
votes
2 answers

How can I use setw() with input stream

I know some people will say this question is repeated but I really couldn't find a useful answer. Let's say I have the following program: #include #include using std::cout; int main(){ std::string something; cout<<"Type…
Seba
  • 21
  • 8
0
votes
2 answers

setw working on alphabets but not on dashes and underscores

This is the code which gives my desired output using spaces, but I have to perform the task using setw which isn't working as I want(2nd code attached). Help please! #include #include using namespace std; int main() { cout << …
0
votes
2 answers

Using std::setw() without header

How is it possible for this code to compile even though I didn't include ? #include #include int main() { std::cout << std::setw(5) << "test" << std::endl; return 0; } Compiles with: clang++ test.cpp But…
0
votes
1 answer

How to use a shorter name for `setw`, `setprecision`, `left`, `right`, `fixed` and `scentific`?

I would like to use these STL functions in my code, but so many times that their presence makes the readability of the code cumbersome. Is there any way to define "aliases" for these commands?
TobiR
  • 207
  • 3
  • 11
0
votes
0 answers

first entry in cout statement tapped over

I have this program (below). I have everything working like I want except for the first entry in the cout statement in the function displayOne(). The output is displayed below. How do I fix the first entry (#1009) to line up with the rest? …
0
votes
1 answer

I am having trouble understanding the purpose of setw(n) function in C++

I have been studying C++ from a textbook and in my opinion their explanations of the purpose of the setw() functions have been poor. Simply put, I do not understand the purpose of these functions, and online explanations have not been much help…
0
votes
2 answers

Why is the output ending the line early and filling every empty spaces with setfill instead of creating a line with it?

Im trying to create a ling of the special character "=" Output I want Name==================== Horace Horsecollar 56.00 12.50 700.00 210.00 10 480.00 Eleven Thirty 34.00 12.50 425.00 127.50 10 287.50 Victoria Elven 34.00 …
0
votes
1 answer

Can't align properly using setw manipulator

I can't align the output of my program. I want to keep the same names and get the right spacing. The code is provided below. I also tried using left but it still does not work. The output I am expecting: The output I am getting: //taking name…