cant figure it out

This is a discussion on cant figure it out within the C++ Programming forums, part of the General Programming Boards category; I need help to understand this problem. Code: #include <iostream> using namespace std; int main () { int password; cout<<"Password: ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    cant figure it out

    I need help to understand this problem.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main ()
    {
        int password;
        
        cout<<"Password: ";
        cin>> password;
        cin.ignore();
        if (password == 100) {
                cout<<"Access cleared\n";
                }
        else {
             cout<<"Access denied\n";
             }
        cin.get();
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    how come the compiler stops when i put david for 100?
    how do i put an alfabeth password? do i need char?

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    must i put char at
    int password?

  4. #4
    adr
    adr is offline
    Registered User
    Join Date
    Dec 2005
    Posts
    155
    you could use a char or a string for alfabeth password.
    string password; //<~~string form

    Also it stops b/c the int is looking for a interger not a word or letter unlike a char or string.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > how come the compiler stops when i put david for 100?
    Be careful of your terminology, when you run the program you're no longer anywhere near the compiler.

    The compiler turns your source code into an executable.
    You then run your executable and enter input.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    Quote Originally Posted by Salem
    > how come the compiler stops when i put david for 100?
    Be careful of your terminology, when you run the program you're no longer anywhere near the compiler.
    the compiler stops saying something about an error and it makes the line with the 100 [or david] red.

    How would the code be then?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well post your error messages then!
    Or try www.psychic-programming-network.com

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    here look.
    Attached Images Attached Images  

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Right 2 things wrong with it.

    if(password==david)

    The compiler thinks that david is a variable that you havent declared. You are trying to use it as a literal. You need to put speech marks round david like so:

    if(password=="david")


    second of all you have declared password as an integer and it needs to be of type char*.

    change:
    int password;
    to
    char* password;

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    i thought so about the char. What about the asterisk?

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Quote Originally Posted by cloudy
    second of all you have declared password as an integer and it needs to be of type char*.

    change:
    int password;
    to
    char* password;
    What it needs to be (in order to be used in the context you are suggesting, i.e. using the equality operator == to compare with a string literal) is a string and not an uninitialized pointer pointing to god knows where.



    Quote Originally Posted by Dark Ares
    i thought so about the char. What about the asterisk?
    Ignore that for now (it means that the variable is a pointer), just use a string.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        string password;
        
        cout<<"Password: ";
        cin>> password;
        cin.ignore();
        if (password == "david") {
                cout<<"Access cleared\n";
                }
        else {
             cout<<"Access denied\n";
             }
        cin.get();
    }
    "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

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    you dont have to use the asterix, if you wanted to you could do

    char password[20];

    you can replace the number 20 with what ever integer you want, but the password cannot exceed that length.

    This is some basic stuff, do you have any c books because the first 2 chapters can help you with this stuff mate.

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    try:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        string password;
        
        cout<<"Password: ";
        cin >> password;
    
        if (password == "David") 
    	{
    		cout<<"Access cleared\n";
    	}
        else
    	{
    		cout<<"Access denied\n"; 
    	}
    	return 0;
    }
    This will also work for numbers:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main ()
    {
        string password;
        
        cout<<"Password: ";
        cin >> password;
    
        if (password == "42") 
    	{
    		cout<<"Access cleared\n";
    	}
        else
    	{
    		cout<<"Access denied\n"; 
    	}
    	return 0;
    }
    I think 42 was an appropriate choice don't you? May not be the untimate answer, but it's pretty good.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    i just started an hour ago with the c++ tutorial on this site and whanted to try my learnings. the 20 in the char password[20] is the max lenght?
    i will try now

  15. #15
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Quote Originally Posted by Dark Ares
    the 20 in the char password[20] is the max lenght?
    Yeah, it is. However it's easier if you #include <string> cause you don't have to declare its length. You can declare strings by going:

    Code:
    string mystring1 = "Place ";
    string mystring2 = "text ";
    string mystring3 = "here!";
    string mystring4 = mystring1 + mystring2 + mystring3;
    Output:

    Code:
    Place text here!
    But if you're used to doing the char[len], it's just as good. You have to remember that if you want a string with 10 characters in it, you must allow for 11, this is because the last element of any string must be the terminating character or something.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Figure out how method was called?
    By jcafaro10 in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2009, 09:43 AM
  2. 3 dimensional figure volume
    By thekautz in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2009, 04:22 PM
  3. trying to figure out someone's code for a plugin
    By paulpars in forum C++ Programming
    Replies: 4
    Last Post: 07-20-2006, 10:57 AM
  4. newb to C, cant figure something out.
    By Phate4219 in forum C Programming
    Replies: 16
    Last Post: 03-06-2006, 12:47 AM
  5. ahh i can't figure out this loop
    By blindleaf in forum C Programming
    Replies: 1
    Last Post: 03-18-2003, 08:42 AM

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