Need help with short program

This is a discussion on Need help with short program within the C++ Programming forums, part of the General Programming Boards category; Hello. In this short program I am calculating the amount of money a person spent paying a product off over ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Need help with short program

    Hello. In this short program I am calculating the amount of money a person spent paying a product off over x months.

    Code:
    #include<iostream>
    using namespace std;
    
    int main()
    
    {
        double amount, totalPay;
        int num, months;
        
        
        cout << "Enter how many months it took you to pay off the product: ";
        cin >> months;
        
        for(num = 1; num <= months; num++)
        {
                cout << "\nMonth " << num << ": \n" << "\tHow much did you pay? $";
                cin >> amount;
                
                totalPay = amount * num;
                
        }
                
        cout <<  "\nYou payed $" << totalPay << "\n" << endl;      
        
        
        return 0;
        
    }
    The problem I am having is I don't know how to calculate the amount payed over the x months. Like for an example, if the user enters 3 months how do I get the amount payed for all of those 3 months. With my code right now I am only getting the amount for the last month, and not all 3. No matter what number I enter for months, I only get the last month's amount.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Initialize totalPay to 0 and then add amount * num to totalPay in the loop instead of straight assigning amount * num to totalPay like you're doing now.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by itsme86 View Post
    Initialize totalPay to 0 and then add amount * num to totalPay in the loop instead of straight assigning amount * num to totalPay like you're doing now.
    Well I already have totalPay = num * amount in the loop, and when I assign totalpay to 0 it still gives me the same result. :/

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Read what I suggested again. You're not adding amount * num to totalPay. Hint: Use the '+' or '+=' operator.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by itsme86 View Post
    Read what I suggested again. You're not adding amount * num to totalPay. Hint: Use the '+' or '+=' operator.
    Well I think my code is wrong then because when I do
    Code:
    totalPay += amount * num;
    and say I enter 3 months and $5, $10, and $15 dollar for months, I end up with a value of $70 when it should be $30.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Well that's because you're multiplying it by the number of months. You should really only be adding the amount for that month. You want:

    totalPay = 0
    month 1: 5
    totalPay = 5
    month 2: 10
    totalPay = 15
    month 3: 15
    totalPay = 30

    So what are you doing each time? You're adding the amount you paid that one month to totalPay: totalPay += amount
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    Quote Originally Posted by itsme86 View Post
    Well that's because you're multiplying it by the number of months. You should really only be adding the amount for that month. You want:

    totalPay = 0
    month 1: 5
    totalPay = 5
    month 2: 10
    totalPay = 15
    month 3: 15
    totalPay = 30

    So what are you doing each time? You're adding the amount you paid that one month to totalPay: totalPay += amount
    oh wow thanks. I was using num because I thought it would need to keep track of how many months the user picks. Didn't realize you didn't need to do that.

    Appreciate your help a lot.

    EDIT: Also not sure why I had so much trouble with this. I just learned "keeping a running total" not that long ago.
    Last edited by theCanuck; 03-24-2011 at 01:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program help :>
    By n2134 in forum C Programming
    Replies: 9
    Last Post: 02-06-2010, 12:12 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 10:17 AM
  4. Help in explaining this short program
    By sturm100 in forum C Programming
    Replies: 22
    Last Post: 05-30-2002, 06:06 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21