Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Divinding doubles

Posted on 2002-04-17
7
Medium Priority
?
208 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

618 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