Bitwise operator test - It keeps skipping past a certain point

This is a discussion on Bitwise operator test - It keeps skipping past a certain point within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I'm currently reading "C++ Weekend Crash Course" by Stephen R. Davis. This is the first book I've read ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Question Bitwise operator test - It keeps skipping past a certain point

    Hello everyone, I'm currently reading "C++ Weekend Crash Course" by Stephen R. Davis. This is the first book I've read about C++. I'm still learining the basics. Anyways, In the chapter about "Logical Operations" there is a section focusing on "working with binary numbers", "performing bitwise operations", and "creating logical assignment statements". The book provides code for a "bitwise operator test". Here's the part that doesnt seem to work for me.

    Code:
    // input the first argument
        int nArg1;
        cout << "Enter arg1 as a four-digit hexadecimal: ";
        cin >> nArg1;
        
    //***Right here***    
        int nArg2;
        cout << "Enter arg2: ";
        cin >> nArg2;
        
        cout << "nArg1 & nArg2 =0x"
                  << (nArg1 & nArg2) << "\n";
                  
        cout << "nArg1 | nArg2 = 0x"
                  << (nArg1 | nArg2) << "\n";
                  
        cout << "nArg1 ^ nArg2 = 0x"
                  << (nArg1 ^ nArg2) << "\n";

    Everytime I run the program it works fine until it gets to the second argument. It skips right past the whole part of the code begining at "int nArg2; cout << "Enter arg2: "; cin >> nArg2;"
    and goes right into the next lines of code. Everything looks right to me. I've played around with programs that were more complex then this one and have gotten them working correctly. Any help would be greatly appreciated. I'm using Dev-C++.

    Oh and another thing, when I run my compiled programs soon as it gets to the end of the program the DOS prompt closes not giving me enough time to read my programs usally. Anyone know how I can remedy this? I've searched around through Dev-C++ looking for an option that would allow me to keep the programs open even after there finished but couldnt find anything. Thanks.
    Last edited by euphoric; 10-30-2002 at 01:38 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That runs just fine for me.

    If you don't want the console to close, put a getchar() at the end of the program (located in stdlib.h), or preferably getch() (but then you need Borland).
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    It skips because entering something like "2B5A" into nArg1, will assign 2 to nArg1, as the next character is not numeric. Then when you get to nArg2, "B5A" is left in the input buffer. It then attempts to assign this to nArg2.

    I don't think cin will allow you to input a number in hex.. you could try cin.sef(ios::hex), if not, then you may be left to write your own input routine for these hexadecimal numbers... or stick with decimal.

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    to keep the window open in DEV, add this bevore return:
    Code:
    if(getche());
    with
    Code:
    #Include <conio.h> // getche();
    at the top.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072

    Re: Bitwise operator test - It keeps skipping past a certain point

    Originally posted by euphoric

    int nArg1;
    cout << "Enter arg1 as a four-digit hexadecimal: ";
    cin >> nArg1;
    should be:
    Code:
        int nArg1;
        cout << "Enter arg1 as a four-digit hexadecimal: ";
        cin >> hex >> arg1;
    Remember to include <iomanip> or whatever header std::hex is located in.

    (edit)I missed Eibro's post(/edit)
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  6. #6
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    Ok, I see what was the problem. I dont really know to much about binary/hexadecimal numbers at the moment. I didn't really understand that chapter to much. Anyone know some good tuts for binary/hex? I guess I the first numbers I was entering in for Arg1 was doing what you said Eibro. I do have this at the top of the program.

    Code:
    // set output format to hexadecimal
        cout.setf(ios::hex);
    Thanks for the help everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. observing whether a point is in a triangle???
    By hebele in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2003, 03:38 PM
  2. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  3. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  4. point lies in circle?
    By cozman in forum Game Programming
    Replies: 3
    Last Post: 12-20-2001, 03:39 PM
  5. more with my 1st structure program
    By sballew in forum C Programming
    Replies: 42
    Last Post: 10-22-2001, 08:03 PM

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