classes, constructors, and user input

ok i posted a question last night and got a GREAT response that really cleared things up for me, so i thought i would try again... i know i should read through the documentation first, but i was hoping for a quick response for my project... is there a way to write a class member function either in or out of the constructor itself that allows for user input of the data? since the typical instantiation method enters the data automatically class classobj(data,data,data...) i was wondering if you could call a function in that field instead, allowing the data to be entered and returned ... and i DON'T want the actual code lines written for me!! if you could give a PDL or basic description or even a yes or no, i would like to kinda figure out the code myself, as a pet project... and before i even see an answer... THANKS!
to kinda continue... i had this function written up, but couldn't get it to wkr... i was having problems getting ANYTHNG to work, so it may have been my own fault, not the base code's fault but her eit is..
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
void inputClass(mo&,dy&,yr&)
{
     int a;
     bool b=0;
     while (b==0)
     {             
       cout<<"Please enter a number between 1-12 for current month: "<<endl;
       cin>>mo;
       if (mo<1 || mo>12)
       {
          cout<< "the number you entered is invalid, please re-enter:"<<endl;
          cin>>mo;
       }
       cout<<"Please enter a number between 1-31 for current day: "<<endl;
       cin>>dy;
       if (dy<1 || dy>31)
       {
          cout<<"the number you entered is invalid, please re-enter:"<<endl;
          cin>>dy;
       }
       cout<<"Please enter a number between 1950-2020 for current year: "<<endl;
       cin>>yr;
       if (yr<1950 || yr>2020)
       {
          cout<<"the number you entered is invalid, please re-enter:"<<endl;
          cin>>yr;
       }
       cout<<"you entered"<<mo<<""-""<<dy<<""-""<<yr<<"is this correct?"<<endl;
       cin>>b;
       cout<<"press 1 to exit"<< endl;
   cin>>a;
   while(a !=1)
    {
            cout<<"try again"<<endl;
            cin>>a;
    }
     }


now i know the inputs on the function header are prolly wrong, but i was wondering more if i could use this somewhere and get it to work during construction..
You're supposed to declare the parameters just like variables.

void inputClass(int &mo, int &dy, int &yr)
As Catfish points out, the parameter's you have there need to be adusted.

While it's certainly possible to do what your're asking, I wouldn't recommend it. It's better to have your input separate from the innards of the class as much as possible. What you might consider is a function like:

1
2
3
4
5
6
7
8
Date getDate()
{
     int m, d, y ;

     // get values for m, d and y.

     return Date(m,d,y);
}


And then, whenever you need a Date from user input:
 
Date userDate = getDate() ;

Last edited on
so perform the input as a separate function instead of a member function? and do it after i've created the object?
or use it as a member function after the constructor?
your suggestion code reads like you mean a separate function in int main, am i right?
Last edited on
A stand-alone function, yes. And perhaps called from main, if that's what you're asking.
Topic archived. No new replies allowed.