Hello fellas, I'm having 1 final problem with my final project. We are asked to froup all the projects in one as functions, my problem is some of my projects (as in the code below) already include functions in them, so is it possible to use a function in another function? if so, how should I go about it? I have 2 other projects that include more functions so if you can address the problem beyond this particular program I would be greatfull. Thank you in advance.
#include<iostream>
#include<iomanip>
#include<string>
#include<cctype>
#include<cmath>
#include<stdlib.h>
usingnamespace std;
//Prototype of functions
void c(int,int);
void Probability();
//Main Program(User Prompt>input collect>function application>outputpresent)
int main()
{ while(cin)
{
int selection;
cout<<" 1) Calculate the Probabity of r from n.\n\n";
cout<<" 2) ***EXIT***\n\n";
cout<<" Select one of the following options:";
cin>>selection;
cout<<endl;
if(selection==3)
Probability();
elseif(selection==6)
break;
else
{
cout<<"Your selection is not applicable.\n\n";
system("pause");
}
system("cls");
}
return 0;
}
void probability()
{
int n,r;
cout<<"This program will calculate the number of ways you can\nselect a number of certain items from a group of total items...\n\n";
while(cin)
{
cout<<"Please enter the total number of items in the group: \n\n";
cin>>n;
n=abs(n);
cout<<"\nPlease enter the number of certain items in the group: \n\n";
cin>>r;
r=abs(r);
if(n<r)
{
cout<<"The number of (certain items) cannot \nexceed the (total) number of items in a group\n\n";
continue;
}
cout<<"\nYou can select the "<<"("<<r<<")"<<" certain items\nfrom the group of "<<"("<<n<<")"<<" items in \n--> (";
c(n,r);
cout<<") different ways.\n\n";
}
return 0;
}
//definitions of functions
longint factorial(int x)
{
if(x==0)
return 1;
elsereturn x * factorial(x-1);
}
void c(int n,int r)
{
cout<<(factorial(n))/((factorial(r))*(factorial(n-r)));
}
We are asked to froup all the projects in one as functions
Do you mean your instructor told you to use only one function? If so, that's.. kind of ridiculous. There should be no limit to how functional you make your program.
my problem is some of my projects (as in the code below) already include functions in them, so is it possible to use a function in another function?
Also unclear as to what you're asking in this statement. Is it possible to declare a function inside a function? It's not.
I think you have misunderstood what has been asked of you. Your prof wants you to integrate all of your previous assignments functionality into a final project. Not directly copy and paste the code in and build it.
So, you'll need to modify your main method to offer more options to the user, and call methods based on the options they pick etc.
Yes guys, sorry my statments were unclear. We need to get all the projects and make them in to functions in out project. But since I cant declare a function inside another function then I just have to include the factorial inside the function probability itself, I couldntmake it work, would be helpful if you give me any ideas as to how I can do that. In my other project(function body) The cin.getline(x,y) dont seem to work, is it not suppose to work when its in a function?
I believe C/C++ language does not allow you to define a function within a function. Other languages do support like JavaScript (a very dynamic and loosely-typed language).
You are confusing yourself on what you need to achieve. You need to make a single project that offers the user all of the functionality you've been building in all of your previous work.
There is no need to "Declare a function within a function". You declare each of your function prototypes above you main method as per normal. Then write the methods. You need to add more user input options in your main method to cater for the new functionality.
Your code above doesn't work because you haven't put a function prototype for longint factorial(int x) above your main method with the other prototypes.