Why Is the Console Closing After I'Ve Included Cin.Get()

Why is the Console Closing after I've included cin.get()?

cin >> carrots;

This line leaves a trailing newline token in the input stream, which then gets consumed by the next cin.get(). Just do a simple cin.ignore() directly before that:

cin.ignore();
cin.get();

Prevent console from closing

Do it like this:

// do your stuff here

// prevent console from closing
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();

What type of situations cause the cin.get() function to not work?

Using cin.get(), you get only one of those characters, treated as char.
You would not able see the output as the command prompt will close as soon as the program finishes. Putting cin.get() forces the program to wait for the user to enter a key before it will close, and you can see now the output of your program.

 using namespace std;
int carrots;
cout << "How many carrots do you have?" << endl;
cin >> carrots;
cout << "You have " << carrots << endl;
cin.get();
cin.get();// add another one
return 0;

How to stop C++ console application from exiting immediately?

Edit: As Charles Bailey rightly points out in a comment below, this won't work if there are characters buffered in stdin, and there's really no good way to work around that. If you're running with a debugger attached, John Dibling's suggested solution is probably the cleanest solution to your problem.

That said, I'll leave this here and maybe someone else will find it useful. I've used it a lot as a quick hack of sorts when writing tests during development.


At the end of your main function, you can call std::getchar();

This will get a single character from stdin, thus giving you the "press any key to continue" sort of behavior (if you actually want a "press any key" message, you'll have to print one yourself).

You need to #include <cstdio> for getchar.

Preventing console window from closing on Visual Studio C/C++ Console application

Starting from Visual Studio 2017 (15.9.4) there is an option:

Tools->Options->Debugging->Automatically close the console

The corresponding fragment from the Visual Studio documentation:

Automatically close the console when debugging stops:

Tells Visual Studio to close the console at the end of a debugging session.

std::cin:: and why a newline remains

Because that's its default behavior, but you can change it. Try this:

#include<iostream>
using namespace std;

int main(int argc, char * argv[]) {
char y, z;
cin >> y;
cin >> noskipws >> z;

cout << "y->" << y << "<-" << endl;
cout << "z->" << z << "<-" << endl;
}

Feeding it a file consisting of a single character and a newline ("a\n"), the output is:

y->a<-
z->
<-

Console window won't stay on screen despite cin.get(); or system(pause)

You are not calling your printoscreen function. Try adding printtoscreen(); before the end of your main() function.

EDIT:

Also consider changing int printoscreen(){ to void printoscreen(){ and correspondigly return 0; to just return; in this function, as you are not returning anything meaningful, and are ignorig result value in main. So the enitre code would be:

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <cstdio>
using namespace std;

ifstream file("maze.txt");
vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); // Imports characters from file
vector<char> path; // Declares path as the vector storing the characters from the file
int x = 18; // Declaring x as 18 so I can use it with recursion below
char entrance = vec.at(16); // 'S', the entrance to the maze
char firstsquare = vec.at(17); // For the first walkable square next to the entrance
vector<char> visited; // Squares that we've walked over already

void printtoscreen();

int main()
{
if (file) {
path.push_back(entrance); // Store 'S', the entrance character, into vector 'path'
path.push_back(firstsquare); // Store the character of the square to the right of the entrance
// into vector 'path'.
while (isalpha(vec.at(x)))
{
path.push_back(vec.at(x));
x++;
}
}
printtoscreen();
}

void printtoscreen()
{
cout << "Path is: "; // Printing to screen the first part of our statement

// This loop to print to the screen all the contents of the vector 'path'.
for(vector<char>::const_iterator i = path.begin(); i != path.end(); ++i) //
{
std::cout << *i << ' ';
}

cout << endl;
cin.get(); // Keeps the black box that pops up, open, so we can see results.
return;
}


Related Topics



Leave a reply



Submit