1

I am working on formatting and I am not getting the correct formatting.

Here is my code and here is what I am getting for the output

cout << fixed << setprecision(2) << setw(3) << right << source << setw(20) <<right <<vec[source].cityname << setw(15) << left<< sink << setw(15) << left <<vec[sink].cityname << setw(10) << left << vdist[ij] << "miles\n"; 

I am getting this:

30 Novosibirsk_RU13 Tokyo_JP 10497.68 miles 

And I would like to be getting this:

30 Novosibirsk_RU 13 Tokyo_JP 10497.67 miles 

How can I get that space between the name and the number?

I've been working on this for a while but I just can't get the right formatting

1 Answer 1

1

You have used the std::right IO manipulator wrongly in your cout statement. To achieve the output as shown in your sample you want vec[source].cityname and vec[sink].cityname obviously left aligned:

cout << fixed << setprecision(2) << setw(3) << right << source << setw(20) << left <<vec[source].cityname << // ^^^^ setw(3) << left<< sink << // ^ setw(15) << left <<vec[sink].cityname << setw(10) << left << vdist[ij] << "miles\n"; 

Also note, if you use setw(n) and n is smaller than the length of the next field (string) variable, it won't be cutoff, but shifts the whole output to the right for the number of additional characters.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.