Results 1 to 20 of 20

Thread: Returning a char array doesn't work :(

  1. #1
    DaoK
    Guest

    Returning a char array doesn't work :(

    why that doesn't work :

    Code:
    char[] PasswordMaker()
    {
    	char cPass[20];
    	char cChar;
    	
    	for (int i = 0 ; i < 20;i++ )
    	{
    		if ((cChar = getch()) != '\n')
    			cPass[i++] = cChar;
    		else
    			break;
    	}
    	return cPass;
    }

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800

    Thumbs up

    I've never seen char [] as a returntype...try making a call by reference parameter...

    void PasswordMaker(char &mystring[20])


    so now whatever you enter into the function is what gets changed

  3. #3
    DaoK
    Guest
    error C2234: '<Unknown>' : arrays of references are illegal


  4. #4
    DaoK
    Guest
    well i replace the & byt * and it worked,
    Thx for the hint

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Is this in C++? Why not just return a string object?
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6
    DaoK
    Guest
    well I never played with String in C++ and I can not figure how to use the keyboard input to put stuff in String so I do not touch to String, want to learn me ? In my book they do not talk about String

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
    	cout << "What is your name? " << flush;
    	
    	string nm;
    	getline(cin, nm);
    
    	cout << endl << "Hello " << nm << "!" << endl;
    
    
    	return 0;
    }
    See www.bruceeckel.com (Thinking In C++) for more info on strings
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    DaoK
    Guest
    Thx you, it worked once and when I tryed to implement it to my function i got now 22 errors and they look like :
    error C2065: 'string' : undeclared identifier

    and I do not have a clue why, because I have the include <string>

  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In a .cpp file, whack a "using namespace std;" just after the includes. In a header, use "std::string" rather than "string".
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    DaoK
    Guest
    How you know that ? Can you tell me if it's in : Thinking In C++ ?
    I need to know all that stuff !

    And a big thank you for your help

  11. #11
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Most things are in there

    Strings are chapter 2


    (FYI I learnt most of this by progressively trashing my computer No, really, I actually wiped out Win 98 when my recursive folder search program went haywire )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  12. #12
    DaoK
    Guest
    Coooool

    But one more thing before I go eat

    I have searched in my VisualC++ help and I have found something about the string library. I know that it have a length() function but I can not access it in my header file (in a function)

    here is a little piece of code :
    Code:
    void utility::PasswordMaker(std::string *sPass)
    {
    	char cChar;
    	char cPass[sPass.length()];
    If you can tell me why when I add the dot after the sPass I do not see all String option maybe it will solve the error (error C2057: expected constant expression)

    And what I want to do is to be able to write at a speacific place in the string so I put back my String in a char[] and later will put back in the String, I know it's not the best way but I want to try this one for the moment. So if you can help me to tell me why it do that I will appreaciate and if you see an other solution feel free to suggest it. I think this weekend I will go buy your good book

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You're passing a pointer so you need to use ->.

    In this situation, I'd recommend a const reference:
    Code:
    void utility::PasswordMaker(const std::string &sPass)
    {
    	char cChar;
    	char cPass[sPass.length()];
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  14. #14
    DaoK
    Guest
    I will cry !!!!!

    Code:
    error C2228: left of '.length' must have class/struct/union type
    C++ will make me crazy

    and between I knew the -> but it did work that why I asked here

  15. #15
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Hmm. Actually, you can't even initialise the array like that, arghh...

    You need to do char *cPass = new char[sPass.length()];
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  16. #16
    DaoK
    Guest
    error C2100: illegal indirection¸

    I think I need to stop for tonight, I tryed so many thing and it still bugging grrrr

  17. #17
    Lively Member
    Join Date
    Aug 2001
    Posts
    109
    either return a pointer to char array, or have one of your functions param be a pointer to a char array you declared outside the function and modify it in the function threw the pointer...

    about strings....

    there's also the CString class...

  18. #18
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    No, CString is not an option here. It's MFC only, and I don't think it has a place in what DaoK is doing here.
    If you post the code we could try finding the errors. (If you haven't already done, it's been a while.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  19. #19
    DaoK
    Guest
    Thx but STRING did the work well

  20. #20
    Hyperactive Member
    Join Date
    Sep 2001
    Posts
    396
    Originally posted by AlbafaN
    either return a pointer to char array
    You can't return a pointer to char array. bcos the char array will go out of scope when the function returns.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width