C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You don't need a fax machine to get faxes. Get a fax-to-email fax number from CallWave. Try it free.
  #1  
Old February 24th, 2002, 01:15 PM
meeh82 meeh82 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2000
Location: Riverside, CA
Posts: 63 meeh82 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
Formatting Help

I have this program and I need to know how to format it correctly:

#include <iostream.h>
#include <math.h>
#include <iomanip.h>

void main(void)
{

double Principle = 0.0;
double Interest = 0.0;
double Savings = 0.0;
float InterestR = 0.0f;
int T = 0;

cout << "Savings Account" << endl << endl;

cout << "Please enter in the principle: ";
cin >> Principle;

cout << "Please enter in the interest rate: ";
cin >> InterestR;
InterestR = InterestR/100;

cout << "Please enter in the number of times compounded during a year: ";
cin >> T;

Savings = Principle * pow((1 + (InterestR / T)),T);
Interest = Savings - Principle;

cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);

//Formatted text needs to go here

}

This is what the output should look like:

Interest Rate: 4.25%
Times Compounded: 12
Principle: $ 1000.00
Interest: $ 43.33
Amount in Savings: $ 1043.33

Another Example would be:

Interest Rate: 50%
Times Compounded: 12
Principle: $ 10000.00
Interest: $ 6320.94
Amount in Savings: $ 16320.94

I need the numbers to line up correctly each time, no matter how big the numbers are. I need the last number on the right of each number to line up. So basically it needs to be right aligned.

Thanks,
Jonathan Donaghe

Reply With Quote
  #2  
Old February 25th, 2002, 02:35 PM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 8
cout.width

If you calculate the number of characters you'll be printing, you can use the cout.width() member. The cout.width() member function will automatically fill the blank area with whitespace to right align the next output with the width you specify. You can change the fill character with cout.fill(), I believe.

Code:
    // Just assign the character arrays for now
    // for simplicity.
    char chInt[] = "Interest Rate:";
    char chIntRate[] = "4.56%";
    char chPrinc[] = "Principle:";
    char chPrinciple[] = "$9 876.54";

    // bring into namespace
    using std::cout;
    using std::endl;

    // print out the formatted results
    cout << chInt;
    cout.width(40 - sizeof(chInt));
    cout << chIntRate << endl;
    cout << chPrinc;
    cout.width(40 - sizeof(chPrinc));
    cout << chPrinciple << endl;

    return 0;

Reply With Quote
  #3  
Old February 26th, 2002, 10:31 PM
Er1c Er1c is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2002
Posts: 0 Er1c User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
You can use setw(length) too, if you wanted.

Code:
cout << setw(10) << "Text";

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Formatting Help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway