FUNCTIONS

Hi there,
I am trying to learn the concept of a function but looking for information on the web and my notes have made me more confused than ever!

I know that functions combine like programs together so it saves on the amount of work you have and of course the line space. My question would be........ what would a function look like if I were to combine 3 programs together. More specifically if I had 3 individual programs and the user gets to choose one of the programs he/she wants to use. How would I format this - function wise?
Could I use the if statment in this case scenario?
Yes those articles helped clarify a little bit,
so to summarize what is needed in a function is global variables -->specific variables (to carry out the instructions) --> instructions

So here is my program where I let the user choose which program they would like to run.
*/
#include <iostream>
#include<iomanip>

using namespace std;
int Users_Choice (int 1,int 2,int 3)
{
Return (1,2,3)
}
int main ()
{
cout<<”Which function do you want to call ?/n”;
cout<<” 1. Lab Activity 2, Program 1”;
cout<<”2. Lab Activity 2, Program 2”;
cout<<”3. Lab Activity 2, Program 3”;
cout<<”Enter your choice here : “;
cin>>Users_choice;

if (Users_Choice==1)
{
char initial 1;
char initial 2;
int number1;
int number2;
int number3;
cout<<”Please enter your first initial =”;
cin>>initial1;
cout<<”Please enter your last initial =”;
cin>>initial2;
cout<<”Please enter your month of birth (1-12) =”;
cin>>number1;
cout<<”Please enter your date of birth =”;
cin>>number2;
cout<<”Please enter the year you were born =”;
cin>>number3;
cout<<”\n”;
cout<<”\n”;
cout<<”\n”;
cout<<”\n”;
cout<<”\n”;
cout<<”My first initial is”<<setw(10)<<initial1;
cout<<”\n”;
cout<<My last initial is”<<setw(10)<<initial2;
cout<<”\n”;
cout<<”My month of birth is =”<<setw(7)<<number1;
cout<<”\n”;
cout<<”My date of birth is =”<<setw(8)<<number2;
cout<<”\n”;
cout<<”My year of birth is =”<<setw(8)<<number3;
cout<<”\n”;
return 0;
}
If(Users-Choice==2)
{
float number1;

cout<<”Enter a number =”;
cin>>number1;
cout<<”\n”;
cout<<”\n”;
cout<<”\n”;
cout<<”The number with no decimal places set is=”<<setw(16)<<number1;
cout<<”\n”;
cout<<”The number with 0 decimal places set is =”<<setw(17)<<setprecision(0)<<fixed<<number1;
cout<<”\n”;
cout<<”The number with 2 decimal places set is=”<<setw(16)<<setprecision(2)<<fixed<<number1;
cout<<”\n”;
cout<<”The number with 4 decimal places set is =”<<setw(17)<<setprecision(4)<<fixed<<number1;
cout<<”\n”;

return 0;
}
If (Users_Choice==3)
{
float number1;
int number2;
int number3;

cout<<”Enter a number =”;
cin>>number1;
cout<<”Enter the field width =”;
cin>>number2;
cout<<”Enter the precision =”;
cin>>number3;
cout<<”\n”;
cout<<”The number with a field width of”<<number 2<<”and”<<number3;
cout<<”decimal places is”<<setw(number2)<<setprecision(number3)<<fixed<<number1;
cout<<”\n”;

return 0;
}
}
Would this be correct?
keep main() smal :p, use functions after every if statement or smth


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main() // Main asks which prog you want to execute
{
	int uc;
	cin >> uc;

	if(uc == 1) 
	{
		 prog1()//excutes the prog which does bla bla
	}

	if(uc == 2)
	{
		 prog2()//excutes the prog which does doremi
	}

	if(uc == 3)
	{
		 prog3()//excutes the prog which does 1+3
	}

}


I would make it like that, explaining what each function does behind it, so if you'd want to edit your code a few months later, you can easely find what it does by just reading main()
+ main isn't oversized , so it's quickly read

Hope this helped ^^

