I have few c++ programming problems

Discussion in 'OT Technology' started by seanjohn, Oct 10, 2007.

  1. seanjohn

    seanjohn i like to lurk, so DIAF OT Supporter

    Joined:
    Oct 14, 2004
    Messages:
    1,765
    Likes Received:
    0
    im not asking you to do my homework because most of it is already done, i just need some insight on what im doing wrong so that i can fix it.

    this is my program so far: (pls excuse the comments, the prof wants them there)

    // Purpose: This program calculates and prints the factorial of a number
    //----------------------------------------------------------
    #include<iostream>
    usingnamespace std;
    int Factorial(/* in */int n); // Purpose: This function computes the factorial of a number 'n'
    // Precondition: n is assigned an integer value
    // n >= 0 and n! <= INT_MAX
    // Postcondition: Returns the factorial of n

    int main()
    {
    int number;
    cout << "*** FACTORIAL ***\n";
    cout << "-----------------\n";
    cout << " \n";
    cout << "Enter a non-negative number whose factorial is to be computed: ";
    cin >> number;

    **********cout << "The factorial of " << number << " is " << Factorial(int n) << endl;
    return 0;
    }

    int Factorial(int n)
    {
    int result; //hold partial products
    result = 1; //initialize
    while (n > 0) //while the value of n is positive
    {
    result = result * n;
    n--;
    }
    return result; //return the calculated factorial of n
    }

    the line with all the asterisks has all the problems:
    error c2660: 'Factorial' : function does not take 0 arguments
    error c2144: syntax error: 'int' should be preceded by ')'
    error c2059: syntax error: ')'

    i have another program that also highlights the use of function calls and also has the same problems. tried looking in the textbook but it has a very short section on function calls.

    is it something to do with my preconditions?
    any help would be nice.
    thanks.
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    error c2660: 'Factorial' : function does not take 0 arguments


    **********cout << "The factorial of " << number << " is " << Factorial(int n) << endl;

    You're calling your Factorial function without a proper argument
     
  3. seanjohn

    seanjohn i like to lurk, so DIAF OT Supporter

    Joined:
    Oct 14, 2004
    Messages:
    1,765
    Likes Received:
    0
    im sorry, im a complete noob with c++ and im not quite sure what you mean.
     
  4. seanjohn

    seanjohn i like to lurk, so DIAF OT Supporter

    Joined:
    Oct 14, 2004
    Messages:
    1,765
    Likes Received:
    0
    oops, nevermind. i changed:

    cout << "The factorial of " << number << " is " << Factorial(number) << endl;

    and it works
     
  5. CodeX

    CodeX Guest

    You should be calculating the factorial recursively anyway

    Code:
    unsigned int factorial(unsigned int n)
    {
       if(n <= 1) return n;
       else return factorial(n-1) * n;
    }
    
    lol, confused yet?
     
    Last edited by a moderator: Oct 10, 2007
  6. seanjohn

    seanjohn i like to lurk, so DIAF OT Supporter

    Joined:
    Oct 14, 2004
    Messages:
    1,765
    Likes Received:
    0
    ^^ lol, thanks.

    how about this one:

    // Purpose: This program will calculate how long it will take to pay a loan
    // when only the minimum payment is paid and how much the total
    // interest payment is.
    //------------------------------------------------------------------------

    #include<iostream>
    #include<conio.h>
    usingnamespace std;

    int main()
    {
    double loanAmount; //amount of the loan
    double principlePay; //amount to pay down principle after interest
    double annualInterest; //annual interest
    double monthlyInterest; //monthly interest
    int minPay; //minimum monthly payment
    int howManyMonths; //time it takes to pay the loan
    double totalInterest; //total interest paid
    double interestPay; //amount of interest paid each month
    double totalPaid; //total amount paid

    //set up floating point with 2 digits after the decimal
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    //initializing variables
    totalInterest = 0;
    howManyMonths = 0;

    cout << "PAYING A LOAN\n";
    cout << "-------------\n";
    cout << " \n";

    //prompting user for useful info
    cout << "Enter the amount of the loan: $";
    cin >> loanAmount;
    cout << " \n";
    cout << "Enter the annual percent interest rate of the loan: ";
    cin >> annualInterest;
    cout << " \n";

    monthlyInterest = annualInterest / 12;
    cout << "Enter the minimum payment amount: $";
    cin >> minPay;
    cout << " \n";

    while (loanAmount >= 0) //checking loop control
    {
    interestPay = loanAmount * monthlyInterest;
    loanAmount = loanAmount - (minPay - interestPay);

    totalInterest = totalInterest + interestPay;
    howManyMonths = howManyMonths + 1;
    }

    totalPaid = loanAmount + totalInterest;
    cout << howManyMonths << endl;
    cout << totalPaid << endl;
    getch();
    return 0;
    }


    the build goes fine, but the debug has nothing happen after i punch in the minimal payment.
     
  7. CodeX

    CodeX Guest

    It worked as is, you need to enter the percent interest as 0.06 for 6%... when I first ran it I entered 6 for 6 percent and that will actually increase the amount you owe faster than you pay it off, making it infinite loop.

    Additionally, you are not calculating totalPaid right... you are adding totalInterest to loanAmount which seems fine, the only problem is after that loop loanAmount = 0... you need to make another variable to hold loanAmount constant through that loop so it can be added to totalInterest at the end
     
  8. Rodthrower18

    Rodthrower18 New Member

    Joined:
    Jun 8, 2005
    Messages:
    2,447
    Likes Received:
    0
    Location:
    Dallas
    This takes me back, why didnt i stick with this so i can get paid to do this vs analyzing reroutes and processing ics payments all day? :hsd:
     
  9. CodeX

    CodeX Guest

  10. Rodthrower18

    Rodthrower18 New Member

    Joined:
    Jun 8, 2005
    Messages:
    2,447
    Likes Received:
    0
    Location:
    Dallas
    :rofl: Cal 3 is wat killed me, that and trying to program for cal based physics:bowrofl:
     
  11. Sexual Vanilla

    Sexual Vanilla New Member

    Joined:
    May 23, 2005
    Messages:
    6,305
    Likes Received:
    0
    Location:
    South Carolina
    Got a good taste of many of those topics in my undergrad Numerical Analysis class...scary shit.
     
  12. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    When you call Factorial, you shouldn't create a new "n" variable inside the parentheses. Just give it the "n" variable you've already created.
     
  13. seanjohn

    seanjohn i like to lurk, so DIAF OT Supporter

    Joined:
    Oct 14, 2004
    Messages:
    1,765
    Likes Received:
    0
    hey guys, i really do appreciate all the help you given. :)

    this is the final code of the second program if anyone cares:

    // Purpose: This program will calculate how long it will take to pay a loan
    // when only the minimum payment is paid and how much the total
    // interest payment is.
    //------------------------------------------------------------------------

    #include<iostream>
    #include<conio.h>
    using namespace std;

    int main()
    {
    double loanAmount; //amount of the loan
    double amountLeftToPay; //amount left to pay
    double annualInterest; //annual interest
    double monthlyInterest; //monthly interest
    int minPay; //minimum monthly payment
    int howManyMonths; //time it takes to pay the loan
    double totalInterest; //total interest paid
    double interestPay; //amount of interest paid each month
    double totalPaid; //total amount paid

    //set up floating point with 2 digits after the decimal
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    //initializing variables
    totalInterest = 0;
    howManyMonths = 0;

    cout << "PAYING A LOAN\n";
    cout << "-------------\n";
    cout << " \n";

    //prompting user for useful info
    cout << "Enter the amount of the loan: $";
    cin >> loanAmount;
    cout << " \n";

    amountLeftToPay = loanAmount;

    cout << "Enter the annual percent interest rate of the loan: ";
    cin >> annualInterest;
    cout << " \n";

    monthlyInterest = (annualInterest / 12) / 100;
    cout << "Enter the minimum payment amount: $";
    cin >> minPay;
    cout << " \n";


    while (amountLeftToPay >= 0) //checking loop control
    {
    if (loanAmount == amountLeftToPay)
    {
    interestPay = loanAmount * monthlyInterest;
    amountLeftToPay = loanAmount - (minPay - interestPay);
    }
    else
    {
    interestPay = amountLeftToPay * monthlyInterest;
    amountLeftToPay = amountLeftToPay - (minPay - interestPay);
    }

    totalInterest = totalInterest + interestPay;
    howManyMonths = howManyMonths + 1;
    totalPaid = loanAmount + totalInterest;
    }

    cout << "For the initial loan amount of $" << loanAmount << ",\n";
    cout << "it will take " << howManyMonths << " months to pay the loan in full\n";
    cout << "with the total of $" << totalPaid << " paid over the life of the loan.\n";
    cout << " \n";
    getch();
    return 0;
    }
     

Share This Page