Equivalent of to_string() in c++

So I have finished the assignment however, I can't seem to wrap my head around another equivalent in c++ for to_string() inside string convertInteger(long n, int base) My error appears right at tostring()


here is my code :
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;


string convertInteger(long n, int base);


string convertFraction(double fraction, int base);


int main(int argc, char* argv[])
{
	string infname;
	string outfname;

	
	if (argc < 2)
	{
		cout << "Enter the input file name: ";
		cin >> infname;
	}
	else
		infname = argv[1];

	
	if (argc < 3)
	{
		cout << "Enter the output file name: ";
		cin >> outfname;
	}
	else
		outfname = argv[2];


	
	ifstream in(infname.c_str());
	if (!in.is_open())
	{
		cout << "Error opening input file " << infname << endl;
		return 1;

	}

	ofstream out(outfname.c_str());
	if (!out.is_open())
	{
		cout << "Error opening output file " << outfname << endl;
		return 1;
	}

	double n;
	int base;


	while (in >> n)
	{
		in >> base;
		long integral = n; 
		double fraction = n - integral;

		string converted = convertInteger(integral, base);
		converted += convertFraction(fraction, base);

		
		out << fixed << setprecision(8) << setw(20) << n << " ";
		out << setw(2) << base << " ";
		out << setw(20) << converted << endl;
	}

	in.close();
	out.close();

}


string convertInteger(long n, int base)
{
	int rem;
	string str;

	while (n > 0)
	{
		rem = n % base;
		if (rem < 10) 
			str = to_string(rem) + str; 
		else 
		{
			char digit = 'a' + rem - 10;
			str = digit + str;
		}
		n /= base;
	}
	if (str.empty())
	{
		str = "0";
	}
	return str;
}


string convertFraction(double fraction, int base)
{
	int int_part;
	string str = ".";

	
	while (fraction != 0)
	{
		fraction = fraction * base;
		int_part = fraction;
		str += convertInteger(int_part, base); 
		fraction = fraction - int_part;
	}

	while (str.length() < 5)
		str += "0";

	return str;
}
another equivalent in c++ for to_string()


The same way as you do it for base > 10! Instead of using 'a', use '0'. so that 0 becomes '0', 1 becomes '0' + 1 which is '1' etc.

1
2
3
			//str = to_string(rem) + str;
			char digit = '0' + rem;
			str = digit + str;

Last edited on
Note that convertInteger() can be simplified. Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string convertInteger(long n, int base)
{
	string str;

	for (; n > 0; n /= base)
	{
		const int rem = n % base;
		const char digit = rem < 10 ? '0' + rem : 'a' + rem - 10;

		str = digit + str;
	}

	if (str.empty())
		str = "0";

	return str;
}


using the ternary operator ?
Last edited on
Re convertFraction.

It's usually a bad idea to do exact comparisons with float/double as the internal representation of a decimal using binary may not be exact. Normally a double/float test is for the value to be less than a specified constant (epsilon). Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string convertFraction(double fraction, int base)
{
	const double epilson = 0.0000001;
	string str = ".";

	while (fraction > epilson)
	{
		const int int_part = fraction *= base;

		str += convertInteger(int_part, base);
		fraction -= int_part;
	}

	while (str.length() < 5)
		str += "0";

	return str;
}


Note the initialisation of int_part. All assignments return the value of the assignment so fraction *= base returns the result of that assignment which is then in turn assigned to int_part.
Last edited on
Hello @Lastchance, i believe i made the mistake to double post , when i didn't know i could, I am still bright new to the website, so that is why i deleted them, THANKS :)
This is not a bad place for DIY code. The built in number to string and string to number stuff is all pretty sluggish, and rolling your own can be both educational and practical. But 99.99 % of code should use the built in ones or avoid the process. Only a very few things truly need it -- I did it about a year ago on an MD5 implementation and pulled it from 1.8 seconds per million inputs to .45 per million just by changing c++'s int to string to my own.

I found several websites on approaches to doing it various ways.
Last edited on
Topic archived. No new replies allowed.