Solved

Divinding doubles

Posted on 2002-04-17
7
201 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

920 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

13 Experts available now in Live!

Get 1:1 Help Now