New to C What Is wrong with this...

This is a discussion on New to C What Is wrong with this... within the C++ Programming forums, part of the General Programming Boards category; Take a look at the code below, when I enter the password "test" I get the message incorrect password. Why ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    31

    New to C What Is wrong with this...

    Take a look at the code below, when I enter the password "test" I get the message incorrect password.

    Why is this. Also is there a way to define passwords via a text file.

    And put a line break between printed messages.

    And Auto display both lines? Without user input?


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    char string[25];
    
        cout<<"Welcome to the UK Memory.com inventory system";
        cin.get();
        cout<<"Enter Your Password then press enter to continue!";
        cin>> string;
        cin.get();
    
        if (string == "test")
            cout<<"Welcome Karl";
        else {cout<<"Incorrect Password! Try Again";}
    
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You cannot use "==" to compare two C style strings. You need to use strcmp(). Since you appear to be using C++ instead of C, it would be better if you used std::string instead of C style strings in the first place. If you use std::string, then you can use std::getline(std::cin, my_string) to get user input.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by headshot119
    Take a look at the code below, when I enter the password "test" I get the message incorrect password.

    Why is this.
    Because you cannot compare null terminated strings with ==. You should use std::strcmp for this, from the <cstring> header.

    A better approach would be to #include <string> and use a std::string object. This also means that have to either get rid of that using directive, or rename your string.

    Quote Originally Posted by headshot119
    Also is there a way to define passwords via a text file.

    And put a line break between printed messages.

    And Auto display both lines? Without user input?
    Yes. Yes. Yes. Yes. Take things one step at a time.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Sorry I'm new to C++, I just followed some of the C++ tutorials on the main page to cobble this together, and what you said went way over my head.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Here is a short example on how to use std::string:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string s;
        std::cin >> s;
        if(s == "test")
            std::cout << "Welcome Karl\n";
    }
    Note that you can use "==" when comparing C++ strings (which you cannot do with C style strings).
    bit∙hub [bit-huhb] n. A source and destination for information.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Figured out the string bit thanks bithub. Just need answers to how to do the other things?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by headshot119 View Post
    Figured out the string bit thanks bithub. Just need answers to how to do the other things?
    What things (specifically)?
    bit∙hub [bit-huhb] n. A source and destination for information.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Also is there a way to define passwords via a text file.

    And put a line break between printed messages.

    And Auto display both lines? Without user input?

    EDIT

    How can I use a loop so that if an incorrect password is given it asks for it again? Think I'd use a do while loop here, I maybe wrong.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Also is there a way to define passwords via a text file.
    Yes. You need to read up on how to do file I/O with C++.

    And put a line break between printed messages.
    Yes. The '\n' character is used to print a line break.

    And Auto display both lines? Without user input?
    I'm not sure I understand. The only reason your original program requires user input is that you asked for it. See the cin.get() statements?
    bit∙hub [bit-huhb] n. A source and destination for information.

  10. #10
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    You misunderstood me, I wanted it to display two lines of text, without the user having to press enter in between.

    EDIT

    Doesn't matter fixed once I thought about it logically.

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    
        cout<<"Welcome to the UK Memory.com inventory system \n Enter Your Password then press enter to continue!";
        std::string password;
        std::cin >> password;
    
        while (password != "test")
            std::cout<<"Incorrect Password";
    
        if (password == "test")
            std::cout<<"Welcome User";
    }
    How do I stop the infinite loop when you execute this code?
    Last edited by headshot119; 08-07-2009 at 12:35 PM.

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    How do I stop the infinite loop when you execute this code?
    That depends. You need to start thinking like a programmer. What are your intentions with that loop? Did you mean to do this?

    Code:
    while (password != "test")
    {
            std::cout<<"Incorrect Password";
            std::cin >> password;
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Your right I do need to think like a programmer, when I sit back and thing logically after seeing your code it makes much more sense.

    Just read up on files I/O. I understand how to call them or create them, but not how to use the bit inside it.

    EDIT

    How do you neaten the output of the code, at the moment when you enter a wrong password asks you to enter it again, but when you type it appears right next to the printed message, rather than on a new line.

    EDIT 2.

    Once again I followed your advice, looked at something else for a bit, looked at the code and realized that adding a simple "\n" solved my problem.
    Last edited by headshot119; 08-07-2009 at 12:47 PM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by headshot119 View Post

    EDIT

    How do you neaten the output of the code, at the moment when you enter a wrong password asks you to enter it again, but when you type it appears right next to the printed message, rather than on a new line.
    Print a new line?

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    I realized that, read my second edit in that post.

  15. #15
    Registered User
    Join Date
    Aug 2009
    Posts
    31
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    //Main Strings
    std::string Password;
    std::string AdministratorPassword;
    
    //Main Menu
    std::string MainMenu;
    
    //Edit Menu
    std::string EditMenu;
        std::string AddItem;
        std::string EditItem;
    
        cout<<"Welcome to the UK Memory.com inventory system \n Enter Your Password then press enter to continue! \n";
        std::cin >> Password;
    
    while (Password != "test")
    {
            std::cout<<"Incorrect Password \n Please Re-enter your password \n";
            std::cin >> Password;
    }
    
    //I need to add multiple password support, and user configurable passwords
        if (Password == "test")
            std::cout<<"Welcome User \n What would you like to do now? \n 1> Lookup item. \n 2> Packing mode. \n 3> Edit mode. \n \n \n Enter your selection. \n " ;
    //Menu Selection Start Here.
            std::cin >> MainMenu;
    
        if (MainMenu == "1")
            std::cout<<"Lookup Mode selected \n Please scan the item's barcode and press enter, or type other for more options." ;
    
        if (MainMenu == "2")
            std::cout<<"Packing Mode selected \n Scan the order barcode to begin." ;
    
        if (MainMenu == "3")
            std::cout<<"Edit Mode selected \n Please enter administrative password to begin. \n" ;
    
             std::cin >> AdministratorPassword;
    
                    while (AdministratorPassword != "admin")
    {
                            std::cout<<"Incorrect Password \n Please Re-enter your password \n";
                            std::cin >> AdministratorPassword;
    }
                    if (AdministratorPassword == "admin")
                            std::cout<<"Edit Mode confrimed \n Please select an option. \n 1> Add item \n 2> Edit item \n 3> Delete iten. \n 4> Password configuration \n" ;
    
                                std::cin >> EditMenu;
    
                                if (EditMenu == "1")
                                    std::cout<<"Add item mode. \n Please enter the item's details using the following syntax \n Item Number ; Item Barocde ; Item Name ; Stocked quantity \n" ;
    
                                        std::cin >> AddItem;
    
                                        if (AddItem != "*******;*******;********;********" )
                                            std::cout<<"Syntax incorrect. The correct syntax for this operation is...\n Item Number ; Item Barocde ; Item Name ; Stocked quantity";
    
                                if (EditMenu == "2")
                                    std::cout<<"Edit item mode." ;
    
                                if (EditMenu == "3")
                                    std::cout<<"Delete item mode." ;
    
                                if (EditMenu == "4")
                                    std::cout<<"Password configuration mode. \n Please re-enter administrative password \n" ;
    
                                            std::cin >> AdministratorPassword;
    
                                        while (AdministratorPassword != "admin")
    {
                                            std::cout<<"Incorrect Password \n Please Re-enter your password \n";
                                            std::cin >> AdministratorPassword;
    }
                                        if (AdministratorPassword == "admin")
                                            std::cout<<"Password configuration mode confirmed." ;
    }
    //Edit mode needs another menu, giving access to various options including, adding / editing items, password editing ect.
    When I go to edit mode, then try to access option 2 3 or 4, it doesn't do anything which I suspect is to do with line 59 interfering with the selection choice. How Do I fix this?

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

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

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