Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
C++
#include <iostream>
using namespace std;

    int doDateofBirth(int m , int d , int y);

    int main(){

        cout << "My DoB is, " << doDateofBirth(4,14,1998) << endl;


    }

    int doDateofBirth(int m,int d,int y){
        return m,d,y;
    }


The console is only returning the y
Posted
Comments
Afzaal Ahmad Zeeshan 10-Oct-15 3:56am    
Did you intend to return a "string" for your date of birth?
Justine Cantado 10-Oct-15 4:07am    
No i only want to return the date,day and year.
Afzaal Ahmad Zeeshan 10-Oct-15 5:14am    
I only want to return — would return only one single object. You are trying to get 3. Why?
George Jonsson 10-Oct-15 5:11am    
You can only return a single object from a method.
Return either a string or a class with a built in print function.
As it is now, your code doesn't make much sense.

Quote:
The console is only returning the y
That is the correct behaviour of the comma operator (see here[^]).
Anyway a C/C++ function returns a single value.


However, the single return value could be an object (instance of a class or a struct) and you may define the insertion operator for such a struct or class, e.g.
C++
#include <iostream>
using namespace std;

struct DOB //'date of birth' struct
{
  int m, d, y;
};

// insertion operator for DOB objects
ostream & operator << (ostream & os, const DOB & dob)
{
  return os << dob.m << " " << dob.d << " " << dob.y;
}

DOB doDateofBirth(int m , int d , int y);

int main()
{
  cout << "My DoB is, " << doDateofBirth(4,14,1998) << endl;
}

DOB doDateofBirth(int m,int d,int y)
{
  DOB dob;
  dob.m = m; dob.d = d; dob.y = y;
  return dob;
}
 
Share this answer
 
v2
Comments
Patrice T 10-Oct-15 13:28pm    
+5
CPallini 10-Oct-15 15:39pm    
Thank you.
Here is another way to do it.

C++
#include <iostream>
using namespace std;
 
    void doDateofBirth(int mym , int myd , int myy);
    int m, d, y;
 
    void main(){
        doDateofBirth(4,14,1998);
        cout << "My DoB is, " << m << d << y << endl;
    }
 
    void doDateofBirth(int mym,int myd,int myy){
        m= mym;
        d= myd;
        y= myy;
    }</iostream>
 
Share this answer
 
v2
Comments
George Jonsson 10-Oct-15 21:05pm    
Not really C++ as you use global variables.
Patrice T 10-Oct-15 21:40pm    
Why? global variables are not allowed any more?
George Jonsson 10-Oct-15 21:47pm    
They are used in C, but should be avoided there too.
C++ is all about object orientation and encapsulation.
You should avoid manipulating variables outside the scope of a class, or in this case a method.
Using global variables can have very nasty side effects.
Patrice T 10-Oct-15 22:11pm    
My experience is that global variables are very useful and I am still to see the nasty side effects.
George Jonsson 11-Oct-15 3:43am    
Then you have been lucky.:)
Or never worked with fast and furious programmers.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900