display_it... what does it do exactly?

I don't exactly understand this "display_it" command. I get that it's used as "display_it(double,int)" but I don't understand its purpose. I got it from part of a homework assignment...

1) An example of a void function is a function that takes a real value and displays it with a specific number of decimal points.
void display_it(double x, int precision)
{
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout.precision(precision);
      cout << x << endl;
}
Use overloading to write a function that displays two real values with a specific precision.
Turn in the program and its output.


Well I'm using dev, so I had to change it a little because it wouldn't compile...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

int main()
{
    double x;
    void display_it(double x=3.14159265358979323846, int precision);{
      cout.precision(3);
      cout<<x<<endl;
} 
    system("PAUSE");
    return 0;
}


and I mean it does what it's supposed to do (I think >.>) but I don't get what this display_it thing is. I tried googling to no avail, and doing a search here on cplusplus.com didn't give me any results either. So can someone explain it to me?

Thanks,
-Oni Kami
display_it is a function. It gets define outside of main.
Then you call function in main.

void display_it ( .... )
{
    .....
}
int main()
{
    .....
    display_it( ... );
    .....
}
Last edited on
I bet you won't find anything about the term "display_it" no matter where you look... It's a user defined function, YOU are the one supposed to create that function! And I can see here that your teacher gave you a lot of help by giving you the definition. All you have to do is declare it in your program and define it using the code he gave you! Now, I use Dev myself and the piece of code you threw here won't compile (I could tell that by just looking at it :D but I tried to compile it just in case Dev accidentally compiles it due to some very weird bug...). Since your homework is about functions, don't you think that, at least, you should read how to declare, define and call one??? I ll do you a favor this time, but not the next! And be sure that no one else will!

When your teacher says
"Use overloading to write a function that displays two real values with a specific precision."
he simply means to declare and define 2 functions, with the same name ("display_it ") but with different arguments. Then, when u use the common name to call one of them, the compiler will decide which one to call depending on the number and type of the arguments u give. Let's see how this is done:

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
48
#include <iostream>
using namespace std;

void display_it(double, int); //first version declaration
void display_it(double, double, int); //second version declaration


int main()
{
    double d1,d2;
    int p;
    
    cout << "give me a double: ";
    cin >> d1;
    cout << "give me another double: ";
    cin >> d2;
    
    cout << "give me the precision u want: ";
    cin >> p;
    
    cout << "printing first double:\n";
    display_it(d1,p); //first version call
    cout << "printing second double:\n";
    display_it(d2,p); //also first version call
 
    cout << "printing both doubles:\n";
    display_it(d1,d2,p); //second version call
    
    system("pause");
    return 0;
}

void display_it(double x, int prec) //first version definition
{
      cout.setf(ios::fixed);
      cout.setf(ios::showpoint);
      cout.precision(prec);
      cout << x << endl;
} 

void display_it(double x, double y, int prec) //second version definition
{
     cout.setf(ios::fixed);
     cout.setf(ios::showpoint);
     cout.precision(prec);
     cout << x << endl;
     cout << y << endl;
}
Last edited on
Thank you guys sooooo much, I totally get it now. :D
Topic archived. No new replies allowed.