trouble w/ fstream and a double that contains a comma

Basically I have a program that is designed to read a file that contains a first & last name a integer, and a double.

What happens is the double contains a comma, which the program reads as a white space. At that point all the data gets shifted, and i end up with char in a integer handler. You can guess what happens from there.

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

int main()
{
	char quest, pathi[81] = "../employee.dat", patho[81] ="../managers.dat", name_first[20], name_last[20];
	int dep;
	double wage, wage1;

	ifstream inFile;
	ofstream outFile;

	cout <<"\nThe current path to input file is " <<pathi<<endl;
	cout <<"Do you wish to use the expected file path for this Program? Y, or N \n";
	cin >>quest;

	if ((quest == 'n') || (quest == 'N'))
		{	
			cout<<"Type in the address, and name of file you wish to use.\n";
			cin >> pathi;
		}

	quest = 'y';

	cout << "\nBegin editing files. \n";
	cout <<"Openning "<<pathi<<endl;

	
	inFile.open(pathi);		//start file, and location relative to program
	if(inFile.fail())
		{
			cout<<"Input file failed to open.\n";
			exit(1);
		}

	cout <<"The current path to output file is " <<patho<<endl;
	cout <<"Do you wish to use the expected file path for this Program? Y, or N \n";
	cin >>quest;

	if ((quest == 'n') || (quest == 'N'))
		{
		cout<<"Type in the address, and name of file you wish to use.\n";
		cin >> patho;
		}

	cout<<"The output is going to: " <<patho<<endl;

	/* outFile.open(patho, ios::app);		//creates new blank file for export
	if (outFile.fail())
		{
			cout<< "Output file failed to open";
			exit(2);				//leaves program # is the error code
		} */

	int pro= 0;
	outFile.setf(ios::fixed);
	outFile.setf(ios::showpoint);
	outFile.precision(2);

	while(!inFile.eof())
	{
		inFile >> name_first >> name_last >> dep >> wage;  //this is where is goes wrong
		cout << name_last << ", " << name_first << " "<< wage<<endl;
		pro++;
			if (pro>20)
				break;
	}

	inFile.close();
	//outFile.close();

	cout<< "End of editing files. \n";

	return 0;


}


[input]Sue Leon 4 55,000.00
Robert Wise 3 42,500.00
Sam Woo 1 47,000.00
Nathan White 3 32,500.00
Suzan Head 2 52,000.00
Henry Williams 4 45,500.00
Christine Mint 1 68,400.00
Kim Leeds 4 29,000.00
Elton Sue 3 39,600.00
Ozzie Lynch 2 41,000.00
Ken Latch 2 45,700.00
Tom Sawyer 4 49,300.00
Tommy High 3 44,000.00
Ann Silver 3 54,000.00
Tony Hope 4 41,000.00
Tammy Lee 4 29,500.00
Liz McCabe 1 59,000.00
Andy Wong 3 70,700.00
Tony Hope 1 49,200.00
Bright Gold 2 44,100.00[/input]

Thanks for any help :)
Just use the appropriate locale: US English uses commas as thousands separators, so you can use that, unless your system is particularly foreign.

Somewhere in the beginning of main(), write this:
1
2
3
4
5
#ifdef _WIN32
    std::locale::global(std::locale("English_USA"));
#else
    std::locale::global(std::locale("en_US.utf8"));
#endif 


and after inFile is opened, write this:

inFile.imbue(std::locale());

online test: http://ideone.com/dk5QL

also tested with Visual Studio on Windows 7

(and if your system actually lacks a US English locale, you could write your own std::numpunct facet)

PS: "while not eof" is an error, don't use that
Last edited on
Thanks!!

I've been using Visual studio 2010, and 11 beta. Both did the same thing, and in US English but that was giving me a headache.

Some of the stuff in this version of the program was meant as a checksum, so it is very clunky atm.
Last edited on
Topic archived. No new replies allowed.