# I have few c++ programming problems

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

1. ### seanjohni like to lurk, so DIAFOT Supporter

Joined:
Oct 14, 2004
Messages:
1,765
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. ### skinjobActive Member

Joined:
Jan 6, 2001
Messages:
2,337
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. ### seanjohni like to lurk, so DIAFOT Supporter

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

4. ### seanjohni like to lurk, so DIAFOT Supporter

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

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

and it works

5. ### CodeXGuest

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. ### seanjohni like to lurk, so DIAFOT Supporter

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

// 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. ### CodeXGuest

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. ### Rodthrower18New Member

Joined:
Jun 8, 2005
Messages:
2,447
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?

10. ### Rodthrower18New Member

Joined:
Jun 8, 2005
Messages:
2,447
0
Location:
Dallas
Cal 3 is wat killed me, that and trying to program for cal based physics

11. ### Sexual VanillaNew Member

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

12. ### deusexaetheraOT Supporter

Joined:
Jan 27, 2005
Messages:
19,712
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. ### seanjohni like to lurk, so DIAFOT Supporter

Joined:
Oct 14, 2004
Messages:
1,765
0

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;
}