(ps I'm playing with c++ for just a month, so if some1 corrects me, beleive him :D)
Negotiating a consumer loan is not always straightforward. One form of the loan is the discount installment loan, which works as follows.

Suppose a loan has a face value $1,000.00, the interest rate is 15%, and the duration is 18 months. The interest rate is computed by multiplying the face value of $1,000.00 by 0.15 to yield $150.00. This figure is then multiplied by the loan period of 1.5 years to yield $225.00 as the total interest owned. The amount is immediately deducted from the face value, leaving the consumer with only $775.00.

Repayment is made in equal monthly installment based on the face value. Hence, the monthly loan payment will be $1,000.00 divide by 18, which is $55.56. This method of calculation may not be too bad if the consumer needs $775.00 dollars, but the calculation is a bit more complicated if the customer needs $1000.00.

Write a program that will take three inputs:
- the amount the consumer needs to receive
- the interest rate
- the duration of the loan in months
The program should then calculate the face value required in order for the consumer to receive the amount needed. It should also calculate the monthly payment. Your program should allow the calculations to be repeated as often as the user wishes.




//I have the mathematical view to this and i wrote this code...but i was told functions is needed..i run it and not working..what should i do??

#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
double amount_needed,face_value,interest_rate,rate,loan_period, months;
double total_interest_owned,monthly_payment;
int const year=12,percent_decimal = 100;
char answer;


do
{
cout<<"The amount needed is:$ ";
cin>>amount_needed;
cout<<"The interest rate is : ";
cin>>interest_rate;
interest_rate=rate/100;
cout<<"The duration of the loan in months : ";
cin>>loan_period;
loan_period=months/12;

face_value=amount_needed/1-(interest_rate*loan_period);
monthly_payment=face_value/months;

cout<<"total face_value is:$ "<<face_value<<endl;
cout<<"monthly payment is: "<<monthly_payment<<endl;

cout << "Do you want to do it again?\n";
cout << "Press y for yes, press n for no: ";
cin >> answer;
}


while (answer == 'y' || answer == 'Y');

cout << "Goodbye!\n";


return 0;

}







what does return (1, 2, 3); do?
what does int Users_Choice (int 1,int 2,int 3) do?
@whiisper2uall: why are you posting this in some1 else's thread ? :P, questions like these atleast require one of its own :p

Logical answer to your question: "Fix it"

Assuming that's not what you wanted to hear, I'll give a try at explaining your errors and showing my "Fixed version" :)



Reason of error : using variables which don't have a value

others:
-making variables which aren't being used
-Way to long variable names (imo)
-Hard to read ( to compressed, use some white lines :P)
-You said you wanted functions but aren't using any (except main() )


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
using namespace std;

int main()
{
double am, interest,face,mp;// am =amount_needed , face = face_value , interest = interest_rate,mp = monthly_payment
int dur;// = loan_period
char answer;

	do
	{
		cout << "The amount needed: ";
		cin >>am;

		cout << "\nInterest rate: ";
		cin >> interest;

		cout << "\nDuration of loan (in months): ";
		cin >> dur;


		face = am/1 - (interest * dur);
		mp = face/dur;


		cout << "Total face val = " << face << endl;
		cout << "monthly payment = " << mp << endl;

		cout << "Do you want to do it again?\n";
		cout << "Press y for yes, press n for no: ";
		cin >> answer;

	}
	while (answer == 'y' || answer == 'Y');

		cout << "Goodbye!\n";

return 0;
}


Truly questioning your math formula, but this would be your code when it's simplified/working :)
Hope it somehow helped
thanks ...i will work on it...can u help me to fix in functions?
That would be this one :)
Though in this case I think the first on would be better readable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
using namespace std;

//prototypes
double face (double a, double b, int c); // face value calculator
double mp (double a, int b); // monthly payment calculator

int main()
{
double am, interest,y;// am =amount_needed , face = face_value , interest = interest_rate,mp = monthly_payment
int dur;// = loan_period
char answer;

	do
	{
		cout << "The amount needed: ";
		cin >>am;
	
		cout << "\nInterest rate: ";
		cin >> interest;

		cout << "\nDuration of loan (in months): ";
		cin >> dur;

		cout << "Total face val = " << face (am, interest, dur) << endl;
		cout << "monthly payment = " << mp(face (am, interest, dur),dur) << endl;

		cout << "Do you want to do it again?\n";
		cout << "Press y for yes, press n for no: ";
		cin >> answer;
	}
	while (answer == 'y' || answer == 'Y');

		cout << "Goodbye!\n";

return 0;
}

double face (double a, double b, int c)//function for face value calc
{
	return a/1 - (b*c);
}

double mp (double a, int b)//function for monthly calc
{
	return a/b;
}


I think this works :D
Topic archived. No new replies allowed.