0

I am new to C++ STL libraries and need help.
I want to add two numbers suppose A = 4555 and B = 50, and output them as:

4555
  +50
4605

Another Examples:

500000 + 12

500000
      +12
500012

If i am storing both A and B in integer data type while the sign '+' in character data type. How can i manipulate them to get the preferred output. I just cant figure out how to manipulate two variables together.

  • Combine the sign and the number into a string: `std::string s = signChar + std::to_string(value);` Now print `s` with formatting to taste. – Igor Tandetnik Apr 13 '15 at 16:59

3 Answers3

2

You might utilize the manipulators std::showpos, std::noshowpos and std::setw:

#include <iostream>
#include <iomanip>

int main() {
    int a = 4555;
    int b = 50;
    std::cout
        << std::noshowpos << std::setw(10) << a << '\n'
        << std::showpos   << std::setw(10) << b << '\n'
        << std::noshowpos << std::setw(10) << (a+b) << '\n';
}

If you want a width depending on the values you may use three std::ostringstream(s) and create intermediate strings (without setw). After that you print the strings using the maximal length of each for setw:

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>

int main() {
    int a = 4555;
    int b = 50;

    std::ostringstream as;
    std::ostringstream bs;
    std::ostringstream rs;
    as << std::noshowpos << a;
    bs << std::showpos   << b;
    rs << std::noshowpos << (a+b);
    unsigned width = std::max({ as.str().size(), bs.str().size(), rs.str().size() });
    std::cout
        << std::setw(width) << as.str() << '\n'
        << std::setw(width) << bs.str() << '\n'
        << std::setw(width) << rs.str() << '\n';
}

See also:

Note: You may have a look at the manipulator std::internal.

0

If you could use constant width (or variable width equal to the maximum width of the numbers involved) with std::setw from <iomanip> as:

#include <iostream>
#include <iomanip>
#include <string>

void display_sum(int a, int b)
{
    std::cout << std::setw(10) << a  << "\n"
              << std::setw(10) << ("+" + std::to_string(b)) << "\n"
              << std::setw(10) << (a+b) <<"\n" << std::endl;
}

int main()
{
    display_sum(4555, 50);
    display_sum(500000, 12);
    display_sum(503930, 3922);
}

Output:

  4555
   +50
  4605

500000
   +12
500012

503930
 +3922
507852

Online demo

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • Hi Nawaz, I would make some slight changes to your soln, including determining the number of digits at run-time to ensure the field is large enough, as well as using the `showpos` flag to print a + or - symbol based on sign of `b`. Here's my slightly more complete example: http://ideone.com/ApLZEV – AndyG Apr 13 '15 at 17:25
0

In your example the fields can fit a maximum number of 7 characters. Perhaps you want to resize the strings to 7 before writing. e.g. fname.resize(7).

To format it as you want you need to #include <iomanip> and use std::left and std::setw(7).

file1 << left << setw(7) << fname
      << tab << setw(7) << lname
      << tab << setw(7) << street
      << tab << setw(7) << city
      << tab << setw(7) << state
      << tab << setw(7) << zip << endl;
randag
  • 423
  • 8
  • 7