Whats wrong with this simple code?

This is a discussion on Whats wrong with this simple code? within the C++ Programming forums, part of the General Programming Boards category; Allright, i'm a total beginner, and i'm just testing stuff. Well i was trying to make this password check, but ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Whats wrong with this simple code?

    Allright, i'm a total beginner, and i'm just testing stuff.

    Well i was trying to make this password check, but it won't run...

    Something about thisisapassword being an undecleared identifier.

    Could someone please tell me how it should look like?

    please.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char string[256];                               // A nice long string
    
      cout<<"======== WELCOME TO MY TOP SECRET PROGRAM =======\n";
      cout<<"Password: ";
      cin.getline ( string, 256, '\n' );              // Input goes into string
      if(string==thisisapassword) {
    	  cout<<"Correct password!";
      }
      else {cout<<"Wrong password";
      cin.get();}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,114
    You probably want a literal string "thisisapassword" (note the double quotes), not just thisisapassword.
    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

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Another problem is that you are comparing C style strings with ==. That doesn't work because it ends up comparing pointers that can be different even if the strings are the same. In C, you use strcmp to compare strings.

    However, this is C++, and there's no reason to use C style strings. Add #include <string> and change your string variable to have the type string (you'll want to give it a different name so that the names don't conflict):
    Code:
    string userPassword;
    Once you do that, you'll want to change the version of getline you're using because the one for C++ strings is different:
    Code:
    getline ( cin, userPassword );
    Then you can use == like your code does now and it will work as you expect.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    And since "string" is a character array and not a C++ string container/object you'd need to use the strcmp function as well... or you can just use an actual string object (you'd likely have to change the name of the variable though).

    [edit]Drat, beaten by mere moments![/edit]
    "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
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    Thanks for the reply, ill try to use the c++ string instead, I had only learnt the C ones ;D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with simple piece of code
    By Furious5k in forum C++ Programming
    Replies: 10
    Last Post: 12-12-2008, 05:25 PM
  2. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 01:18 PM
  3. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 01:02 PM
  4. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM

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