Simple C++ code doesn't work

This is a discussion on Simple C++ code doesn't work within the C++ Programming forums, part of the General Programming Boards category; Can anyone tell me why my code doesn't work. I'm simply doodling with C++ and because of some knowledge in ...

  1. #1
    Registered User
    Join Date
    May 2006
    Location
    Grandrapids Michigan
    Posts
    7

    Simple C++ code doesn't work

    Can anyone tell me why my code doesn't work. I'm simply doodling with C++ and because of some knowledge in PHP, I thought I could put together a simple login program.

    Code:
    #include <stdio.h>
    #include <iostream.h>
    
    int main() {
    
    char username[10];
    char password[10];
    char user[10];
    char pass[10];
    
    "happy" == user;
    
    "sad" == pass;
    
    cout << "Please login. \n Username:";
    cin >> username;
    
    cout << "Password: ";
    cin >> password;
    
    if(user == username && pass == password) {
    
    cout << "Login complete!";
    
    
    } 
    else
    {
    
    cout << "Phail!";
    
    }
    }
    No matter what I type in, it always returns PHAIL!, even if if I type in the correct username and password.

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Code:
    #include <iostream>
    #include <string>
    
    int main() {
    
         std::string username = "happy";
         std::string password = "sad";
         std::string user, pass;
         std::cout << "Please login. \n Username:";
         std::cin >> username;
    
         std::cout << "Password: ";
         std::cin >> password;
    
         if(user == username && pass == password) {
    
              std::cout << "Login complete!";
         } 
         else
         {
              std::cout << "Phail!";
    
         }
    }

    First: stdio.h is not needed, that is a C io header, you are using iostream which is C++.
    Second: There is no iostream.h. If you see any reference to that, it's from pre-1998 C++ (before the language was standardized at all) and no longer is valid.
    Third: == tests for equality, and returns true or false. = is for assigning.
    Fourth: You can only assign from right to left. x = "hi"; is correct. "hi" = x; is not.
    Fifth: Use std::string, not character arrays. Character arrays CANNOT be compared with ==, but std::strings can (among many other things like std::strings resistance to buffer overruns)
    Last edited by Cat; 10-18-2006 at 01:45 AM.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,479
    Here is another way to tackle your program using string instead of char arrays:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    #include <string>
    
    using std::string;
    
    // main function - driver //////////////////////////////////////////////////////
    //
    int main ( void )
    {
       string login;	// user login string
       string pass; 	// user password string
       int flag = 0;	// works with logical NOT
       
       cout << "Enter your login name: ";
       cin >>login;
       
       cout << "\nEnter your password: ";
       cin >> pass;
       
       // logical AND
       if (( login == "willow" ) && ( pass == "ada" ))
       {
          cout << "\nWelcome to the network" << endl;
          flag = 10; // prevent logical NOT executing
       }
       
       // logical OR
       if (( login == "guest" ) || ( pass == "guest" ))
       {
          cout << "\nGuest login lasts for 24 hours" << endl;
          flag = 5; // prevent logical NOT executing
       }
       
       // logical NOT
       if ( !flag )
       {
          cout << "\nAcsess denied" << endl;
       }
       
       cin.get();  // freeze console window
       cin.ignore();
        
       return 0;   // indicate program ended sucsessfuly
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    19
    Code:
    std::
    if you write using namespace std; at the begining of your code (straight after the includes) you will not have to prefix commands with std::

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    Quote Originally Posted by rickyoswaldiow
    Code:
    std::
    if you write using namespace std; at the begining of your code (straight after the includes) you will not have to prefix commands with std::
    Importing the entire namespace is generally not reccomended.
    "There are three kinds of people in the world...
    Those that can count and those that can't."

  6. #6
    "Why use dynamic memory?"
    Join Date
    Aug 2006
    Posts
    186
    the
    Code:
    ==
    mark is used in if, but if u wanna declare variable u should use
    Code:
    =
    "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg."-Bjarne Stroustrup
    Nearing the end of finishing my 2D card game! I have to work on its 'manifesto' though <_<

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by rickyoswaldiow
    Code:
    std::
    if you write using namespace std; at the begining of your code (straight after the includes) you will not have to prefix commands with std::
    Yeah, but that tends to work well for small, beginner programs but makes more advanced programs harder to read and write. I say, don't learn anything you're going to spend time unlearning later.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program won't work on a friend's comp
    By Ouendanation in forum C++ Programming
    Replies: 6
    Last Post: 01-25-2008, 08:03 PM
  2. Replies: 7
    Last Post: 11-15-2007, 01:36 AM
  3. Replies: 4
    Last Post: 11-10-2007, 06:02 PM
  4. Replies: 9
    Last Post: 09-09-2007, 09:08 AM

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