Thread: Can someone tell me why this doesn't work?

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    8

    Can someone tell me why this doesn't work?

    just a simple getinput function that for some reason doesn't work it just returns empty, can anyone help me out?

    Code:
    string getInput(string data, int limit, bool hidden) {
        int ch, i = 0;
        
        do{
            ch = getch();
            ch = tolower(ch);
            
            if (ch == 8) {
                //TODO: user hit backspace
            } else if (isalnum((char)ch) && i <= limit && ch != 13) {
                data += (char)ch;
                if (!hidden) {
                    cout << (char)ch;
                } else {
                    cout << "*";
                }
                i++;
            } else if (i <= limit && ch != 13) {
                Beep(350, 75);
            }
        } while (ch != 13);
    
        return data;
    }

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Parameters of the function cannont be modofied...if you want to modify the parameters of a function....create a macro instead.... , in other words, you can't modify first parameter "data", try to exclude "data", and make a string that was created in the function and return that one instead

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Like This:
    Code:
    string getInput(int limit, bool hidden) {
        int ch, i = 0;
        string data;
    
        do{
            ch = getch();
            ch = tolower(ch);
            
            if (ch == 8) {
                //TODO: user hit backspace
            } else if (isalnum((char)ch) && i <= limit && ch != 13) {
                data += (char)ch;
                if (!hidden) {
                    cout << (char)ch;
                } else {
                    cout << "*";
                }
                i++;
            } else if (i <= limit && ch != 13) {
                Beep(350, 75);
            }
        } while (ch != 13);
    
           //Now Here you can return data, with modified content
        return data;
    }

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    oooooooooooohhhhh really? wow now I feel dumb, thanks alot I prolly never would've thought of that

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Actually that's not correct. It makes no difference in this case. It might be better style, but it might also return a different value than the original code.

    What parameters are you passing to your function?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    Well originally I was doing getInput(data, 9, false); to get a string to be stored into data that was no longer then 10 characters long and didn't show up as hidden, but it wasn't returning what i typed, just random jibberish. After doingt what toonlover said, I can just do like string = getInput(9, false); to achieve the same effect...and it actually works

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The fix was to use the return value of the function instead of the passed in parameter. The change toonlover made didn't actually change how the function worked, it just forced you to find the real problem. Of course, it is a better idea to use the new code for style reasons, since you apparently aren't intending to pass an initial value to your function, so all is well. I just don't want people thinking that you cannot modify a function parameter because of course you can.

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    Erm, what would be the best way to convert a char[x] or whatever the length to lower be? tolower() only takes int >.>

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    A char can be converted to an int automatically. If you need to, cast the return value back to char:
    Code:
    str[x] = static_cast<char>(tolower(str[x]));

  10. #10
    Registered User
    Join Date
    Feb 2006
    Posts
    8
    ahh thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM