Converting Char to Double

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.
//

#include "pch.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include<cstdlib>
#include<stdlib.h>
#include <string>


using namespace std;


int main()
{
int months;
string complexName, most;
double average, mostrent;
double KingRent = 0.00;
double MainRent = 0.00;
double ClayRent = 0.00;
double KingTotal = 0.00;
double MainTotal = 0.00;
double ClayTotal = 0.00;
double Total = 0.00;

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;

ofstream King;
ofstream Main;
ofstream Clay;
ofstream inFile;


inFile.open("ComplexMonthlyRent.txt");
Main.open("Main.txt");
Clay.open("Clay.txt");
King.open("King.txt");

bool zero;

// 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

char lineK[1000];
char lineM[1000];
char lineC[1000];
ifstream MainR("Main.txt");
ifstream ClayR("Main.txt");
ifstream KingR("King.txt");
cout << fixed << setprecision(2);

inFile << "Main ";
while (MainR) {
MainR.getline(lineM, 1000);
if (MainR) {
inFile << lineM << " ";
}
}
inFile << endl;
inFile << "Clay ";
while (ClayR) {
ClayR.getline(lineC, 1000);
if (ClayR) {
inFile << lineC << " ";
}
}

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;
}


return 0;

}



thanks for your time and help!
Last edited on
It's unclear to me what you're asking. What is the part of your code you having problems with?
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.
Please provide the contents of ComplexMonthlyRent.txt and the cin input that you're using.

Note that you don't need #include "pch.h" at the beginning. The program compiles without it.
is that the windows precompiled header thingy that it puts in for you?
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
You just use fixed/setprecision for cout. You need it for the files as well.
Just as @coder777 mentioned, rather than manipulating cout, you should manipulate ifstream. I.e., you should write "Main << fixed << setprecision(2)".
oh thanks that one helped a lot :)
Topic archived. No new replies allowed.