problem with reference string

This is a discussion on problem with reference string within the C++ Programming forums, part of the General Programming Boards category; this is my code i can't get the function to pass back the string usrname, i don't even think that ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    2

    Angry problem with reference string

    this is my code i can't get the function to pass back the string usrname, i don't even think that i did the pointers right...... i have been messing with it for days so it might be a little screwy.

    plz help me.





    int GetChar(string& usrname, int& CharLvl)
    {
    char usr[60];
    char pass[60];
    char temppass[60];
    char *user;
    user->usrname;
    cout << "Enter 'new' for New Character!" << endl;
    cout << "Username: ";
    cin.getline(usr, 60, '\n');
    if(strcmp(usr, "new") == 0)
    {
    cout << "What is the username you wish to use: ";
    cin.getline(usr, 60, '\n');
    ofstream b_file(usr, ios::noreplace | ios:ut );
    if( !b_file ) {
    cout << "Username In use pick a new name.\n";
    return 1;
    }
    cout << "What password you wish to use: ";
    cin.getline(pass, 60, '\n');
    b_file << pass << endl;
    b_file.close();
    usrname = &usr[60];
    return 0;
    }
    cout << "Password: ";
    cin.getline(pass, 60, '\n');
    ifstream a_file(usr);
    if (!a_file )
    {
    cout << "Username not found.\n\n";
    return 1;
    }
    a_file.get(temppass, 60, '\n');
    a_file.close();
    if( strcmp(temppass, pass) != 0 )
    {
    cout << "Invalid Username and or Password!\n\n";
    return 1;
    }
    strcat(usrname, user);
    cout << usr << endl;
    cout << usrname<< endl;
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    Code:
    ok a couple of things...
    
    1. usr->username on a char array(usr[60])
         the -> (arrow notation is used for accesing members of a struct
        or a class when the variable is pointer to the object
        example: CMyObject *pobject;
        pobject = new CMyObject;
        pobject->memberVariable1;
        pobject->memberVariable2;
        
    
    struct CMyObject{
         int memberVariable1;
         int memberVariable2;
    };
    
    2. Whenever you pass a variable by reference... function1(int& myVar)
    
    modifying that variable from within your function will maintain the new value after the function returns...
    
    and although you could do this...
    
    char* pString = string1->c_string();
    
    why do it just call c_string();
    
    3. Study classes a little closer... you will notice that the string class has overloaded the = operator
    you can do things like this]
    
    char buffer[10] = "mickey";
    string string1("mouse");
    
    string1 = buffer;  //not string1 = &buffer[10]
    
    hope this helps....
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 10:52 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21