ifstream read double from file

closed account (o1pz6Up4)
I'm trying to read a line of information from a file and I'm running into a problem while reading the double. I need the program to read and display the number 5693.3 and it's displaying some weird long number including a symbol. Any help is greatly appreciated!

This is the line of information:
John H. Doe 1001 H 5693.3

The command window outputs this:
John H. Doe 1001 H 5693.3-858993460╠-9.25596e+61


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

void readBillingRecord(ifstream& fin, string& name, int& accNum, char& type, double& usage);

int main()
{
	string billingDataFile = "Water Bill Records.txt";
	string billingReportFile = "Water Bill Report.txt";
	ifstream fin;
	ofstream fout;
	string name;
	int accNum;
	char type; 
	double usage;

	fin.open(billingDataFile);
	if (fin.fail())
		{
			cout << "\nError opening " << billingDataFile << " file for reading. \nProgram Terminated. \n";
			system("pause");
			return EXIT_FAILURE;
		}
    readBillingRecord(fin, name, accNum, type, usage);
	system("pause");
	return 0;
}

void readBillingRecord(ifstream& fin, string& name, int& accNum, char& type, double& usage)
{
	fin >> name >> accNum >> type >> usage;
	cout << name << accNum << type << usage << endl;
}
Last edited on
getline reads the whole line.
closed account (o1pz6Up4)
Yes, I see where I went wrong. However, now I'm stuck on how to get the name stored in a string including white space characters, and then get the rest of the input stored separately as an int, char, and double?

Edit: Here is the entire file I need to read and extract data from line by line. I need the name extraction to work for all types of names.

John H. Doe 1001 H 5693.3
James Randolph, Jr. 3333 H 1000.0
Sara Lawrence-Smith 2456 H 3999999.5
Good Time Industries 4678 C 10000000.1
Big Business, Inc. 6757 I 12500849.9
Mom and Pop Shop 5002 C 4000000.7
The O'Leary Company 8022 I 9999999.9
Last edited on
closed account (o1pz6Up4)
Alright, so far I've gotten this piece of program to read the lines and separate the pieces into the data types I need. However, it is creating and unhandled exception at accNum = stoi(m[2]);

Here is the modified function code that I changed in the above program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void readBillingRecord(ifstream& fin, string& name, int& accNum, char& type, double& usage)
{
	string line;
	regex r{ R"(([\D]*)(\d*) (\w) (\d*.?\d*))" };
	smatch m;
	while (getline(fin, line)) 
	{
		regex_match(line, m, r);

		name = m[1];
		accNum = stoi(m[2]);
		type = string{m[3]}[0];
		usage = stod(m[4]);

		cout << name << accNum << type << usage << endl;
	}
}
Last edited on
Make sure that the lines does not contain spaces at the end, or change your regex to handle it properly.
You should check the return value of regex_match before using the matches, or use a try-catch block.
Topic archived. No new replies allowed.