Why doesnt this work

This is a discussion on Why doesnt this work within the C++ Programming forums, part of the General Programming Boards category; Okay again a very basic question, I followed the tutorial step by step and it does not want to work. ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    19

    Angry Why doesnt this work

    Okay again a very basic question, I followed the tutorial step by step and it does not want to work. It executes fine but it will not say the passwords match

    <code>
    #include <iostream.h>

    void main()
    {
    //declerations
    char userName[24] ="";
    char pw[8]="";
    char pw2[8]="";

    cout << "Enter a user ID: ";
    cin >> userName;
    cout << "Enter a password: ";
    cin >> pw;
    cout << "";
    cout << "Re-Enter your password: ";
    cin >> pw2;

    if (pw==pw2)
    {
    cout<<"Your passwords match"<<endl;
    }
    else
    {
    cout<<"Your passwords do not match"<<endl;
    }

    }

    </code>

    It takes two seconds to read you will understand what I mean
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    ><code>
    Square brackets, not angle brackets.

    >void main()
    int main(). Even if you're using a compiler that refuses to support the new headers and namespaces, void main is still wrong.

    >if (pw==pw2)
    pw and pw2 are pointers. This isn't doing what you want. Try including <string.h> (preferably <cstring>) and use strcmp:
    Code:
    if ( strcmp ( pw, pw2 ) == 0 )
    >It takes two seconds to read
    That depends on how well one reads code with a zero indention.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    19
    I thought the <string.h> would fix it, but it is still returning the passwords don't match
    jotun

    n : (Norse mythology) one of a race of giants often in conflict with the Aesir [syn: Jotun, Jotunn]

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but it is still returning the passwords don't match
    Then you aren't doing it right. This works fine:
    Code:
    #include <iostream> 
    #include <cstring>
    
    using namespace std;
    
    int main() 
    {
      //declerations
      char userName[24] ="";
      char pw[8]="";
      char pw2[8]="";
    
      cout << "Enter a user ID: ";
      cin >> userName;
      cout << "Enter a password: ";
      cin >> pw;
      cout << "";
      cout << "Re-Enter your password: ";
      cin >> pw2;
    
      if ( strcmp ( pw, pw2 ) == 0 )
      { 
        cout<<"Your passwords match"<<endl;
      }
      else
      {
        cout<<"Your passwords do not match"<<endl;
      }
    }
    The only changes I made were strcmp, including <cstring>, making main return int, and using the new headers because the compiler I'm using right now complains if I don't.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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