Alright, so I've been reading this pdf/online book of learning C++, and I got
Dev-C++ as a compiler to run the "hello world" program. The console closed down instantly, so after some searching I found the top thread, and tried the first replies code:
1 2 3 4 5 6 7 8 9 10
#include <iostream> //includes I/O Library
usingnamespace std;
int main()
{
cout <<Hello World!!<< endl;
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
return 0;
}
That solved the problem but then I'm on the second example program and I have no idea where to put said code, or how to change it to work. The console closes down after the program runs no matter. I'm completely new and I've read through the first couple pages of the sticky and it doesn't make any sense to me. If someone could provide me with an explanation in ENGLISH, that would be most appreciated. This is the problematic noob program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Change in dimes and pennies
#include <iostream>
usingnamespace std;
int main ()
{
int price, change, dimes, pennies;
cout << "Enter price (0:100): ";
cin >> price;
change = 100 - price; // how much change
dimes = change / 10; // number of dimes
pennies = change % 10; // number of pennies
cout << "\n\nThe change is :"
<< dimes << " dimes ";
cout << pennies << " pennies." << endl;
cout <<"Press ENTER to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
return 0;
}
There is still input left in the cin buffer, so your cin.ignore line finds that, happily ignores it, and carries on. You need that buffer to be empty so that cin.ignore will wait for something to be put into it.
You can do it inelegantly by sticking another cin.ignore(numeric_limits<streamsize>::max(), '\n' ); in there.
Thank you for the speedy reply!
But like I said I have literally no base knowledge, I read you typing "cin buffer" and I'm like "what IS that". I copied the "fixing" code from the beginning without knowing what any of it's components actually do or mean. However from the link you posted: The first code when inputted std::cin.ignore(INT_MAX);
It works fine before or after
1 2 3
cout << pennies << " pennies." << endl;
cout <<"Press ENTER to continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n' );
but it no longer closes the program when I press enter. How can I get it to maintain that function but still "flush the cin buffer".
I remember when reading through that post someone posting a cool idea of defining a class to do this for you, by putting whatever command you use to keep the console open in the classes destructor, so if you just create an object of the class at the start of main you shouldn't have to worry about this. Your class could look something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Quit {
public:
~Quit()
{
std::cout << "Press ENTER to continue..." << std::flush;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
int main(int argc, char *argv[])
{
Quit quit;
// put your program here, now whenever main ends the destructor will be called
return EXIT_SUCCESS;
}