?
Solved

Divinding doubles

Posted on 2002-04-17
7
Medium Priority
?
207 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 150 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

770 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