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

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

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

    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++.
    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();
    
    
    }
    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.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    Quote Originally Posted by Allthingslive View Post
    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.


    Quote Originally Posted by Allthingslive View Post
    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.


    Quote Originally Posted by Allthingslive View Post
    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).

    Quote 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.
    Hardly lengthy or wordy.
    Last edited by hk_mp5kpdw; 06-10-2010 at 06:57 PM.
    "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

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    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?

Popular pages Recent additions subscribe to a feed

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21