formatting output in C++
Posted: Wed Aug 14, 2002 11:00 pm
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::out | 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
pepostep
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::out | 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
pepostep