I wrote this little code after learning the first 2 lessons of the C++ series to test myself, but I need a little help understanding a few features of C++.
It runs just how I want it to, but I still have 3 questions.Code:#include <iostream> using namespace std; int main() { int answer; cout<<"What is 3 times 4?\n"; cin>> answer; cin.ignore(); if ( answer < 8 || answer > 16 ) { cout<<"You answered: " << answer << "\n That is way off, try again"; } else if ( answer >= 8 && answer <= 16 && answer != 12) { cout<< "You answered: " << answer << "\n That is close, try again."; } else if ( answer == 12 ) { cout<< "You answered: " << answer << "\n That is correct!"; } cin.get(); }
1. About all those curly braces, why do the If, else, and else if statement need to have the following output in curly braces? An in depth explanation of these curly braces would be highly appreciated.
2. What is up with the int main(); thing? From what I understand your stating that there is an integer variable named main(). So, what does this int main() thing really mean/what does it do?
3. the cin.ignore(); feature is also tough to follow. I know that it gets rid of the enter after the inputted numbers, but in what instances is this necessary? Anytime i have a value inputted, followed by a unwanted enter?
I know this is all lengthy and very wordy, but any help is again, highly appreciated.
Thanks for your time and help.