Thread: If else problem

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    4

    If else problem

    I am using a book called "C++: A Dialogue" in order to learn the language. I am stuck on the following code:
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    short balance;
    cout << "Please enter your bank balance: ";
    cin >> balance;
    if (balance < 10000)
    cout << "Please remit $20 service charge." << endl;
    else
    cout << "Have a nice day!" << endl;
    return 0;
    }
    I cannot get the compiled program to go beyond input after it says "please enter your bank balance". After I imput a number and press enter, the program simply closes. Can anyone tell me what I am doing wrong?

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    It works for me with MinGW/GCC 3.4.5. What compiler are you using?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    4
    I am using Bloodshed Dev-C++ 4.9.9.2.

  4. #4

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    4
    I tried using
    Code:
    cin.get();
    in order to keep it open, but it still did not work for me.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by darkfaust View Post
    I tried using
    Code:
    cin.get();
    in order to keep it open, but it still did not work for me.
    Put in a second one. After you type enter after entering the balance, the newline character is left in the input stream. The first cin.get() will read that one and the program will then keep going... which means it still ends. So, put in a second one right after the first and that one will block/wait for input.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    4

    Smile

    Thanks hk_mp5kpdw, that solved the problem. Thanks to everyone for the help.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could try indenting a bit better, you know... Surely, the book's code itself must be indented.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    another way

    Code:
    std::cin.get();
    std::cin.ignore();
    Double Helix STL

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    another way
    Actually, those statements should be swapped.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Actually, the FAQ might show more "correct" ways: there is no guarantee that only one character is left in cin (e.g user might type "102 lalalalala").

    Or simply make it a habit to run the program from a command window (where such code to keep the window "open" only becomes an annoyance).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Or set a breakpoint at the end of the program and run in debug mode or use a special run such as Visual Studio's Ctrl+F5.
    There are many ways... Running from the command line is not what everyone might like or understand.

    But otherwise, the STL has functions to eat whitespace. So eating all whitespace and then doing cin.get() should cause the prompt to pause.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I always prefer my writing my own pausing mechanism like:

    Code:
    std::cout << "Press [Enter] to continue...";
    std::cin.ignore( 256, '\n' );
    Though due to the stream buffer you might have to hit enter twice, but it's better than system("open the gates of h3ll!");
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    aaaahhh people, just work with Linux! no disappearing output screens, no breakpoints, no ignores or whatsoever, just a nice, fail proof, wysiRwyg*, terminal....

    * what you see is REALLY what you get

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by MarkZWEERS View Post
    aaaahhh people, just work with Linux! no disappearing output screens, no breakpoints, no ignores or whatsoever, just a nice, fail proof, wysiRwyg*, terminal....

    * what you see is REALLY what you get
    Actually, if you run a console program from an IDE in Linux, I'm pretty sure it disappears too [assuming the IDE doesn't perform some "magic" to prevent that]. Which turns your argument into "just use command line" - which is far enough except modern programmers don't know how... ;-)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM