cin.get() Not working

This is a discussion on cin.get() Not working within the C++ Programming forums, part of the General Programming Boards category; Ok I'm writing a program. The program works and all but the cin.get() is not keeping the console window open. ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    7

    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.

    Code:
    #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";
        cin.get();
        return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,176
    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:
    Code:
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cin.get();
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    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
    Posts
    14,185
    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, 05:08 AM

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