Code Help needed.

This is a discussion on Code Help needed. within the C++ Programming forums, part of the General Programming Boards category; I need some help just starting learning C++ a couple days ago. I REALY! wonna learn I tried to make ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Unhappy Code Help needed.

    I need some help just starting learning C++ a couple days ago. I REALY! wonna learn I tried to make a small password asking program. But i get a "password:no such directory/file exists" here is the code.

    Code:
    ]#include <iostream>
    #include <password>
    using namespace std;
    		
    int main()
    {
       int password;
      char Password;
      
      cout<<"Please input your Password: ";
      cin>> Password;
      cin.ignore();
      if ( Password = JJ66547) {
         cout<<"WELCOME!!\n";
      }
      else if ( Password == adam ) {
         cout<<"HAHA WRONG!\n";
      }
      else {
        cout<<"LEAVE ME ALONE!!\n";
      }
      cin.get();
    }
    Any help please

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    #include <password>
    As far as I know, that's not a standard header file. Take that part out.
    Code:
    int password;
    You never use this variable. You can leave it in there, but there's really no need.
    Code:
    char Password;
    //snip
    cin>> Password;
    Just so that you understand, you'll only read in a single character. You might want to try using a string instead.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
            
    int main()
    {
      string Password;
    //snip
    Code:
    if ( Password = JJ66547) {
    You need to enclose your literal in quotes. Like this:
    Code:
    if ( Password == "JJ66547") {
    You'll have to do that for each of your if-statements. Also, you need the equality operator '==' instead of the assignment operator '='.
    Last edited by pianorain; 02-15-2005 at 02:10 PM. Reason: Covering my tracks. ;)
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >#include <password>
    There is no need for this line.

    int password;
    There is no need for this line.

    > char Password;
    Declare this as a string:
    string Password;

    > if ( Password = JJ66547) {
    Instead this should be:
    if ( Password == "JJ66547") {

    > else if ( Password == adam ) {
    Same as above.

    To use strings, make sure to:
    #include <string>
    at the top.
    Last edited by swoopy; 02-15-2005 at 05:27 PM. Reason: edited if() after reading hk_mp5kpdw's post

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Code:
    #include <password>
    There is no password header (unless it is a custom header you've created and placed in with the rest of your compilers header files). This is where I believe your error is comming from.

    Code:
    char Password;
    This only stores a single character, not enough for what you need. You want to use a string container (in which case you also need to include the <string> header), or an array of characters (in which case you are likely to need the functions declared in the <cstring> header).

    Code:
    if ( Password = JJ66547) {
    = is the assignment operator, you want the equality operator == instead. Also, if using the string class, you would want enclose the text in red above in quotes "JJ66547". If using character arrays, you would need to use the strcmp function to test for equality.

    Code:
    else if ( Password == adam ) {
    Same as my previous comment above (with regards to enclosing the text within quotes). At least you got the operator correct here.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Heh, anyone else want to take a stab at it? Nice catch on the equality/assignment operator, hk_mp5kpdw. I'd hit you with some rep, but apparently I have to spread it around.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    2

    Thank you.

    Thank You all SO much! It worked great. I'm gonna continue to expand on it. And learn to loop a little bit better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help needed with simple code
    By Branka in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2005, 10:25 AM
  2. Help needed w/ line of code..Thanks!
    By aspand in forum C Programming
    Replies: 7
    Last Post: 05-30-2002, 04:12 PM
  3. Help Needed W/ Code..please!!
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 02:51 PM
  4. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21