Author Topic: [SOLVED] Unable to Multiply Decimals  (Read 6650 times)

Offline Pantheon08

  • Single posting newcomer
  • *
  • Posts: 2
[SOLVED] Unable to Multiply Decimals
« on: December 25, 2010, 12:15:39 am »
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

Code
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
« Last Edit: December 25, 2010, 12:56:42 am by Pantheon08 »

Offline Pantheon08

  • Single posting newcomer
  • *
  • Posts: 2
Re: Unable to Multiply Decimals
« Reply #1 on: December 25, 2010, 12:22:37 am »
RESOLVED: changes int pendepcalc from int to float
« Last Edit: December 25, 2010, 12:56:18 am by Pantheon08 »

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: [SOLVED] Unable to Multiply Decimals
« Reply #2 on: December 25, 2010, 07:52:34 am »
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.

Please do not post such questions in thies forum/on this site.

This website/forum is dedicated to developping C::B and using C::B only.
It's not a website that teaches basic programming or solves programming problems.

If you have a question that is related to C::B feel fre to ask here, but have in mind, that C::B is not a compiler "just" an IDE.