I want to create a function which will assign the value of the struct array and it will determine the member of struct through its parameter.
I mean instead of creating seperate function for each member of the struct, determine the member through the function parameter(examples : &.tests, lessons.exams)
The Code ı wrote down is only for explain what I mean, values can be imported from a text file instead of assigning them by random.
What I want to understand is; is there any other way to call struct member without writing its name?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
struct lsn
{
char name[20];
int tests[4];
int quizzes[4];
int exams[4];
int finals[4];
};
void random_notes(lsn *x, int *y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
int main()
{
srand(time(NULL));
lsn lessons[30];
random_notes(lessons, &.tests);
random_notes(lessons, &.quizzes);
random_notes(lessons, &.exams);
random_notes(lessons, &.finals);
return 0;
}
Instead of creating 4 functions as below,
void random_tests(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].tests[j]=rand()%101;
}
void random_quizzes(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].quizzes[j]=rand()%101;
}
void random_exams(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].exams[j]=rand()%101;
}
void random_finals(lsn *x)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].finals[j]=rand()%101;
}
Just one function that determine the struct member throuh its parameter,
void random_notes(lsn *x, .struct_member y)
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
x[i].y[j]=rand()%101;
}
In this example the function is very small, but imagine a huge code in a fuction, only the struct member is different and rest of the code is same.
Yes, C++ has a concept of "pointers to members". This will allows you to pass the identity of the member you wish to initialize. The syntax is a bit wonky however, so beware:
void random_notes(lsn *x, int (lsn::* y)[4])
{
int i,j;
for(i=0;i<20;i++)
for(j=0;j<4;j++);
(x[i].*y)[j]=rand()%101; // << Access the member of x[i] via y
}
Which is to be called like this:
random_notes(lessons, &lsn::tests);
Pass a function which when called returns the appropriate struct member. For example:
random_notes(lessons, [=](lsn& lesson) { return lesson.quizzes; });
in random_notes function, you just call the function with a lsn instance and it will give you the array to populate
Related
I want to make a overloading function with a prototype in C++.
#include <iostream>
using namespace std;
int rectangle(int p, int l);
int main() {
cout << rectangle(3);
return 0;
}
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
I got error at
int rectangle(int p, int l);
is that possible make prototype with a overloading function? if possible how to do it
You've to declare the function before you use/call it. You did declare the 2 argument version of rectangle function but you seem to forget to declare the 1 argument taking version.
As shown below if you add the declaration for the 1 argument version then your program works(compiles).
#include <iostream>
using namespace std;
//declare the function before main
int rectangle(int p, int l);
int rectangle(int p);//ADDED THIS DECLARATION
int main() {
cout << rectangle(3);
return 0;
}
//define the functions after main
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
The output of the program can be seen here.
Alternative solution:
If you don't want to declare each function separately then you should just define them before main instead of declaring them as shown below.
#include <iostream>
using namespace std;
//define the functions before main. This way there is no need to write a separate function declaration because all definition are declarations
int rectangle(int p) {
return p*p;
}
int rectangle(int p, int l) {
return p*l;
}
int main() {
cout << rectangle(3);
return 0;
}
calculation() function is not working when m making input() function outside the class...has it got something to do with inline function??
#include <iostream>
using namespace std;
class price
{
public:
int pen;
int rubber;
int scale;
void input()
{
cout<<"enter the variables\n";
cin>>pen>>rubber>>scale;
cout<<"\n"<<pen<<" "<<rubber<<" "<<scale;
}
};
void calculate(price p)
{
int rate[2],total;
rate[0]=p.pen*5;
rate[1]=p.rubber*3;
rate[2]=p.scale*4;
total=rate[0]+rate[1]+rate[2];
cout<<"\n"<<total;
}
int main()
{
price a,b,c;
a.input();
calculate(a);
return 0;
}
No we don't. inline has no effect at all on the semantics of a C++ function. It's only effect is on how that function is treated by the linker.
I am trying to define a type for function pointers, in c++. However, once I define the pointer funcp as a fptr, I cannot redefine the pointer as a different function, times. This program does not even compile. Is this because I have to delete the nullptr before reassigning times to funcp?
My code:
#include <iostream>
using namespace std;
typedef int (*fptr)(int, int);
int times(int x, int y)
{
return x*y;
}
fptr funcp = nullptr;
funcp = times;
int main()
{
return 0;
}
The problem is that you are trying to do that outside of any function. Try that:
int main()
{
funcp = times;
return 0;
}
Your question has nothing special for C++17. To make it a little more modern and easier to read you may use the using instead of typedef:
using fptr = int (*)(int, int);
I just noticed something when creating functions. In the code:
#include <iostream>
using namespace std;
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
it will work because the function being called is on top of the caller, but if I put the function add() below the calling function in main() it won't work.
#include <iostream>
using namespace std;
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b = 20)
{
int r;
r = a + b;
return (r);
}
and the compiler will tell me that the identifier add() cannot be found.
so why do we declare functions anyway? like this:
#include <iostream>
using namespace std;
int add(int a, int b = 20);
int main()
{
int result;
result = add(20);
cout<<result;
return 0;
}
int add(int a, int b)
{
int r;
r = a + b;
return (r);
}
A definition is implicitly a declaration. And a declaration must come ahead of the use.
All functions need to be declared before they are used.
You can do that by either (1) writing a declaration, or (2) writing a definition.
Relying solely on (2) can be tempting but then you are bound to order your program in a particular way, and is occasionally impossible. For example, the following will not compile unless the comment is removed.
//void bar(int);
void foo(int n)
{
if (!n){
bar(n);
}
}
void bar(int n)
if (n){
foo(n);
}
}
int main()
{
foo(1);
}
No.
If the function definition appears before the function call, then prototype is not mandatory. Otherwise function prototype is necessary to let compiler know how to respond to a function when it is called.
A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
if the function definition appears after the function call then prototype is mandatory. because it tells the compiler to how to respond the function when it is called.
check the following example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int, int); // function prototype
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
and if the function definition is made before the function call then it is not mandatory to declare function prototype.
consider example.
/* C++ Function Prototype and C++ Function Definition */
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
int add(int x, int y) // function definition
{
int res;
res = x + y;
return res;
}
void main()
{
clrscr();
int a, b;
cout<<"Enter any two number: ";
cin>>a>>b;
cout<<"\nSummation = "<<add(a, b);
getch();
}
#include <iostream>
using namespace std;
void test(float, int);
int main()
{
const int size=11;
float a[size];
test(a, size);
return 0;
}
void test(float a[], int size)
{
[....]
}
it points to test(a, size); but I can't figure out whats wrong(I'm also learning coding and just learned about arrays/confused)
Your function prototype void test(float, int); does not match your function void test(float a[], int size). Change the prototype at the top to void test(float a[], int size); (I like to leave the input variable names in the prototype for consistency, but this is not necessary).
You probably meant to write:
void test(float*, int);
// ...
void test(float* a, int size)
{
[....]
}
when test is called with array argument, array will decay to pointer to its first element - and its size will be lost.
Wrong parameter type for the test forward deceleration.
Try this.
#include <iostream>
using namespace std;
void test(float *, int);
int main()
{
const int size=11;
float a[size];
test(a, size);
return 0;
}
void test(float a[], int size)
{
}