Thread: Whats wrong with this simple code?

  1. #1
    Registered User
    Join Date
    Mar 2008

    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?


    #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";

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    You probably want a literal string "thisisapassword" (note the double quotes), not just thisisapassword.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    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):
    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:
    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
    Northern Virginia/Washington DC Metropolitan Area
    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
    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, 12:18 PM
  3. What's wrong with this code?
    By Luciferek in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2008, 12: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