0

I'm making a "project" for university. Short program that will exchange any sum of money to coins, valued from 1 to 0,01 .

#include <iostream>

using namespace std;

int main()
{
    double C;
    int rest;
    cout << "Wpisz kwote w zl. " << endl;
    cin >> C;
    double c1=1 ,c2=0.75 ,c3=0.50 ,c4=0.20 ,c5=0.10 ,c6=0.05 ,c7=0.02 ,c8=0.01;
    int coin1=c1*100 ,coin2=c2*100 ,coin3=c3*100 ,coin4=c4*100 ,coin5=c5*100 ,coin6=c6*100 ,coin7=c7*100 ,coin8=c8*100;
    int ile1=0, ile2=0, ile3=0, ile4=0, ile5=0, ile6=0, ile7=0, ile8=0;
    int cash= C*100;
    double check;


        if (cash-coin1>=0){rest=cash%coin1;ile1= cash/coin1; cash = rest; }
        cout << cash << " " << 28083%coin1 ;
        if (cash-coin2>=0){rest=cash%coin2;ile2= cash/coin2; cash = rest; }
        cout << cash << endl;
        if (cash-coin3>=0){rest=cash%coin3;ile3= cash/coin3; cash = rest; }
        cout << cash << endl;
        if (cash-coin4>=0){rest=cash%coin4;ile4= cash/coin4; cash = rest; }
        cout << cash << endl;
        if (cash-coin5>=0){rest=cash%coin5;ile5= cash/coin5; cash = rest; }
        cout << cash << endl;
        if (cash-coin6>=0){rest=cash%coin6;ile6= cash/coin6; cash = rest; }
        cout << cash << endl;
        if (cash-coin7>=0){rest=cash%coin7;ile7= cash/coin7; cash = rest; }
        cout << cash << endl;
        check=ile1*c1+ile2*c2+ile3*c3+ile4*c4+ile5*c5+ile6*c6+ile7*c7+ile8*c8;

I know the structure itself is super messy, but I'm learning. I'll try to shorten it later and make it more clear. But that's not my problem.

The problem is that it looks like % operand doesn't work correctly.

When I put in value 280.83 for C, well, the program read cash%coin1 (coin1 being equal to 100 ) as 82. Where did the random 1 go ? Also, just to check, i wrote 28083%coin1 and the results were 837. Weird.

How can I fix this ?

edit:

double C=280.83, c1=1;
int cash = C*100, coin1=c1*100, ile1=0;
rest=cash%coin1;ile1= cash/coin1; cash = rest;

rest = 82. Should be 83.

Wiranek
  • 3
  • 1
  • 3
  • 1
    I can assure you with 100% certainty that there is nothing wrong with the modulo operator. Your understanding and usage of it are quite flawed though. When you come to realize this, debugging will become easier. What you are experiencing now is probably the biggest psychological hurdle for new programmers. – Mad Physicist Nov 21 '16 at 17:19
  • 2
    you also might want to read [this](http://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) – 463035818_is_not_an_ai Nov 21 '16 at 17:21
  • @FredLarson I think std::fmod() is probably the closest – Alfie J. Palmer Nov 21 '16 at 17:24
  • The language is called C++, not CPP (I've fixed the title for you). CPP is often used as an abbreviation for the C preprocessor. – Keith Thompson Nov 21 '16 at 17:28
  • @MadPhysicist I tried using fmod. Still, fmod(280.83,100) gives me out 80.837... Am I doing something wrong or not understanding something ? – Wiranek Nov 21 '16 at 17:30
  • @KeithThompson Thanks, I've always thought CPP is short for C Plus Plus, which seems same to me. – Wiranek Nov 21 '16 at 17:31
  • @Wiranek in mathematics, the remainder of the division (modulo operation) is only defined for integers. What do you expect to see when this concept is applied to real numbers? Hint: you might want to reduce your problem to integer arithmetic. – Ivan Aksamentov - Drop Nov 21 '16 at 17:33
  • @Wiranek: We often use `.cpp` as the file extension for C++ source files because punctuation characters in file names can cause problems on some systems. (`.cc` is another common choice.) – Keith Thompson Nov 21 '16 at 17:36
  • @Drop: In mathematics, remainders on division of real numbers can make perfectly good sense. For example, the remainder when dividing `2.0` by `1.5` is `0.5`. – Keith Thompson Nov 21 '16 at 17:38
  • @Drop I've multiplied all currencies by 100, to make sure they're integer... putting 280.83 as "C" will make it 28083 in "cash". While you put in the value for C and the actual value of coins are doubles, it's cash and coinX variables that are used, which are integer. – Wiranek Nov 21 '16 at 17:38
  • @KeithThompson Yes, if you enforce quotient to be an integer. However this kind of extensions is not a part of the classic arithmetic and is not included into typical CS syllabus, so mentioning this wouldn't help OP to solve his problem. – Ivan Aksamentov - Drop Nov 21 '16 at 17:41
  • @Wiranek So were you able to resolve the issue? Notice that now your implementation is also robust, as you will not loose cents when performing arithmetic operations (as it was in case of floats). – Ivan Aksamentov - Drop Nov 21 '16 at 17:43
  • @Drop No, because it's just that for some reason, it always reduces the actual value by 1 ! I've tried to get remainder by making it like this. rest=cash-(coin1*ile1) where coin1 is coin value and ile means how many times it legit appears in the cash. I'd think that again, for cash = 28083, coin1=100 and ile1=280 it'd give out 83. Nope, still 82. – Wiranek Nov 21 '16 at 17:44
  • @Wiranek you need to come up with a very simple example that demonstrates this problem (here we call it [MCVE](http://stackoverflow.com/help/mcve)). Remove all unnecessary code and write a 1-2 line long program that shows what happens. Edit your post and add it to the end. – Ivan Aksamentov - Drop Nov 21 '16 at 17:46
  • @Drop Like that ? – Wiranek Nov 21 '16 at 17:53
  • @Wiranek Yes. But you still have floating points there. Here is where you loose your money. – Ivan Aksamentov - Drop Nov 21 '16 at 18:05
  • @Drop I'm a newbie, I'm not sure what should I do about it. I mean, the c1 has to stay a double, because i'll put in an option for the person using the program to input your own coin values. But, we're not really using them for any actual math operations. For that, I've created cash, coin1 which are ints. int cash = C*100 I suppose this part is the problem ? Declaring the variable as int and setting it's value to a double*integer is wrong ? If it wasn't actually int, the program would've thrown me an error, since it seems doubles aren't cool with % operand. – Wiranek Nov 21 '16 at 18:13

0 Answers0