0

This is my code:

#include <iostream>
#include <string>
#include <iomanip>
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 << "   " << n2 << endl;
    cout << setw(8) << n1 << "     " << n2 << endl;
    cout << setw(9) << n1 << "       " << n2 << endl;
    cout << setw(8) << n1 << "     " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(6) << n1 << " " << n2 << endl;
    cout << setw(5) << n1 << endl;

    return 0;
}

This is my output:

My output

However, my intended output is:

Intended output

what's wrong in my code?

Chris
  • 26,361
  • 5
  • 21
  • 42
Sohaib
  • 15
  • 4
  • Nothing's "continuing". Your middle line is set to width 9, which is *longer* than 5, hence will print further to the right. If you want it to print further to the left, you need a *smaller* width. – Silvio Mayolo Sep 06 '22 at 14:15
  • 1
    The output you are getting is what I would expect. I'm finding it hard to understand what your expectation of `setw` is. – john Sep 06 '22 at 14:17
  • 1
    *Kindly explain what's wrong in my code* -- What's the reason for `setw` being used? I don't see a need for it at all. This is all a matter of generating the correct string and displaying that string on each line. A loop and a little bit of logic could have been used. – PaulMcKenzie Sep 06 '22 at 14:17
  • 1
    It seems that your expectation for a line like `cout << setw(6) << n1 << " " << n2 << endl;` is that the `setw(6)` applies to all three of `n1`, `" "`, and `n2`, taken together. Is that a fair summary? – Nathan Pierson Sep 06 '22 at 14:18
  • Please read more about [`std::setw`](https://en.cppreference.com/w/cpp/io/manip/setw). – Some programmer dude Sep 06 '22 at 14:19
  • @PaulMcKenzie yes, you are right. This can be done in a simpler way, but the requirement of my assignment is to print this pattern with setw() func. – Sohaib Sep 06 '22 at 15:10
  • @MuhammadSohaib -- What other requirements are there? When you post code and a question, you will get answers and comments assuming you can use whatever C++ has to offer. We did not sit in your classroom to know what you can and cannot use. – PaulMcKenzie Sep 06 '22 at 15:11
  • @NathanPierson umm, no. That's not my expectation. I expect setw(6) to be only applied on the cout statement in which I write it. For e.g. if I write cout << setw(6) << n1 << " " << n2 << endl; then I expect that on the 4th character position, there should be a 1 printed (since n1 has an integer val of 1), on the 5th place, there should be a space, and on the 6th character position, there should be a 2 printed (because setw by default displays from the right-hand side). – Sohaib Sep 06 '22 at 15:17
  • I don't understand how your description of what you thought would happen differs from my summary of what I thought you thought would happen. To clarify what's actually happening: The `setw(6)` means that the `n1` gets printed at the 6th position. Then the `" "` and the `n2` get printed without any effect from the earlier `setw`. – Nathan Pierson Sep 06 '22 at 15:19
  • @PaulMcKenzie sorry, this is my first question. I tried to make my question as clear as possible, just forgot to mention that I am required to print the pattern using setw() only. – Sohaib Sep 06 '22 at 15:21
  • @NathanPierson thank you so much, I've understood now. – Sohaib Sep 06 '22 at 15:32
  • @MuhammadSohaib In this case it is more straightforward to just print spaces directly instead of calling `setw()`. – Zenul_Abidin Sep 06 '22 at 18:31

1 Answers1

0

You just put the numbers in wrong order, try this:

int main() {
    int n1 = 1, n2 = 2;
    cout << setw(9) << n1 << endl;
    cout << setw(8) << n1 << " " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(6) << n1 << "     " << n2 << endl;
    cout << setw(5) << n1 << "       " << n2 << endl;
    cout << setw(6) << n1 << "     " << n2 << endl;
    cout << setw(7) << n1 << "   " << n2 << endl;
    cout << setw(8) << n1 << " " << n2 << endl;
    cout << setw(9) << n1 << endl;

    return 0;
}
Feniks
  • 49
  • 3