Results 1 to 15 of 15
  1. #1
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored

    How can a user input words?

    Hey guys, im working on a program that you type a password and than you can proceed with the program but what do i type instead of int? (srry i forgot what the int was. i know its an integer but what was it.) coz with int,double you can put numbers, bool, true/false and with char only 1 char, but how do i insert a full word?



    Dont be a TOOL say Obamas cool.

  2. #2
    Nathan's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    In a magical place
    Posts
    6,113
    Reputation
    394
    Thanks
    363
    Code:
    char passwd[50];
    std::cin >> passwd;
    If(passwd == "the actual pass"){
    std::cout << "Access Granted”;
    } else {
    std::cout << "Access Denied";
    }
    This should work, but I haven't used C++ for like 2 months, so it might contain some small syntax errors. (I've been using Java lately).

  3. #3
    Fly3r's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Not telling.
    Posts
    720
    Reputation
    18
    Thanks
    265
    My Mood
    Paranoid
    isnt it with cin?
    for example
    Code:
    int blabla;
    cout << "This is a random line";
    cin >> blabla;
    Forgive me if im wrong.
    I just started learning C++
    Joined MPGH: 07/08/09


    i used to tell arrow to the knee jokes then i died due to blood loss from takeing tomany arrows to the knee at once
    A network problem caused by you? What did you do? Trip over the cable?




  4. #4
    Pxpc2's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    231
    Reputation
    6
    Thanks
    11
    My Mood
    Angelic
    Quote Originally Posted by Fly3r View Post
    isnt it with cin?
    for example
    Code:
    int blabla;
    cout << "This is a random line";
    cin >> blabla;
    Forgive me if im wrong.
    I just started learning C++
    Yup I learned this with cout<< for outputs and cin >> for user input.

    But as I also changed to Java, maybe...

  5. #5
    Nathan's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Location
    In a magical place
    Posts
    6,113
    Reputation
    394
    Thanks
    363
    Quote Originally Posted by Fly3r View Post
    isnt it with cin?
    for example
    Code:
    int blabla;
    cout << "This is a random line";
    cin >> blabla;
    Forgive me if im wrong.
    I just started learning C++
    An int doesn't hold a word.
    A array of chars or a string does.

  6. #6
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored
    O.o i created something like this (Just to try if the pw thing works) but when i run it and write kana it says access denied. plz help.
    Code:
    #include <iostream>
    #include <Windows.h>
    
    using namespace std;
    
    int main()
    {
    	char passwd[50];
    		cin >> passwd;
    	if(passwd == "kana"){
    		cout << "Access Granted";
    	}else{
    		cout << "Access Denied";
    	}
    }



    Dont be a TOOL say Obamas cool.

  7. #7
    Fly3r's Avatar
    Join Date
    Jul 2009
    Gender
    male
    Location
    Not telling.
    Posts
    720
    Reputation
    18
    Thanks
    265
    My Mood
    Paranoid
    Cookie thanks for correcting me .. i ve just started
    Joined MPGH: 07/08/09


    i used to tell arrow to the knee jokes then i died due to blood loss from takeing tomany arrows to the knee at once
    A network problem caused by you? What did you do? Trip over the cable?




  8. #8
    Auxilium's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    深い碧の果てに
    Posts
    4,518
    Reputation
    445
    Thanks
    609
    My Mood
    Happy
    #include <string>
    then use std::string type

  9. #9
    Hell_Demon's Avatar
    Join Date
    Mar 2008
    Gender
    male
    Location
    I love causing havoc
    Posts
    3,976
    Reputation
    343
    Thanks
    4,320
    My Mood
    Cheeky
    You can't compare two words like that, a char array is nothing but a pointer, seeing as the pointers to both strings will be different even if the text is the same, you'll have to use one of the following:
    Code:
    if(!strcmp(passwd, "kana") // strcmp returns 0 if both strings are the same
    {
    //code
    }
    Or:
    Code:
    if(std::string(passwd).Compare("kana"))
    {
    }
    Ah we-a blaze the fyah, make it bun dem!

  10. The Following 2 Users Say Thank You to Hell_Demon For This Useful Post:

    Fly3r (06-18-2011),whit (06-18-2011)

  11. #10
    wtfiwantthatname's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Posts
    260
    Reputation
    10
    Thanks
    39
    My Mood
    Bored
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
    	string passwd;
    		cin >> passwd;
    	if(passwd == "kana"){
    		cout << "Access Granted";
    	}else{
    		cout << "Access Denied";
    	}
    }
    If he has using namespace std; and #include <string.h> than he doesnt need to prefix std:: to all of his strings.
    "I don't believe in an afterlife, so I don't have to spend my whole life fearing hell, or fearing heaven even more. For whatever the tortures of hell, I think the boredom of heaven would be even worse." - Isaac Asimov

  12. #11
    Ronon666's Avatar
    Join Date
    Jan 2011
    Gender
    male
    Location
    What are you? A stalker?
    Posts
    122
    Reputation
    8
    Thanks
    46
    My Mood
    Bored
    thx guys for your help.
    #include <string.h> did not help at all btw.....

    #include <string> did help but i tried it before... it didnt work than o.O.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main(){
    	string acc, pw;
    	cout << "Username: \n";
    	cin >> acc;
    	cout << "Password: \n";
    	cin >> pw;
    	if(acc == "MPGH"){
    		cout << "Username correct\n\n";
    	}
    	else if(acc != "MPGH"){
    		cout << "Username incorrect\n\n";
    	}
    
    	if(pw == "cplusplus"){
    		cout << "Password correct\n\n";
    	}
    	else if(pw != "cplusplus"){
    		cout << "Password incorrect\n\n";
    	}
    	if(acc != "MPGH" && pw != "cplusplus"){
    		cout << "Username or/and password incorrect, are you sure you got the valid info?" << endl << "contact me for the password and username\n" << endl;
    	}
    	else if(acc == "MPGH" && pw == "cplusplus"){
    		cout << "Information correct. Press any key to see your code." << endl;
    		system("pause");
    		cout << endl;
    		cout << "Code : gHahwsejGKfndfj6893860339gDhbJeGhtdhzxvnG" << endl << "paste this on the forum\n\n";
    	}
    	system("pause");
    
    }
    That what i came up with and i think its pretty cool.
    Now i need to find out somewhere what to use it for xD.



    Dont be a TOOL say Obamas cool.

  13. #12
    Lu1z1's Avatar
    Join Date
    Jul 2011
    Gender
    male
    Posts
    1
    Reputation
    10
    Thanks
    0

    probably last post for now sry for the multiple posts

    .-. Really simple:

    Code:
    #include<iostream>
    #include<cstdlib>// to add Pauses, if u don't want them then erase em.
    #include<string>
    #include<windows.h>//I think u need this to erase everyting on the screen. //don't remember
    
    using namespace std; // so u can just write cout instead of std::cout
    
    int main()
    {
       string username = "Guest"; // declare a string variable and initialize it.
       string password = "MPGH";
       string userInput1;
       string userInput2;
    
       int security;
    
    // a string variable holds letters. Which in this case it's what you want
    
       cout << "username: ";
       cin >> userInput1;
    
       system("CLS"); // erases everything on the screen. 
    
       cout << "Password: ";
       cin >> userInput2;
    
       if(userInput1 == username)
       {
           security = 5;
           
           if(userInput 2 == password)
           {
           security = security + 5;
    
    
           }
    
       }
       else 
       {
       security = 0;
    
       }
       
       if(security == 10)
       {
          cout << "Welcome guest!\n\n";
    
       }
       if(security == 5)
       {
            cout << "incorrect password or username. Shutting down.\n";
            system("PAUSE");
            return 0;
       }
       if(security == 0)
       {
            cout << "error Shutting down\n\n";
            
            system("PAUSE");
            return 0;
    
    
       }
    
     system("PAUSE");
     return 0;
    }
    I think this should work. If it doesn't tell me. remember to #include<string>
    if u don't it won't work.

    oops. Didn't see your last post XD sry then

    a error I saw. first u forgot one little curly brace at the end. and u put
    system("pause"); instead of system("PAUSE");
    and sudgestion, when u name functions try naming them so u remember what they're for. And so that your code is more readable to others. and something people tend to do is that the first word in a variable name starts with a none capital letter and the other words in a variable name are capital letters.
    like this:

    int aVariable = 100;

    oh and if you name more than one variable by the same type, name them in different lines so it's more readable to others.

    like this:

    instead of:
    int pie, cake;

    try:
    int pie;
    int cake;

    I think that's it
    Last edited by Lu1z1; 07-07-2011 at 05:32 PM.

  14. #13
    FailHacker's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    444
    Reputation
    8
    Thanks
    49
    "system("CLS");" <------- DONT DO IT.

    Couldn't word it well so here:
    "It's frowned upon because it's a platform-specific hack that has nothing to do with actually learning programming, but instead to get around a feature of the IDE/OS - the console window launched from Visual Studio closes when the program has finished execution, and so the new user doesn't get to see the output of his new program."
    Legen...wait for it...dary







  15. #14
    DecoderBack's Avatar
    Join Date
    May 2011
    Gender
    male
    Posts
    111
    Reputation
    10
    Thanks
    12
    Quote Originally Posted by Virtual Void View Post
    #include <string>
    then use std::string type
    it should be better if he uses using namespace std; into the top with includes hearders lol


    for example

    Code:
    #include < blah blah my hearder>
    
    using namespace std;
    
    
    int main()
    
    {
                          cout << "Hello my first message in output";
    // output ( message )
    
    			cin >> inicial >> final >> razao;
    
    // input ( when the person enter something into the screen)
    
    
    }
    Last edited by DecoderBack; 07-08-2011 at 08:52 AM.

  16. #15
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    That is absolutely incorrect. Polluting the global namespace (e.g., using namespace std;) is highly frowned upon. There is reason why the standard library is implemented within the std namespace.

    If you MUST import names, do so in the most local scope possible using a using declaration, not a using directive.