Results 1 to 7 of 7

Thread: Seperating a Character in C++

  1. #1
    Join Date
    Feb 2006
    Beans
    192

    Seperating a Character in C++

    Hi,
    I have a userInput kind of like this

    cin >> userInput;

    I know the user input will be a character of to ie "G5" But I don't know how I can seperate the character. For instance I want;

    char one = 'G';
    char two = '5';

    Is there a member function that can complete this task??? Thanks any help is much apritiated,

    Cobbweb

  2. #2
    Join Date
    Jun 2006
    Location
    Sheffield
    Beans
    51

    Re: Seperating a Character in C++

    Hi CobbWebb!

    If userInput is a character array (or string) and you know the input will always be 2 characters then it's so simple..

    try

    cin >> userInput;

    char one = userInput[0];
    char two = userInput[1];

    Hope this helps!
    Norm

  3. #3
    Join Date
    May 2006
    Location
    Netherlands
    Beans
    561
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: Seperating a Character in C++

    How about cin.get(), which will read one char from the input.
    (\ /)
    (O.o)
    (> <)

    This is Bunny. Copy Bunny into your signature to help him on his way to world domination.

  4. #4
    Join Date
    Nov 2004
    Beans
    2,614

    Re: Seperating a Character in C++

    If it's really just two characters, then cin.get() would suffice and be smarter.

    But it's almost never two characters. It's almost always superior to read whole lines into a string, then split the string in various ways.

  5. #5
    Join Date
    Aug 2006
    Beans
    3

    simple password checker

    "i created a program that prompts the user to give a psswrd, then the program stores the password. the program then checks the password. but when it gets to the part where it checks the password, no matter what i do, it never sees that the two strings are the same...i was wondering if i someone could point me in the right direction?

    anyways, here is the code:

    Code:
    //store/check password
    #include <iostream>
    #include <cstdlib>
    using namespace std;

    class pswrd {
    public:
    char ps[80], usr[80];
    int len;
    void set_ps (char *p) {strcpy(ps, p);}
    void check_ps () {
    printf(""\nPassword: "");
    cin>>usr;
    if(usr!=ps) cout<<endl<<""Access Denied"";
    else if(usr==ps) cout<<endl<<""Access Granted"";
    }
    };

    int main ()
    {
    pswrd ob1;
    char p[80];
    printf(""Set Password: "");
    cin>>p;
    ob1.set_ps(p);
    ob1.check_ps();
    system(""PAUSE"");
    }"

  6. #6
    Join Date
    Nov 2004
    Beans
    2,614

    Re: simple password checker

    Don't ever, ever, ever use char arrays like that in C++. Use std::string.

    No point in talking about your code until you fix that. There's also no need to create a class like you did. If you want a class to represent a password, just have store the password and compare the strings. Don't have it prompting for the input like that, it's really beyond it's scope.

  7. #7
    Join Date
    Mar 2005
    Location
    Dunedin, NZ
    Beans
    559
    Distro
    Kubuntu 7.10 Gutsy Gibbon

    Re: simple password checker

    I have a very similar response to that of LordHunter317.

    I quickly read the post and went "NOOOO!!!!! don't do that", but then I read said previous post and thought "sanity reigns again".
    ACCU - for programmers who care

Bookmarks

Posting Permissions

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