Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I need to convert this code from python to C++

What I have tried:

#constants declaration
DOWN_PAYMENT_RATE = 0.10
ANNUAL_INTEREST_RATE = 0.12
MONTHLY_PAYMENTS_RATE = 0.05

#prompt the user to enter the purchase price
purchasePrice = float(input("Enter the purchase price: "))

#The month number (beginning with 1)
month = 1

#The payment for that month
payment = purchasePrice * MONTHLY_PAYMENTS_RATE

#The current total balance owed
startingBalance = purchasePrice

#print the table heading
print("\n%s%19s%18s%19s%10s%17s" % ("Month", "Starting Balance", "Interest to Pay",
                                    "Principal to Pay", "Payment", "Ending Balance"))

#repeat the loop as long as the startingBalance is greater than 0
while startingBalance > 0:

    #The interest owed for that month
    interestToPay = startingBalance * ANNUAL_INTEREST_RATE / 12

    #The amount of principal owed for that month
    principalToPay = payment - interestToPay

    #The balance remaining after payment
    endingBalance = startingBalance - payment

    #print the statistics
    print("%2d%16.2f%16.2f%18.2f%18.2f%15.2f" % (month, startingBalance, interestToPay,
                                                 principalToPay, payment,endingBalance))

    #update the starting balance
    startingBalance = endingBalance

    #increment the month by 1
    month = month + 1
Posted
Updated 1-Apr-21 20:03pm
Comments
Dave Kreskowiak 1-Apr-21 18:17pm    
Simple. Understand what the code does and write equivalent code in C++.
Rheana Hagigal 1-Apr-21 18:45pm    
Could you please assist me in doing so
Dave Kreskowiak 1-Apr-21 19:42pm    
I'm not going to tell you anything beyond what the comments in the Python code are going to tell you. There's comments for nearly every line of code, so understanding it should not be a problem.
Rick York 1-Apr-21 19:08pm    
You need to find a code conversion service. This site is not one.

1 solution

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900