Thread: Question ?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Question ?

    WHen i compile my code nothing happens at first it looked like something was popping up. Now it looks like it is doin nothing. Below is my code

    Code:
    # include <iostream>
    using namespace std;
    int main()
    
    {
        int x;
      x = 0;
      do {
        int password; 
        cout<<"Enter Password";
        cin>> password;
        cin.ignore();
    if ( password == 100 ) {
                  cout<<"Correct Password Entry";
                  }
                  else {
                       cout<<"Invalid Password /n";
                       }
                       } while ( x==0 );
                       cin.get();
                       }
    It should ask for a key and loop the question things. Im a HUGE newb so can someone explain to me my stupidity thanks.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Step 1 - learn how to format code
    Code:
    # include <iostream>
    using namespace std;
    int main()
    {
        int x;
        x = 0;
        do {
            int password;
            cout << "Enter Password";
            cin >> password;
            cin.ignore();
            if (password == 100) {
                cout << "Correct Password Entry";
            } else {
                cout << "Invalid Password /n";
            }
        } while (x == 0);
        cin.get();
    }
    2. /n is not a newline, \n is. Besides, in C++, it's bettter to use endl;
    3. cout<<"Enter Password";
    Output without a newline may not be seen due to buffering. Calling cout.flush(); will ensure that your prompt will be seen.
    4. You never change the value of x inside the loop, so it loops forever no matter what you do.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    Quote Originally Posted by Salem
    Besides, in C++, it's bettter to use endl;
    Why is it better to use std::endl, rather than just hard-coding a newline into your text? Just curiousity.

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    endl calls flush and thus guarantees that everything in the outputbuffer is seen/written/something else.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    3. cout<<"Enter Password";
    Output without a newline may not be seen due to buffering. Calling cout.flush(); will ensure that your prompt will be seen.
    Except that cin and cout are tied streams

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Thanks guy. Also i want it to loop forever so thats good. I used your code and i have the same problem shouldnt a black dos box come up? then ask me the stuff and loop it forever?

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    The code Salem posted is the same code you posted, but formatted to make it easier to read. Have you fixed the problems pointed out to you above? Are you getting any compiler errors?

  8. #8
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by marsh
    Thanks guy. Also i want it to loop forever so thats good. I used your code and i have the same problem shouldnt a black dos box come up? then ask me the stuff and loop it forever?
    Yes, and it does....it works fine.

    Sure you're using a compiler and not Notepad?

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    IM using dev C++. I will make the changes i thought he already made them sorry for wasting your time guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM