query on Console windows closing

Hi,
I read the sticky, on console windows closing.

so i did..
#include <limits>
.
.
.
.
.
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
  }


but the exe file continue to close after execution without any "Press enter to conitnue.."
It closed immediately... =(
I typed this:


1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <limits>

int main()
{
  std::cout << "Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

  return 0;
}


And it worked fine :?
hihi,
let me explain my situation..

i coded it in VS2010.
i compiled it, and i copy the .exe file
i execute the exe file, it closes straight.
its the same as you simply press F5 in VS2010...
I am using Code::Blocks, I build and run, success. Did you try what I typed? I am not 100% what your problem is. You may have to reinstall your IDE, but that is becoming an all too common solution from me. You can run other programs fine though right? This one is the only program not working?
the codes u gif is working..
the codes u give doesnt work on my program...
=~~(


#include <iostream>
#include <limits>
int main ()
{

	int totalminutes;	//User input of minutes.
	int hours;			//Using totalminutes/60.
	float minutes;		//Using (totalminutes%60) * 60.
	int hoursmoney;		//Using hours * $5.
	float minutesmoney;	//Using minutes * (5/60).
	float earnings;		//Using hoursmoney + minutesmoney.


	//Starting program, asking for user input. Storing to totalminutes
	std::cout << "OT Calculator. Version 0.01" << std::endl;
	std::cout << "Accuracy of calculation is ESTIMATED." << std::endl;
	std::cout << "Please double check with your own calculator." << std::endl;
	std::cout << std::endl;
	std::cout << "Please enter your total minutes. " << std::endl;
	std::cin >> totalminutes;

	//Calculating totalminutes into hours and minutes. Storing to variables.
	hours = totalminutes/60;
	minutes = totalminutes%60;

	//Calculating hoursmoney, minutesmoney and earnings. Storing to variables.
	hoursmoney = hours * 5;
	minutesmoney = minutes * 0.08333;
	earnings = hoursmoney + minutesmoney;

	//Showing user information gathered.
	std::cout << std::endl;
	std::cout << "You have entered " << totalminutes << " minutes." << std::endl;
	std::cout << "Based on the calculation of $5 an hour, " << std::endl;
	std::cout << hours << " hours = $" << hoursmoney << std::endl;
	std::cout << minutes << " minutes = $" << minutesmoney << std::endl;
	std::cout << std::endl;
	std::cout << "For " << totalminutes << " minutes, you earned $" << earnings << std::endl;
	std::cout << std::endl;
	std::cout << std::endl;
	std::cout << std::endl;

	//Exiting program.
	std::cout << "Press ENTER to continue...";
	std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

	return 0;

}
That is odd. I don't really know enough about the way that code words (the exiting section) so hopefully Duoas or someone can explain better.

As for a temporary solution:

1
2
3
4
5
6
7
8
9
    //Previous Code

    //Exiting program.
    std::cout << "Press ENTER to continue...";
    std::cin.get();
    std::cin.get();

    return 0;
}
=~~~~~(
help help the sky is falling
I still remember the old song "Raindrops keep falling on my head...." that has been covered by some many bands and singers :)
closed account (D80DSL3A)
The following works on my system to keep the console window open. Hope it works on yours too.
These are the closing lines in main()
1
2
3
4
5
6
	std::cout << "Hit ENTER to exit";// Oxymoron?
	std::cin.ignore();
	std::cin.get();
	
	return 0;
}// end of main() 
Last edited on
hihi..

whats the difference between
	std::cin.ignore();
	std::cin.get();


and

    std::cin.get();
    std::cin.get();


both are giving me the same desired results..
so whats the difference in the ignore and the double cin.get
First, the problem was this line: std::cin >> totalminutes;

This leaves at extra \n in the buffer, so when you do:
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

It will read until it sees the '\n', then finish. Put a std::cin.ignore() after your input to fix the problem.

Second, ignore() simply ignores the character, whereas .get() will return the character (so you can use it).
omg..
why did the system ownself put a \n into the buffer?
No, the '\n' in a enter. When you type "12" and hit Enter, the buffer will contain: "12\n"
it means that as long as i ask user for input, lets say cin>>name
user enter : james butler
so the buffer will contact james butler\n .........???
closed account (Lv0f92yv)
Yes. The newline character ("\n") represents the 'enter' key. You don't see it, but it is there.

If your curious, you can do: cout << "\nhello\nworld"; and see the line separations.

When you input something to standard input via the keyboard, the enter (return key) is read in as a '\n' and stored in the buffer (usually). There are times when the \n is left in the stream, and not input into the variable, where cin.ignore() comes in handy - this takes one character off of the stream (in this case, the one '\n' character).
Topic archived. No new replies allowed.