I would convert the number to a string using sprintf() then loop through each character in the string and check with isdigit(). If true, then atoi() the character and add it to the sum. An interesting exercise!
vegaseat
DaniWeb's Hypocrite
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
Problem is, when I run threw the loop in the function and arrive at the fourth digit '4', the compiler changes the number into '39997' and I get a wrong result :o
Floating point behaves this way because the value stored is an approximation. Since you're passing adouble, you may want to receive it as a double rather than a float. Then you may discover...
Using sizeof (n) isn't the way to go about it either. That is the bytes required for the underlying storage, not the number of digits beyond the decimal place.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>don't know the name of ',' in English
Radix
>I don't know how I can do that
Floating-point is a bitch to work with. You would be better off converting to a string as vegaseat suggested:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int precision (double val)
{
int n = 0;
ostringstream out;
out<< val;
string s = out.str();
return s.length() == 1 ? 0 : s.length() - 2;
}
int main()
{
cout<< precision ( 0.0 ) <<endl;
cout<< precision ( 0.1 ) <<endl;
cout<< precision ( 0.12 ) <<endl;
cout<< precision ( 0.123 ) <<endl;
cout<< precision ( 0.1234 ) <<endl;
cout<< precision ( 0.12345 ) <<endl;
cout<< precision ( 0.123456 ) <<endl;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>why is it that in your example, the decimal numbers aren't adding up
Because it's your project and not mine. I was just showing you the simplest way of determining how many digits of precision there are.
>why do you use int n = 0, it seems you don't use it in the function?
Leftovers from an older version.
>I understand this to be a selection, but I don't understand what it's doing?
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0. The equivalent is:
if ( s.length() == 1 )
return 0;
else
return s.length() - 2;
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
short amount (double val)
{
short a = 0, i, totvalue = 0;
ostringstream out;
out<< val;
string s = out.str();
for (i = 2; i < s.length(); ++i) // Number 1
{
totvalue += s[i] - '0'; // Number 2 & 3
}
return totvalue;
}
int main()
{
double a = 0.999;
cout<<"Value = " << amount(a) <<endl;
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I understand that using '0' (string value) is equal to the decimal amount of 48
No, it's equal to the integral value of '0'. Not everyone uses ASCII, and you would do well to remember that.
>we never declared an array
The string class overloads the subscript operator, so you can index a string just like you would an array.
>how does i get's the value of the digits
It doesn't. i is just an index into the string, where the values are held. I get the impression that you're confused because the value of i is equal to the next digit stored in the array. You may want to change the value of your floating-point value to avoid confusion. Try using 0.987 instead of 0.123.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>Could you explain this abit more Narue
Characters are actually small integers. When you say '0', you mean that you want the integer that represents '0' in the native character set. In ASCII, that value is 48. In other character sets, the value will likely be different, but you can still use '0' without knowing or caring what the integral value is.
>Because ASCII is limited you mean?
Because ASCII isn't the only character set available. For example, what happens when code you write that assumes ASCII is run on a system that uses EBCDIC? The program won't work properly because the integral values for EBCDIC are different than ASCII. That's why it's considered good practice to use the character representation instead of the integral value ('a' instead of 97 for example).
>And the string class is defined by --> ostringstream out;?
ostringstream is a completely different beast from basic_string. ;) basic_string is not a derived class, it stands alone. ostringstream derives from ostream and uses a basic_stringbuf instead of a basic_streambuf to do the dirty work.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>I still don't understand what this has got to do with my question concerning
>how the string class is defined in my small programm
Your question was confusing. ;)
>but still you speak about a string 'class' --> wich part determines the class
The class is defined for you when you include . It's a class from the standard C++ library, so all you need to do is create an object of the class:
#include <string>
std::string object;
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401