Thread: Is my Console/computer borken?

  1. #1
    Registered User
    Join Date
    Sep 2016
    Posts
    1

    Question Is my Console/computer borken?

    So I am new to coding and i have been taught using Code Blocks in school. When i enter code into code blocks it does what i want and then comes up with a message at the end like "process returned 0, press enter to continue". This is exactly what i want but i cant export all my code as a single solid EXE. So i tried using Microsoft visual studio and whenever i get done with the cin (code down below) it briefly pops up my next cout and then the console goes away. Everything i have tried, like adding cin.get() below the final cout line doesn't do anything. The only thing that will work is adding a new int or char and having to enter a value in before i press enter. Why is it doing this?



    Sample code:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
        int a;
        cout << "Hello world!" << endl;
        cout<<"What is your age?";
        cin>>a;
        cout<<"\n You will be 30 in "<<30-a<<"years.";
       //this is where i put the cin.get()
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Open up a cmd.exe window and run the exe from that.

    Your question makes no sense to me; so, I guessed on the most likely answer.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    Ask yourself a question - what does cin.get() actually do? Well, it extracts a char out of the input buffer. What is input buffer consisting of after cin >> a in your code? Say you entered "18" - the buffer will consist of "18\n". Yes, that '\n' is there. Then >> operator extracts 18 out of the buffer and does its magic to turn that string into an int value to store in your variable a. Now the buffer is consisting of... "\n". That's all.

    If you'd now do another cin >> a, and you'd enter "20", your buffer would look like this: "\n20\n". Then >> operator will start looking for a string that can be considered "numerical" but - aha! - it is programmed to ignore initial whitespace, and '\n' is considered as such. So, it will extract it and ignore it, then extract "20" and turn it into an int to store in your variable a. Then the buffer will be consisting of another lonely "\n".

    Now if you do cin.get() - you'll exctract the next char in the input buffer... That being our lonely newline character, because cin.get() does not ignore whitespace. Why should it?

    Since there's already a character to extract in input buffer you never get to be asked for it through the terminal. However, if you clear the input buffer just before doing that, it will work as expected.

    Code:
    cin.clear(); // check the docs to see what it does
    cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n') // ignores any amount of input till newline
    cin.get();
    However, you're probably better off with not using it at all. You could open your terminal and navigate (cd) to your working directory. There are terminal emulators on both *nix and Windows that are prettier than your standard cmd window (ConEmu is nice, with tabs and flexible display). Don't be afraid to use it. Also, usually when using a good IDE it will keep it open for you anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Computer Science vs Computer Engineering degree
    By PCG33K in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-03-2007, 07:13 AM
  2. Win32 Console App... multiple console buffers
    By Trauts in forum Windows Programming
    Replies: 2
    Last Post: 04-28-2003, 11:26 AM
  3. Replies: 23
    Last Post: 01-31-2003, 03:13 AM
  4. Would You Buy A Computer Like This?
    By Soldier in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-01-2002, 08:38 PM
  5. computer science or computer engineering??
    By pkananen in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 02-26-2002, 12:10 PM

Tags for this Thread