Need Help: Function in class

Hey, I'm just started to learn that thing that calls Object-Oriented Programming. And to test myself desided to code simple "user"-object creator. So I has code like this.
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "login.h"
#include "clearscreen.h"
#include <iostream>
#include <string.h>
using namespace std;
class user
{
public:
    char Name [128];
    char Nickname [128];
    char Passwd [15];
    int UID;
};
void hello()
{
    int i;
    cout << "\nPlease enter an number: \n (1)-register new user \n (2)-Login\n (3)-exit \n";
    cin >> i;
    choise (i);
}
int choise (int choise)
{
    if (choise==1)
    {
        registration ();
    }
    if (choise==2)
    {
        return 0; //!Here will be login
    }
    if (choise==3)
    {
        ClearScreen(); //pass 100 lines and..
        return 0; //exiting
    }
    return 0;
}
int registration ()
{
    int UID;
    user iamuser[9];  //new user each registration(int LastUID);
    //user* piamuser;      //*********************************//
    //piamuser = &iamuser; //pointers *just for test          //
    //cout << piamuser;    //*********************************//
    cout << "\nEnter number from 1 to 9; that will be your UserID: ";
    cin >> UID;
    iamuser[UID].UID=UID;
    cout << "UID1 =" << UID << "\n"; //test
    if (UID>9||UID<1)
    {
        cout << "I said 'Enter number from 1 to 9; that will be your UserID'. App will be terminated.";
        return 0;
    }
    cout << "\nEnter your real name: ";
    cin >> iamuser[UID].Name;
    cout << "\nEnter your nickname: ";
    cin >> iamuser[UID].Nickname;
    cout << "\nEnter desired password: ";
    cin >> iamuser[UID].Passwd;
    if (strlen(iamuser[UID].Passwd)>16)
    {
        cout << "\n Your password is too long. It must be < 17;\n Application exiting now...; \n Press enter";
        cin.get();
        return 0;
    }
    else
    {
        cout << "\nYour Name: " << iamuser[UID].Name << "\nYour Nickname: " 
<< iamuser[UID].Nickname 
<< "\nYour UserID: " << UID 
<< "\nYour Password: " << iamuser[UID].Passwd 
<< "\nPress Enter;";
        //  cin >> piamuser->Name;
        //  cout << iamuser.Name;
        cin.get();
        hello ();
    }
    return 0;
}

It works fine, anyway. But if I try doing something like this:
1
2
3
4
5
6
7
8
9
class user
{
public:
    char Name [128];
    char Nickname [128];
    char Passwd [15];
    int UID;
    displayData (int UID)
};

Combining with:
1
2
3
4
5
6
7
8
9
10
int user::displayData (int UID)
{
cout << "\nYour Name: " << iamuser[UID].Name 
<< "\nYour Nickname: " 
<< iamuser[UID].Nickname 
<< "\nYour UserID: " << UID 
<< "\nYour Password: " 
<< iamuser[UID].Passwd << "\nPress Enter;";
return 0;
}

and calling it from registration() in output the weird symbols as arrows and else appeard instead of normal name, nickname, UID and passwd.
Last edited on
I'm not sure how exactly you combined the two, but you cant access iamuser[UID].Name from inside a member function!

member functions have access to the private and public members of that object.

so the proper way to combine would be

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
class user
{
public:
    char Name [128];
    char Nickname [128];
    char Passwd [15];
    int UID;
    void displayData (int UID);
};

Combining with:
1
2
3
4
5
6
7
8
9
10
void user::displayData (int UID)
{
   cout << "\nYour Name: " << Name 
   << "\nYour Nickname: " 
   << Nickname 
   << "\nYour UserID: " << UID 
   << "\nYour Password: " 
   << Passwd << "\nPress Enter";
return 0;
}


first off: that functions returns nothing, hence void.
second off: the variables visible inside the function are only ones declared inside that function, which you got none, or members of the object.
and you dont need an array of users, one is enough in your example
First off, you should take a look at the cplusplus tutorials on
1) arrays,
2) functions, and
3) classes

Secondly...
1
2
3
4
5
6
7
8
    int UID;
    user iamuser[9];      //The array is only of size 9.
                          //This means you can only access iamuser[0] to iamuser[8]

    cout << "\nEnter number from 1 to 9; that will be your UserID: "; //should say 0 to 8
    cin >> UID;

    iamuser[UID].UID=UID;


I recommend using do while loops to check if the user's input is valid.
1
2
3
4
do
{
    cin >> UID;
}while(UID is not valid...);


As for displaying stuff: you misunderstand how to use variables in classes. Since displayData is user's function, it can access all of the variables and function in user.
1
2
3
4
5
6
7
8
9
//call your function like so
iamuser[USD].displayData();
//have your function like this...
int user::displayData()
{
cout << "\nYour Name: " << Name << endl;
//notice how I didn't say iamuser[UID].Name.
//it's just Name because there is only ONE Name for the ONE instance of this class.
}
dem7w2, thanks that worked anyway, will read mor man's and tutorials from series "C++ for Dummies"
Topic archived. No new replies allowed.