Thread: Word variables

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    Word variables

    I am brand new to C++ coding and i have written a short program.

    Code:
    #include <iostream>
    
    int main()
    {
        int   pd;
        int   id;
        
        
    std::cout<<"Loading security profile. Please enter user ID #: \n";
    std::cin>> id;
    std::cout<<"ID # acceped. Loading password protocols...\n";
    std::cout<<"Protocols loaded. Initializing...\n";
    std::cout<<"Protocols initialized. Please enter password: \n";
    std::cin>> pd;
    if (pd == 255 ) {
           std::cout<<"Your password is correct. Please continue.\n";
           std::cin.get();
           }
    else {
         std::cout<<"Login failure. Security lockout online. Terminal shutdown.\n";
         std::cin.get();
         }
    std::cin.get();
    }
    Now, what I want to do is use a word password instead of a number. And I have no idea how to do that. Any suggestions?
    Thanks in advance.

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Look into using the string library:
    Code:
    #include <iostream>
    #include<string>
    int main()
    {
        std::string   pd;
        int   id;
        
        
    std::cout<<"Loading security profile. Please enter user ID #: \n";
    std::cin>> id;
    std::cout<<"ID # acceped. Loading password protocols...\n";
    std::cout<<"Protocols loaded. Initializing...\n";
    std::cout<<"Protocols initialized. Please enter password: \n";
    std::cin>> pd;
    if (pd == "foobar" ) {
           std::cout<<"Your password is correct. Please continue.\n";
           std::cin.get();
           }
    else {
         std::cout<<"Login failure. Security lockout online. Terminal shutdown.\n";
         std::cin.get();
         }
    std::cin.get();
    }

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    2
    amazing...thank you.


    EDIT: one thing. i forgot to add the #include<string> but the program still ran with no errors...why did that happen? is the library not necessary?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > but the program still ran with no errors...why did that happen?
    Luck basically.

    A lot of simple programs seem to work despite many things being wrong with them. But sooner or later, such mistakes will have a negative impact on your program.

  5. #5
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, definately include the string library. Even though your compiler did not complain here, it might later on when you try to do more complicated things with strings. Also, if you ever plan on switching compilers you'll find that you'll suddenly get errors, for example the code I posted above would not work in Microsoft Visual Studio (it would complain about the the << and the ==) if you didn't include the string library.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    If I were you, I'd keep this program around, because this would be good to use when you get into file I/O, where you can have ID's and their corresponding passwords in files...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The most likely reason why your app worked is that on your compiler (and others might behave differently), <iostream> internally includes <string>, perhaps indirectly (that is, it includes headers which include headers which include <string>). The important thing is that such behaviour is specific to your compiler, and more, to the specific version of compiler you're using and the specific vendor and version of standard library you're using (you can replace it independently from your compiler). So you must not rely on it.

    Just as an example, the standard library that comes by default with Visual Studio.Net 2003 (Dinkumware 3.13, I think), includes <istream> from <iostream>. <istream> includes <ostream>, which includes <ios>, which includes <xlocnum>, which includes <streambuf>, which includes <xiosbase>, which includes <xlocale>, which includes <stdexcept>, which includes <xstring>, which is where this standard library defines the std::string class.

    The mere length of this include chain and the fact that it includes internal secret headers of the standard library impementation (those starting with x) should be sign enough that you can't rely on any of it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Assembly Tutorials
    By JoshR in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 06-11-2005, 09:56 AM
  4. Random word problem
    By goron350 in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2005, 03:44 PM
  5. Help with a troublesome C++ program
    By Kristina in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2002, 01:28 PM