Solved

Divinding doubles

Posted on 2002-04-17
7
200 Views
Last Modified: 2010-04-02
i have some code.. sorry i'm a newbie.. first time c++ programming...

I want to change the INTS into DOUBLES....
But doubles can't handle the % function...
DOes anyone know how to solve this???

#include <iostream.h>
#include <stdlib.h>
#include <math.h>

void main()
{
int hoeveelbetalen,methoeveelbetaald,wisselgeld;
cout << "Hoeveel moet u betalen? ";
cin >> hoeveelbetalen;
cout << "Met hoeveel geld betaalt u? ";
cin >> methoeveelbetaald;
wisselgeld = methoeveelbetaald - hoeveelbetalen;
if (wisselgeld < 0)
{
  cout << "" << endl;
  cout << "U heeft te weinig geld gegeven!" << endl;
  system("pause");
  return(0);
}
else
{
cout << " " << endl;
cout << "==========================================" << endl;
cout << "Totale wisselgeld: " << wisselgeld << endl;
cout << " " << endl;
cout << "Teruggegeven: " << endl;
cout << " " << endl;

   
   
    int vijfhonderd = wisselgeld / 500;
    wisselgeld = wisselgeld%500;
    int tweehonderd = wisselgeld / 200;
    wisselgeld = wisselgeld%200;
    int honderd = wisselgeld / 100;
    wisselgeld = wisselgeld%100;
    int vijftig = wisselgeld / 50;
    wisselgeld = wisselgeld%50;
    int twintig = wisselgeld / 20;
    wisselgeld = wisselgeld%20;
    int tien = wisselgeld / 10;
    wisselgeld = wisselgeld%10;
    int vijf = wisselgeld / 5;
    wisselgeld = wisselgeld%5;
    int twee = wisselgeld / 2;
    wisselgeld = wisselgeld%2;
    int een = wisselgeld / 1;
    wisselgeld = wisselgeld%1;
   
    if (vijfhonderd !=0)
    cout << vijfhonderd << " Vijfhonderdje(s)" << endl;
    if (tweehonderd !=0)
    cout << tweehonderd << " Tweehonderdje(s)" << endl;
    if (honderd !=0)
    cout << honderd << " Honderdje(s)" << endl;
    if (vijftig !=0)
    cout << vijftig << " Vijftigje(s)" << endl;
    if (twintig !=0)
    cout << twintig << " Twintigje(s)" << endl;
    if (tien !=0)
    cout << tien << " Tientje(s)" << endl;
    if (vijf !=0)
    cout << vijf << " Vijfje(s)" << endl;
    if (twee !=0)
    cout << twee << " Tweetje(s)" << endl;
    if (een !=0)
    cout << een << " Eentje(s)" << endl;

}
system("pause");
0
Comment
Question by:Blizzard2k
7 Comments
 
LVL 3

Expert Comment

by:Crius
ID: 6949022
A currency exchange program, yes?
Because it is currency, you can multiply everything by 100, or whatever the smallest currency unit is, and deal with whole numbers. I'd suggest this since it is faster and *more accurate* than dealing with decimal (double) values.

If you must deal with double values, you can make your own % (modulo) function since the current one only deals with whole numbers.

double modulo(double InStartValue, double InFactor)
{
   while(InStartValue>=0)
      InStartValue-=InFactor;
   return InStartValue+InFactor;
}
0
 
LVL 2

Accepted Solution

by:
LoungeLizard earned 50 total points
ID: 6949139
What you can also do to figure out the change, is to split the change into two:  One for guilders (I'm assuming you are Dutch) and one for cents and then process each separtely.

So you'd have something like

float hoeveelbetalen,methoeveelbetaald,wisselgeld;
....
wisselgeldguilders = (int) wisselgeld;
wisselgeldcents = (int)((wisselgeld - int(wisselgeld))*100);

Now you simply process each seperately and print out the results one after the other
0
 
LVL 2

Expert Comment

by:LoungeLizard
ID: 6949141
Sorry, that should be:

int wisselgeldguilders = (int) wisselgeld;
int wisselgeldcents = (int)((wisselgeld - int(wisselgeld))*100);
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 6

Expert Comment

by:snoegler
ID: 6950135
fmod()
0
 
LVL 16

Expert Comment

by:imladris
ID: 6950843
Note that in general, because of vagueries in the encoding of fractions in floating point numbers, it is better to use integers to represent money.
0
 

Author Comment

by:Blizzard2k
ID: 6950925
Good answer... simple but working very well...
Thank you.. dind't think of that yet!

Here are your points..
0
 
LVL 16

Expert Comment

by:imladris
ID: 6950951
Note that in general, because of vagueries in the encoding of fractions in floating point numbers, it is better to use integers to represent money.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now