formatting output in C++

This is a discussion on formatting output in C++ within the C++ Programming forums, part of the General Programming Boards category; Hi experts. I am currently writing a program and I can not figure out how to make the output show ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    2

    formatting output in C++

    Hi experts. I am currently writing a program and I can not figure out how to make the output show what the user entered. Example if the user enters 123.25 for their household expenses after this is written to the external file and then reads from the external file I need it to echo back that 123.25. I have only been getting 123, where it rounds it.
    Here is what my program looks like so far:


    /* This program will determine how much money a user is spending each
    month. It will show the use of structures in order to keep track
    of the household expenses. It also shows how to write and retrieve
    from a external file.

    INPUT: The input will be how much the user has spent in one
    month on household expenses. (Household, Health, Personal,
    Entertainent, Savings, and Misc. Expenses)

    OUTPUT: The output is that the program will read from the file
    then echo back the user what they have entered and then
    total up the sum.
    */


    #include<fstream.h> //For writing to external file
    #include<iomanip.h> // To set width using setw()

    //Declaring structure
    struct Info
    {
    int Household;
    int Health;
    int Personal;
    int Entertainment;
    int Savings;
    int Other;
    };
    void main(void)
    {
    char FileName[81];
    cout << "Welcome to a program that helps you budget";
    cout << " your monthly expenses." << endl;
    cout << endl;
    cout << "Let's begin!" << endl;
    cout << "The file name of this program is called expense.dat" << endl;
    cout << "Please enter the file name of the program you wish to open.";
    cout << " Followed by a return: " << endl;
    cin.getline(FileName, 81);

    //Opening up file to write to
    fstream DataFile("expense.dat", ios:ut | ios::binary);
    Info Data; //Declaring a structure variable
    float total;
    //Checking for errors to make sure file was opened
    if (DataFile == 0)
    {
    cout << "File open error!" << endl;
    }
    cout << endl;
    cout << "File opened successfully." << endl;
    cout << endl;
    cout << "Below is a list of the expenses followed by some examples";
    cout << "To help you in your calculation" << endl;
    cout << endl;

    // cout << setiosflags(ios::fixed | ios::showpoint);
    // cout << setprecision(2);
    cout << "Household Expenses: " << endl;
    cout << "(electricity, gas, water, sewer, garbage, phone, cable," << endl;
    cout << "rent/mortgage, etc.)" << endl;
    cin >> Data.Household;
    cout << "Health Expenses: " << endl;
    cout << "(food, vitamins, gym, etc.)" << endl;
    cin >> Data.Health;
    cout << "Personal Expenses: " << endl;
    cout << "(cell phone, clothes, etc.)" << endl;
    cin >> Data.Personal;
    cout << "Entertainment Expenses: " << endl;
    cout << "(dinners out, movies, theater, etc.)" << endl;
    cin >> Data.Entertainment;
    cout << "Savings Expenses: " << endl;
    cout << "(house, furniture, vacation, etc.)" << endl;
    cin >> Data.Savings;
    cout << "Misc. Expenses: " << endl;
    cout << "(other...)" << endl;
    cin >> Data.Other;

    DataFile.close();
    DataFile.open("expense.dat", ios::in | ios::binary);
    if (DataFile == 0)
    {
    cout << "File open error!" << endl;
    }


    // cout << setiosflags(ios::fixed | ios::showpoint);
    // cout << setprecision(2);
    cout << "Household Expenses: ";
    cout << "$" <<Data.Household << endl;
    cout << "Health Expenses: ";
    cout << "$" <<Data.Health << endl;
    cout << "Personal Expenses: ";
    cout << "$" <<Data.Personal << endl;
    cout << "Entertainment Expenses: ";
    cout << "$" <<Data.Entertainment << endl;
    cout << "Savings Expenses: ";
    cout << "$" <<Data.Savings << endl;
    cout << "Misc. Expenses: ";
    cout << "$" <<Data.Other << endl;
    total = (Data.Household + Data.Health + Data.Personal +
    Data.Entertainment + Data.Savings + Data.Other);
    cout << "Total Amount = " <<"$" <<total<< endl;
    cout << "Thank-you for using the program." << endl;

    DataFile.close();

    }

    Thank-you for your help
    ubershatten
    sorry don't know where the face came from

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    ints don't support decimals, they will just round the decimals. use floats for the numbers instead.

    Code:
    struct Info
    {
    	float Household;
    	float Health;
     	float Personal;
     	float Entertainment;
      	float Savings;
      	float Other;
    };
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    using double should be fine (and more accurate at times)
    think only with code.
    write only with source.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include <iostream>
    
    using std::setprecision;
    using std::streamsize;
    
    int main()
    {
      double dNum = 4.55555;
    
      //normal output
      cout << "dNum = " << dNum << endl;
    
      //set output stream to 3 digits
      streamsize prec = cout.precision(3);
      cout << "dNum = " << dNum << endl;
    
      //restore output stream to normal
      cout.precision(prec);
      cout << "dNum = " << dNum << endl;
      return 0;
    }
    Last edited by Troll_King; 08-16-2002 at 01:47 AM.

  5. #5
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Originally posted by XSquared
    ints don't support decimals, they will just round the decimals.
    Actually no... The number will be truncated, a huge difference, one that you need to remember otherwise may come back to haunt you while debugging something in the future... for example:
    Code:
    #include <iostream>
    
    int main ()
    {
    	double d = 3.99;
    	int i = 3.99;
    
    	std::cout << i << ' ' << (int) d;
    	return 0;
    }
    Will output "3 3"; Truncation removes anything after the decimal, without rounding

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21