Please help with basic C++ syntax and the cin.ignore() feature

This is a discussion on Please help with basic C++ syntax and the cin.ignore() feature within the C++ Programming forums, part of the General Programming Boards category; I wrote this little code after learning the first 2 lessons of the C++ series to test myself, but I ...

It runs just how I want it to, but I still have 3 questions.
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.

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.
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.

For what you're doing, you don't need the extra braces but many people still like to have them. If each section of the if/else-if/else contained more than one statement that needed to be executed then you definitely would need the braces but since all the ones in your code sample consist of single-line statements the braces are not technically required. It does make things easier if in the future you wanted to add more code to each section... you wouldn't need to also add the braces since they'd already be there. They certainly don't hurt anything by being present.

Originally Posted by Allthingslive

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?

main is a function. The starting/entry-point for C/C++ programs is always called main (sorta). The main function returns an integer variable which is what the int part is for.

Originally Posted by Allthingslive

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?

In your post, the last line is a cin.get(); statement. If the ignore was not present in this instance, the get would grab the trailing newline character left in the input stream (from where you entered the "answer") and the program would then exit immediately without stopping for additional user input. This is clearly not the intent of the program. The get is meant to force the program to pause at that point so the user can press enter at his/her leisure at which point the program will end. The ignore eats up that newline character from the input stream so the ending get call can do its job of pausing the program. This ensures the get call is a blocking read (i.e. it waits for input like it should).

Originally Posted by Allthingslive

I know this is all lengthy and very wordy, but any help is again, highly appreciated.
Thanks for your time and help.

"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens

Thanks, but one more thing.

Your response was awesome, but just one thing...
Is there a command I can use that will make the program wait for the enter at the end without using cin.get(); and cin.ignore(); ?
would system("pause"); do the trick?