| | |
Adding decimal numbers together?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Hi ladies and gents,
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:
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 :!:
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:
C Syntax (Toggle Plain Text)
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; } return totvalue; } 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 :!:
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)
I'm at part 4 (classes and functions) :lol:
Strings is in part 6 ( Functions, strings en arrays)
•
•
•
•
Originally Posted by JoBe
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
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Hi Dave,
Changing the variable from float into double doesn't change anything, so, your hint towards: 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
Changing the variable from float into double doesn't change anything, so, your hint towards:
•
•
•
•
Then you may discover...
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
>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:
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:
C Syntax (Toggle Plain Text)
#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; }
New members chased away this month: 4
Hi Narue,
Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up, 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?
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 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 :!:
Thanks for the help, tough, why is it that in your example, the decimal numbers aren't adding up,
•
•
•
•
cout<< precision ( 0.123456 ) <<endl;
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 added this piece of code into the program that I wrote
•
•
•
•
if ( n >= 1){ a = n; n -= a; a = 0;}
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:
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:
C Syntax (Toggle Plain Text)
if ( s.length() == 1 ) return 0; else return s.length() - 2;
New members chased away this month: 4
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
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?
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
C Syntax (Toggle Plain Text)
#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; } return totvalue; } 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?
![]() |
Similar Threads
- Adding binary numbers (C++)
Other Threads in the C Forum
- Previous Thread: Clear the Screen using WinApi functions
- Next Thread: Windows and Overlapped Serial Port Communications
Views: 5850 | Replies: 18
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






