Thread: Is cin for only numbers??

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    25

    Is cin for only numbers??

    Code:
    #include <iostream.h>
    char user, pass;
    
    int main(int argc, char *argv[])
    {
         cout <<"                          Project Communication Service\n";
         cout <<"                            Private Message bot v1.0\n\n\n";
         cout <<"LOGIN\n";
         cout <<"Username: ";
         cin>>user;
         cout << "Password: ";
         cin>>pass;
      return 0;
    }
    The above code will only execute the pasword cout and cin if the user cin = a integer, otherwise it just closes. Does anyone know why? (I am really new to C++, sorry) Thanks.
    Thanks

  2. #2
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    I think you are trying to make a user/pass longer than 1 character. In that case you must use strings, either by the string class or a one dimensional char array.

    Code:
    #include <iostream>
    using namespace std;
    int main() /*int argc char *argv[] not needed */ {
    char user[51],pass[51];
    cout<<"Login"<<endl;
    cout<<"pass:"<<endl;
    cin>>pass;
    cout<<"user:"<<endl;
    cin>>user;
    return 0;
    }
    cin works with all built-in data types, including characters.
    I AM WINNER!!!1!111oneoneomne

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    the codes written to hold only one character in the char variable. if you do this:

    Code:
    #include <iostream.h>
    char user[256], pass[256];
    
    int main(int argc, char *argv[])
    {
         cout <<"                          Project Communication Service\n";
         cout <<"                            Private Message bot v1.0\n\n\n";
         cout <<"LOGIN\n";
         cout <<"Username: ";
         cin>>user;
         cout << "Password: ";
         cin>>pass;
      return 0;
    }
    the char variables are now character arrays, and will hold up to 256 letters. Cin isn't really the right use here, so we'll use cin.getline like this:

    Code:
    #include <iostream.h>
    char user[256], pass[256];
    
    int main(int argc, char *argv[])
    {
         cout <<"                          Project Communication Service\n";
         cout <<"                            Private Message bot v1.0\n\n\n";
         cout <<"LOGIN\n";
         cout <<"Username: ";
         cin.getline(user,256);
         cout << "Password: ";
          cin.getline(pass,256);
      return 0;
    }

  4. #4
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    EDIT: Ohhh RoD beat me

    You're asking for single characters, and (veterans can correct my terminology ) if you enter more than one char for "user", the variable will only account for one of the chars and the rest are shifted over to "password". Try this...

    Code:
    #include <iostream.h>
    
    int main(int argc, char *argv[])
    {
    	char user[10], pass[10];
    
    	cout <<"                          Project Communication Service\n";
    	cout <<"                            Private Message bot v1.0\n\n\n";
    	cout <<"LOGIN\n";
    	cout <<"Username: ";
    	cin >> user;
    	cout << "Password: ";
    	cin>>pass;
    	return 0;
    }
    Last edited by abrege; 01-28-2003 at 06:45 PM.
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  5. #5
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    Actually now that you mention it, what is the difference between using cin as it is and cin.getline?
    I know cin cant read strings with whitespaces (i.e. "this is a test" will only read "this")
    Does cin.getline overcome this?
    I AM WINNER!!!1!111oneoneomne

  6. #6
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Originally posted by Panopticon
    I think you are trying to make a user/pass longer than 1 character. In that case you must use strings, either by the string class or a one dimensional char array.

    Code:
    #include <iostream>
    using namespace std;
    int main() /*int argc char *argv[] not needed */ {
    char user[51],pass[51];
    cout<<"Login"<<endl;
    cout<<"pass:"<<endl;
    cin>>pass;
    cout<<"user:"<<endl;
    cin>>user;
    return 0;
    }
    cin works with all built-in data types, including characters.
    cin would cut off all but the first letter in the string.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    Ok, Thanks alot (both of you).
    Thanks

  8. #8
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    cin would cut off all but the first letter in the string.
    Im pretty sure it doesn't...
    I AM WINNER!!!1!111oneoneomne

  9. #9
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    i think, i honestly don't recall. for strings getlines better either way.

  10. #10
    Registered User abrege's Avatar
    Join Date
    Nov 2002
    Posts
    369
    Originally posted by Panopticon
    Im pretty sure it doesn't...
    You're right, it doesn't
    I am against the teaching of evolution in schools. I am also against widespread
    literacy and the refrigeration of food.

  11. #11
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    Wow, Didn't know so many people posted in this thread, Thanks again. I have one more Q though. Is it possible to make all char's in a cin look like * when typed, Kinda like let hte passChat to * (like in VB)
    Thanks

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Is it possible to make all char's in a cin look like * when typed
    No, you'll need a non-standard function to do non-buffered input.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    this was asked not too long ago.....search the boards for password masking, i honestly forget who asked it.

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    25
    Will do, Thanks.
    Thanks

  15. #15
    Back after 2 years Panopticon's Avatar
    Join Date
    Dec 2002
    Posts
    262
    I done some experimenting with getline a few moments ago and it seems to behave as expected. But it can be overrun just like gets()
    hmmm
    What I dont understand are the parameters of getline
    the first one is a pointer to the first element of the target array, and the second one is... what? the length of the string?
    I tried the following:
    Code:
    char name[10];
    cin.getline(name,10);
    cout<<name;
    cout<<name[10];
    i entered 10 characters and cout<<name produced the first 9 characters i entered. name[10] produced a garbage result. It was not a null terminator so thats where im confused about the implementation of getline().
    I AM WINNER!!!1!111oneoneomne

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding Line numbers in Word
    By Mister C in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 06-24-2004, 08:45 PM
  2. input with cin....
    By MainFrame in forum C++ Programming
    Replies: 3
    Last Post: 03-19-2004, 05:10 PM
  3. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  4. Line Numbers in VI and/or Visual C++ :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2002, 09:54 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 05:38 PM