Unable to Multiply Decimals?

Hello!

This is my first thread on here but I hope to be posting a lot more because C++ has become very interesting and I have many projects in mind.

Now on to my question....

I am creating a simple console app (as my first project) that is just a simple cash accounting program. It uses multiple files to store numerical currency data which can be appended or read as needed. Everything seems to work fine BUT when I try to multiply a numerical variable by a decimal number, it just returns a 0.

The example I will show is for when a user will be depositing pennies into the account. The program asks for number of pennies, takes the input and adds it to whatever value was stored in the .dat file, replaces the old amount with new, and is supposed to display how much was added to the account. (This is where the issue comes up, everything else works perfectly, only the multiplying is off)

And I should mention that when a whole number is used, like 2, the program multiplies it correctly, only decimal numbers.

I understand its a bit messy but I am just trying to get it to work properly before cleaning it up :P

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
char depopen()
{
    int penum;
    int pendepsta;
    int pendepfin;
    int pendepcalc;

    if(system("CLS"));
    cout<<"\n";
    cout<<" Enter Number of Pennies to Deposit\n\n";
    cout<<" Number of Pennies: ";
    cin>> penum; //Where # of pennies is entered
    cin.ignore();
    ifstream apenfil ("pen.dat");
    apenfil>> pendepsta; //opening file to retrieve numerical variable
    apenfil.close();
    pendepfin=(penum+pendepsta); //<<Formula works correctly, where vars are added to form new
    ofstream bpenfil ("pen.dat", ios::trunc);
    bpenfil<< pendepfin; //insertion of new numerical value
    bpenfil.close();
    pendepcalc = (0.01* penum); //Where I want numerical input to be multiplied by .01
    cout<<"\n\n Added $";
    cout<< pendepcalc; // where I would like amount added to be displayed
    cout<<" to your Account.\n";
    cin.get();
    depo();
    return (0);
}


Thanks for any help! I just could not seem to find an answer online so maybe I am neglecting some simple C++ concept but idk... :P
pendepcalc = (0.01* penum);

pendepcalc is an int, so it can only store whole numbers, and the closest number to 0.01*penum is zero. Make pendepcalc a float rather than an int
there's some other wierd shit in here too, if(system("CLS"));

without even getting into why you shouldn't use system() calls, what is that line supposed to do? The function has a return type of char and returns 0 every time?

Have a read of this site's tutorials and get a grasp of the language, they're quite good.
I just needed something to clear the screen without just putting a lot of "\n"s

sorry, I just have a basic understanding and I hoping to continue to learn, which apparently i need too!

Thanks!!
Quirky, getting into it even more:
There's a ; behind the line, so the whole point of an if is beaten.
ok so I changed it to system("CLS"); and all of the "char"s to "int"s

anything else? lol the tutorial I used apparently doesn't know what it is talking about.

All of your corrections are very helpful though. Thank you!
Do you know of a way to open a file for input AND output so I dont have to open, close, reopen, and close again as I have done? But I still need all data erased before I enter new value
Instead of ifstream and ofstream, just use an fstream.
Topic archived. No new replies allowed.