Thread: cin.get() Not working

  1. #1
    Registered User
    Join Date
    Nov 2008

    cin.get() Not working

    Ok I'm writing a program. The program works and all but the cin.get() is not keeping the console window open. I've been looking at this program for a while and have talked with others but we can not figure out what is wrong. I am using Code Blocks for my compiler. If anyone can point the obvious out for me it would be much appreciated.

    #include <iostream>
    using namespace std;
    float width;
    float length;
    float perimeter;
    float area;
    int main()
        cout << "Please input the length of the rectangle in centimeters\n";
        cin >> length;
        cout << "Please input the width of the rectangle in centimeters\n";
        cin >> width;
        perimeter = 2 * (width + length);
        area = width * length;
        cout << "The perimeter is " << perimeter << " centimeters\n";
        cout << "The area is " << area << " square centimeters\n";

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    When the user enters a value to be stored in width, the newline from that enter is left in the buffer. This newline is then consumed by cin.get().

    A simple solution is to forget about the cin.get() and just run your program via the command prompt. If you really do want to use it, then ignore the characters left on the buffer before using cin.get(), e.g., change cin.get() to:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    You would need to #include <limits> for std::numeric_limits. std::streamsize should already be available via <iostream>, but if you want to be safe then #include <iosfwd>. The net result is that you ignore as much as the stream can hold, and then wait for the input of a single character.
    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

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Doesn't Code::Blocks have a feature to keep the console window open after the program quits, though?
    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.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Quote Originally Posted by Elysia View Post
    Doesn't Code::Blocks have a feature to keep the console window open after the program quits, though?
    Yes. I suppose it could be disabled, although I haven't seen where you would do such a thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get(); doesn't seem to be working as expected
    By Diablo84 in forum C++ Programming
    Replies: 5
    Last Post: 03-30-2005, 07:00 PM
  2. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  3. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 01:58 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM