High I am in Europe right now and here they treat their commas as decimals points. I am working with Visual express 2008 and I cannot find a way to read a file that has all commas for decimal points. I can read the numbers as a string, but than I need a function to convert it back to numbers and change the commas into decimal points. Anyone have any suggestions?

Please keep in find that I want to keep the file the same way and I don't want to use the find and replace function in the word proccessor to change the commas into decimals.

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib>
using namespace std;

int main()
	string TIME_SD;

	ifstream in("C:\\Dokumente und Einstellungen\\Yaser\\Eigene Dateien\\Internship\\C++_Code\\Data_3.txt");
	ofstream out("C:\\Dokumente und Einstellungen\\Yaser
	string line;
	while( in >> TIME_SD)
	cout << TIME_SD <<  endl;


return 0;

Recommended Answers

All 4 Replies

You can use stringstreams.

Here is the main algorithm which you can work on.

1 ) Get your number into a string.
2 ) replace the ',' with '.'
3 ) Then you can always put the number into the stringstream and then get back the value in the form of a double.
You will need to include <sstream> as well for this.

stringstream s;
s<<str;//put the replaced string in here.
double d;

You mean to say Ten Point Three Eight is represented as 10,38 rather than 10.38 ???

Well if the answer to the above question is yes then of course you cant use the float data type for your work.So define your own data type something as follows:

class myfloat{
string temp;
int bef_decpoint;
int aft_decpoint;
float value;
//Some functions

Now what you can do is this:

1>Read in the string 10,38 up till "," into temp.


//To get the integer value
istringstream out(temp);

3>Read in string 10,38 after the "," till '\0' (not including) into temp.


//To get the numeric value of aft_decpoint
istringstream out(temp);
//To get the floating value
value=(float)bef_point+(float)aft_point / (10*temp.length());

5>Use it as you want now.

You didn't want to change the file so this is one of the "Brute force" ways to solve your problem.
Sky diploma has provided a far better way but with this approach you can use the "before ." and "after ." as separate entities in case you need it. :)

Why not change the locale to something that uses ',' as the radix character? Then you don't have to do any mumbo jumbo with strings:

#include <iostream>
#include <locale>
#include <sstream>

using namespace std;

int main()
    istringstream is("12,34");
    double a = 0;


    is >> a; // will recognize ',' as a radix
    cout << a << '\n';
commented: sweet. i lernt something today. +13
commented: as did I. +22

Cool Thanks! I tried Tom Gunn's way and it seemed most suitable for my understanding. Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.