I'm trying the next exercise:

Write a function value, wich adds the decimal numbers of 'n' together and return the result of this into main? For example:

0.1234 = 10
0.01234 = 10
0.3286 = 19

What Ive got so far is this:

``````short amount (float n)
{
short a = 0, i, totvalue = 0;

for (i = 0; i < sizeof (n); i++)
{
a = n = (n * 10);
n -= a;
totvalue += a;
}

}

int main()
{
double a = 0.1234;

cout<<"Value = " << amount(a) <<endl;

return 0;
}``````

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

Also, is it possible to change the working of the loop using binary operators?

Thanks for the help :!:

## All 18 Replies

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!

Thanks for the reply Vegaseat, but untill this part of exercises, there is no mentioning of using your examples in the piece of the book that Ive done yet, I'll have to go about 60 pages further before the explanation of strings is mentioned in detail so that I could try what you are suggesting :)

I'm at part 4 (classes and functions) :lol:
Strings is in part 6 ( Functions, strings en arrays) ;)

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 a double, 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.

Hi Dave,

Changing the variable from float into double doesn't change anything, so, your hint towards:

Then you may discover...

isn' found?

I understand what your saying about sizeof(), problem is, I don't have a clue as to how to change this towards the amount of numbers behind the 0,... ( don't know the name of ',' in English)

I think (almost certain) that I have to find a way to determine how many numbers are behind the ',' problem is, I don't know how I can do that :confused:

>don't know the name of ',' in English

>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;
}``````

Hi Narue,

Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up,

cout<< precision ( 0.123456 ) <<endl;

This for example shows as result 6 and not 21?

Also, why do you use int n = 0, it seems you don't use it in the function?

return s.length() == 1 ? 0 : s.length() - 2;

I understand this to be a selection, but I don't understand what it's doing?

I added this piece of code into the program that I wrote

if ( n >= 1){ a = n; n -= a; a = 0;}

This is because previously when I entered a number like 1,1234 it didn't get rid of the number before the radix.
Not that it matters since it seems I'm not going to solve this exercise with the code I wrote :-|

I see you, Dave and Vegaseat became moderators, very smart from Dani to do this, you guys are a real asset towards this C++ community, congrats to you all :!:

>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;``````

>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.
Got it, thanks for the help Narue :!:

Hi Narue,

Ive been trying to get this exercise to work with the piece of code you gave me, but I'm stuck :!:

Ive put numbers 1, 2 & 3 left towards the code that I have trouble with

``````#include "stdafx.h"
#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 = 0; i < s.length(); ++i) // Number 1
{
a = s * 10; // Number 2
s = s - a; // Number 3
totvalue += a;
}

}

int main()
{
double a = 0.999;

cout<<"Value = " << amount(a) <<endl;

return 0;
}``````

1) I believe I can use this to determine the amount of digits behind the radix, correct?

2 & 3) when using this code, I get the following error C2676: binary '*' or '-': 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' does not define this operator

I understand it's not recognizing this operator, but, how can I get it to recognize it?

``````#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
}

}

int main()
{
double a = 0.999;

cout<<"Value = " << amount(a) <<endl;

return 0;
}``````

Thanks for the help again Narue, got a few questions about it.

``for (i = 2; i < s.length(); ++i)``

Could you tell me why i has to be 2?
It's probably got to do with:
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0.

But, I actually don't understand why this is needed :?:

``totvalue += s[i] - '0'``

I understand that using '0' (string value) is equal to the decimal amount of 48 , so, when detracting the digit for instance '1' - '0' it get's the value 1, I understand that!

But what I don't understand is that you are using an array wich is detached towards a string s, but, we never declared an array :o That's confusing for me Narue :confused:

I also still have the difficulty in seeing how a variable such as i, wich actually is just a counter, suddenly get's the values of the stringvalue.

Like 0.123

When i in the loop is 2

s = 1

When i in the loop is 3

s = 2

It's hard to explain what I'm trying to ask, but, how does i get's the value of the digits, does it have to do that i wich is used in the loop is not the same as i in s ?

Hope you understand.

Thanks for the help again Narue, got a few questions about it.

``for (i = 2; i < s.length(); ++i)``

Could you tell me why i has to be 2?
It's probably got to do with:
If there are no digits past the radix, subtracting 2 from the string length would give you -1 instead of the desired 0.

But, I actually don't understand why this is needed :?:

The above part is understood Narue, no further explanation is necessary!
Starting at 2, passes the 0 and . when looping the string, got that one ;)

``totvalue += s[i] - '0'``

I understand that using '0' (string value) is equal to the decimal amount of 48 , so, when detracting the digit for instance '1' - '0' it get's the value 1, I understand that!

But what I don't understand is that you are using an array wich is detached towards a string s, but, we never declared an array :o That's confusing for me Narue :confused:

I also still have the difficulty in seeing how a variable such as i, wich actually is just a counter, suddenly get's the values of the stringvalue.

Like 0.123

When i in the loop is 2

s = 1

When i in the loop is 3

s = 2

It's hard to explain what I'm trying to ask, but, how does i get's the value of the digits, does it have to do that i wich is used in the loop is not the same as i in s ?

Hope you understand.

>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.

>No, it's equal to the integral value of '0'.
Could you explain this abit more Narue, I tried to translate this into my language, but didn't get a good translation :!:

>..., and you would do well to remember that.
Because ASCII is limited you mean?

>The string class overloads the subscript operator, ...
And the string class is defined by --> ostringstream out;?

>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.

>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, Ive read this atleast 10 times and 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 :-|

I'll try and ask my question differently, I understand a class to be written like this

class somenameto use
{
...
};

But, that's not the case in the program, but still you speak about a string 'class' --> wich part determines the class :?:

>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

>but still you speak about a string 'class' --> wich part determines the class
The class is defined for you when you include <string>. 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;``````

:D I understand, sometimes it's difficult to write down exactly what I want to ask ;)

>The class is defined for you when you include <string>. 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;``````

Yep that's what the question was about Narue.

Thanks!

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.