Solved

Divinding doubles

Posted on 2002-04-17
7
202 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Would like to move button in a function 3 76
thread-safe code in c++ 2 100
Issues with C++ Class 19 92
draw a Christmas tree by using a nested loop? 26 61
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

774 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