Hi,
I just recently started learning C++ and I bought a book called:
Beginning C++ Game Programming
By Michael Dawson
I've been learning quite a bit, but mostly on my dad's mac. Now at my mother's I don't have a mac with i-code (not sure how the ide is called), so I downloaded the BloodShed Dev-C++ IDE which came on the cd with the book. I seemed to like it, I found it a bit worse, but managable, and I came to a problem. I did a helloWorld to test it out, so my code:
Code:
//HelloWorld
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, World" << endl;
}
What happened apparently is that the program did it, but it just closed right after.. So in the book they give a solution:
Code:
std::cout << "Press Enter to exit" << endl;
std::cin.ignore(std::cin.rdbuf()->in_avail()+1);
I put that in, and it worked fine. Then I went on to do the stuff where I left off in the book. So I copied up a menu (using switch) example they had in the book.
So here it is:
Code:
//Difficulty Menu
#include <iostream>
using namespace std;
int main()
{
cout << "Difficulty Levels\n\n";
cout << "1 - Easy\n";
cout << "2 - Normal\n";
cout << "3 - Hard\n";
int choice;
cout << "Choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "You picked Easy.\n";
break;
case 2:
cout << "You picked Normal.\n";
break;
case 3:
cout << "You picked Hard.\n";
break;
default:
cout << "You made an illegal choice.\n";
}
cout << "Press Enter to exit" << endl;
cin.ignore(cin.rdbuf()->in_avail()+1);
return 0;
}
As you can see at the end I used the enter to exit thing agian, but I realised when I ran the program, I pressed 2 to choose normal for example, and it just closed it (so I presumed since I'm pressing enter to confirm the difficulty, I thought perhaps I should try using some other key for the exit. Is that possible? And how would I do that? I'm not quite sure how the
Code:
cout << "Press Enter to exit" << endl;
cin.ignore(cin.rdbuf()->in_avail()+1);
works either. If someone could explain that, it would be awesome
Thanks,
-Vic