Sign in to follow this  

A little C++ formating help. please...

Recommended Posts

Hi, I have written a little program for my hw... and I need the output so it lines up at the end.. for example... 34.439 _33.43 ___3.0 I am suppose to use setpression() but can't figure out how to do that.. please help! thanks! #include<iostream>//including the library called "iostream" (#=Pre-processor directive) #include <iomanip> using namespace std; //introduces namespace std int main ( ) { double firstno, secondno, thirdno; cout << "Enter 3 decimal numbers:"; cout << "\nFirst Decimal Number: "; cin >> firstno; cout << "\nSecond Decimal Number: "; cin >> secondno; cout << "\nThird Decimal Number: "; cin >> thirdno; cout << "\nDecimals sorted in descending order" << endl; if (firstno > secondno && firstno > thirdno) //compairing three numbers { cout <<"$" << right << firstno<<endl; // printing largest number $ if(secondno > thirdno) { cout <<"$"<< right << secondno<<endl; //printing second largest number $ cout <<"$"<< right <<thirdno<<endl; //printing smallest number $ } else { cout << "$"<< right <<thirdno<<endl; cout << "$"<< right << secondno<<endl; } } if (secondno > firstno && secondno > thirdno) { cout <<"$"<< right << secondno<<endl; if(firstno > thirdno) { cout <<"$"<< right << firstno<<endl; cout <<"$"<< right << thirdno<<endl; } else { cout <<"$"<< right <<thirdno<<endl; cout <<"$"<< right << firstno<<endl; } } else { cout <<"$"<< right << thirdno<<endl; if(firstno > secondno) { cout <<"$"<< right << firstno<< endl; cout <<"$"<< right << secondno<<endl; } else { cout <<"$"<< right << secondno<<endl; cout <<"$"<< right <<firstno<<endl; } } cout << endl; return 0; }

Share this post

Link to post
Share on other sites
This seems to be a good website detailing the components of iomanip.

In your provided example you have 5 digits of precision with 1 decimal point, and you want to align it to the right. Before you display each variable, you should use three iomanip functions. Look at that page to find the ones you need. Also, the syntax for using it would be: cout << (iomanip function) << (another iomanip function) << (yet another iomanip function, etc.) << variable;. At the bottom of the page are some examples of use.

Share this post

Link to post
Share on other sites
Hm, I ought to advise you now before you get discouraged that seeking help with homework tasks is pretty much frowned on around here. The tasks are set to let you figure things out on your own, no doubt. I'm not familiar with 'setpression()' -- perhaps you mean setprecision()? If so, I'm not going to explain how it works (I presume your instructor has done so). But I can suggest that you use Google to search for setprecision() ;)


Share this post

Link to post
Share on other sites
It is much easier than that to sort three numbers. But first, you will have to put them in a container of some sort, so that we can write code that assumes they are "related" to each other. Since we know how many of them there are (3), we can just use an array.

#include <iostream>
#include <iomanip>
#include <algorithm> // the library knows how to sort things for us.
using namespace std;

int main () {
double numbers[3];

cout << "Enter 3 decimal numbers:";
cout << "\nFirst Decimal Number: ";
cin >> numbers[0];
cout << "\nSecond Decimal Number: ";
cin >> numbers[1];
cout << "\nThird Decimal Number: ";
cin >> numbers[2];

// Do the sort. We provide "iterators" that mark the range of values to
// be sorted. A pointer is a kind of iterator; here, we use a pointer to the
// beginning of the array, and a pointer to just past the end of it. The
// "distance" between the iterators equals the number of elements to sort.
sort(numbers, numbers + 3);

cout << "\nDecimals sorted in descending order" << endl;
// The sort actually sorted the numbers in ascending order, so you will
// output them backwards: numbers[2], numbers[1] and finally numbers[0].

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this