Thread: Newbie needs help plz

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    Unhappy Newbie needs help plz

    Hi
    I am learning C++ for the first time; I use Dev C++ as i am 14 with no cash;
    My programe has two if statments in and i use the cin.get(); thing to see what i have done. but after the second if statment, the dos window with my programe in it, is gone.
    What do i do; can someone help me plz


    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char N;
        char Y;
        int uname;
        int pword;
        
        cout << "Welcome to the hackers gateway\n" <<endl;
        cout << "Do you have an account? Y or N (You need to say 'N' if you say"; <<endl;
        cin >> N;
       
        if (N == true); 
           { 
                 cout << "Please type in your username\n";
                 cin >> uname;
                 cout << "And now your Password";
                 cin >> pword << endl;
                 cout << "You now have grated access to the hackers gateway\n";
                 cout << "Well done!";
                 cin.get();
            } 
            
            cout << "To gain access to the hackers gateway";
            cout << "Please enter your username\n";
          
            cin >> uname;
            
            cout << "Please enter your Password";
            
            cin >> pword;
            
            cout << "Checking username and password....";
          
            if (uname AND pword == true);
               {
                      cout << "Hello and Wellcome to the hackers gateway"; <<endl;
                                 
                                     
               }
            
     
        
        cin.get();
        return 0;
    }

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    You need to clear the input buffer, there are additional '\n' in the input stream from where you took in answers... look into cin.ignore();

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I supposed you are writing in Windows. It is a console prograqm. As you are new, read the main site and forum FAQ before sending your question. You get your answer faster and better there.
    add
    Code:
    cin.ignore(2);
    before cin.get() .
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    thanks for the advice but still no luck

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Worked it out!
    Thanks!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Worked it out!
    Could you specify how you worked it out for people with the same question?
    My best code is written with the delete key.

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> if (uname AND pword == true);

    Do you need the ';' there, and the AND ... is that legal?

    I would use:

    Code:
    if ( uname == true && pword == true )
    {
    	cout<< "You have successfully logged in!";
    }
    or something. However, it would work better if you had:

    Code:
    if ( uname == 42 && pword == 42 )
    // you have set the username and the password
    {
    	cout<< "You have successfully logged in!";
    }
    else
    {
    	cout<< "That is not a valid username or password!";
    }
    You can set the numbers of the username and the password. It may be better to use a string for the username though.
    Last edited by twomers; 07-27-2006 at 02:42 PM.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    27
    I did this
    Code:
      if (uname, pword == true);
    worked fine =P
    The semi colo after the parenthesis are uneeded as well

    But, alas, im 16 and just started on this language as well...lol

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    There have been multiple discussions about using commas in if statements on this board... I will look for one later and link it but for now. How you have that written. It only evaluates pword == true... uname isn't even considered.

    And in the OP code, it just evaluates uname to be non-zero/null

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Take my word for it, it doesn't work as you wish. Try setting the password in the if statement to a number (999 for instance), and see what happens when you enter the wrong uname.

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    If you see my and Wraithan answer is the same, its because we send it at the same time. Unfortunately I only solved the problem that OP pointed I didn't mention whole the code.
    Now it seems that OP problem is solved somehow, but I have question: How OP code can ever be compiled with that AND?
    I remember a header with these defines:
    Code:
    #define AND &&
    #define OR ||
    ...
    But this header is not included in OPs code.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Registered User
    Join Date
    Nov 2005
    Posts
    34
    i hade alot of hard times geting the cin.get to work all the time so what i do is found a way to do it whitout that


    i use

    #include <cstdio> for use in system ........ and use

    system("PAUSE");

    hope it helps u if u dont got it=)

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    >>i hade alot of hard times geting the cin.get to work all the time so what i do is found a way to do it whitout that

    If you couldn't make it work, its because you didn't read about it. Like now. If you just read other posts of this thread, you can make it work. And don't use that unsafe method.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> #define AND &&
    >> #define OR ||

    Bad bad bad. That's like saying:

    #define begin {
    #define end }
    blah blah

    then you can do C or C++ in pascal. There's no need to do the above, && and || etc are fine IMO.

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    I didn't say they are good. AND that header came with my MSVS. AND you didn't answer my question.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie c programmer need assistance plz...
    By Nexus-ZERO in forum C Programming
    Replies: 17
    Last Post: 12-27-2007, 04:05 PM
  2. im newbie to opengl, help plz
    By NecroFromHell in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2006, 08:24 PM
  3. newbie reading a file..how? plz help
    By iori in forum C Programming
    Replies: 3
    Last Post: 04-09-2003, 03:58 PM
  4. HI .. newbie to the board... need some help plz
    By Arwen27 in forum C Programming
    Replies: 3
    Last Post: 04-28-2002, 07:27 AM
  5. Newbie Source Code question. Hlp Plz?
    By BOOGIEMAN in forum Game Programming
    Replies: 4
    Last Post: 12-14-2001, 04:30 AM