Hello, I am doing a project and cannot seem to wrap my brain around how to get this to work. The problem I am facing is the text file created I create needs to be shown to the user in a chart form like this:
Complex Monthly Rent Collected per Complex
Main 2000.00 1000.00 3000.00 2000.00 2000.00
King 3000.00 5000.00 4000.00 5000.00 4000.00
Clay 2000.00 2000.00 4000.00 2000.00 0.00
The chart above needs to show the numbers with the decimal points. I've chosen to try to make them doubles but I cannot seem to get them to come up that way or show up in my text documents as doubles this is my code below:
// TsolometesGeorgeCIS111ApartmentComplexIncome.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
cout << "Created by: George Tsolometes" << "\n" << "11/08/2018" << "\n";
cout << "This program will calculate Rent collected per complex, average rent collected and who collected the most rent.\n" << endl;
cout << "How many months are you looking for? ";
cin >> months;
// Main Complex Input
cout << "Main's rent amount(s) ";
for (int i = 0; i < months; i++)
{
cout << fixed << setprecision(2);
cin >> MainRent;
if (MainRent == 0)
zero = true;
else
zero = false;
Main << MainRent << endl;
MainTotal += MainRent;
}
system("pause");
Main.close();
//Clay complex Input
cout << "Clay's rent amount(s) ";
for (int i = 0; i < months; i++)
{
cout << fixed << setprecision(2);
cin >> ClayRent;
if (ClayRent == 0)
zero = true;
else
zero = false;
Clay << ClayRent << endl;
ClayTotal += ClayRent;
}
system("pause");
Clay.close();
//King Complex Input
cout << "King's rent amount(s) ";
for (int i = 0; i < months; i++)
{
cout << fixed << setprecision(2);
cin >> KingRent;
if (KingRent == 0)
zero = true;
else
zero = false;
King << KingRent << endl;
KingTotal += KingRent;
}
system("pause");
King.close();
//this compares the totals and reassings the mostrent and most values to those
//it suits for future use
if (KingTotal > ClayTotal && KingTotal > MainTotal) {
mostrent = KingTotal;
most = "King";
}
else if (ClayTotal > KingTotal && ClayTotal > MainTotal) {
mostrent = ClayTotal;
most = "Clay";
}
else {
mostrent = MainTotal;
most = "Main";
}
Total = ClayTotal + KingTotal + MainTotal;
average = Total / 3;
cout << endl << endl;
cout << "Complex Monthly Rent Collected per Complex" << "\n";
//chart code in progress
// This section writes all the information to one file
inFile << endl;
inFile << "King ";
while (KingR) {
KingR.getline(lineK, 1000);
if (KingR) {
inFile << lineK << " ";
}
}
//end of the condensing stage
inFile.close();
MainR.close();
KingR.close();
ClayR.close();
string value;
int c=0;
char line[1000];
ifstream comp("ComplexMonthlyRent.txt");
while (comp) {
comp.getline(line, 1000);
if (inFile) {
cout << line << '\n';
}
}
// here down is correct
cout << endl;
cout << "Total rent collected for the company = $" << Total << endl;
cout << "The average monthly rent collected for the company = $" << average << endl;
cout << endl;
if (zero != true)
{
cout << "Complex " << most << " collected the most rent. $" << mostrent <<" was collected." << endl << endl;
}
else {
cout << "Complex " << most << " collected the most rent. $" << mostrent << " was collected." << endl;
cout << "Warning one of the complexes submitted zero rent for one of the months" << endl;
}
cstrings can be converted via sprintf and atox functions; you want atof (ascii to float).
double d = atof (cstring);
sprintf(cstring,"%1.20f", d); //other direction
it is better to use strings that C strings, if you can.
I apologize for being unclear. This code writes inputted information to from the user to the text file. so the text file is blank its there just so the program has the file for the user to input. The issue I am having is my numbers aren't showing up with decimal points even though I'm using
cout << fixed << setprecision(2); example if the user inputs 2000 it will be written as 2000 in the .txt I need it to be 2000.00