Like Tree1Likes
  • 1 Post By whiteflags

Questions regarding Variable types and "cin.ignore"

This is a discussion on Questions regarding Variable types and "cin.ignore" within the C++ Programming forums, part of the General Programming Boards category; I'm rather confused at what exactly the difference is between the different variable types, mainly int and char. At first ...

  1. #1
    Registered User Luminory's Avatar
    Join Date
    Jun 2012
    Location
    t(^-^t )
    Posts
    2

    Questions regarding Variable types and "cin.ignore"

    I'm rather confused at what exactly the difference is between
    the different variable types, mainly int and char.

    At first I trying changing the code that was given in the tutorial involving, and it worked. So instead of numbers I thought, how about letters?

    I assume Char (meaning character) is used for letters.. yet when I changed it up it just didnt work.

    So then I went back to numbers and tried to make it so you could only type in numbers, not letters. The tutorial used cin.ignore(); for the program to ignore the enter key being a response to the question "Please enter a number".

    Basicly what I want to know is how did it know cin.ignore means the "enter" key and how can I change it to add multiple other characters to the "ignore list"?

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int omgnumbers;
        cout<<"Please Enter a number: ";
        cin>> omgnumbers;
        cin.ignore();
        cout<<"You Entered:"<< omgnumbers;
        cin.get();
    
    
    }
    Sorry if that was a bit confusing, im not the best at explaining things.
    oh and I cant wait to meet you all! This is my first post here.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    I assume Char (meaning character) is used for letters.. yet when I changed it up it just didnt work
    One letter, not many. The string data type, found in <string>, is more helpful for many characters.

    Basicly what I want to know is how did it know cin.ignore means the "enter" key and how can I change it to add multiple other characters to the "ignore list"?
    cin.ignore() does not mean "ignore the enter key" but it does mean "ignore 1 key". You can find that out by looking at a reference page. But just to help you out, the way to ignore more keys is to tell it how many to ignore. You can even tell the ignore function where to stop ignoring by additionally passing in a character.

    Code:
    // cin.ignore() is helpful for code like this.
    
    #include <iostream>
    #include <limits>
    using namespace std;
    int main()
    {
       double omgnumbers;
    
       cout << "enter anything: ";
       cin >> omgnumbers;
       while (!cin.good())
       {
          cin.clear();
          cin.ignore(numeric_limits<streamsize>::max(), '\n');
          cout << "enter anything (choose a number to stop): ";
          cin >> omgnumbers;
       }
       cout << "you entered " << omgnumbers << "\n";
       cin.ignore(); // ignore the '\n' at the end
       cout << "press enter to exit.\n";
       cin.get();
       
       return 0;
    }
    Try playing with this as well.

    Off-topic but that... isn't Sailor Venus... but it is my best guess.
    Last edited by whiteflags; 06-24-2012 at 10:23 PM.
    Luminory likes this.

  3. #3
    Registered User Luminory's Avatar
    Join Date
    Jun 2012
    Location
    t(^-^t )
    Posts
    2
    Thanks, you were a big help! I have a slightly better understanding of how to correctly use variable types and cin.ignore.

    I played around with the code a little but I could only understand so much, alot of what you put in I haven't learned yet (Ex.cin.clear, #include limits, while, cin.good etc.)

    Also what does Double do? I dont recall that being in the tutorial when he listed 3 of the variable types (He listed Int,Char, and Float).

    And again, thanks for your help, every little bit counts I know it must be rather aggravating at times to answer a beginners question when you already know all of this.

    Offtopic: If your talking about my avatar, thats roll from megaman
    Name:  megaman__roll.png
Views: 237
Size:  48.2 KB

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Input streams, such as cin, can go into an error state where no further input will be processed. This might happen in the code provided by whiteflags if non-numeric input was entered at the prompt. Since omgnumbers is of type double - which incidentally refers to double precision floating-point values - then entering something like "foobar" at the prompt would cause trouble where a program might be expecting something more along the lines of "123.456" as an example.

    The while loop is designed to repeatedly test the state of the cin stream thus ensuring that only valid double values can be entered. The cin.good call returns whether or not the stream is in a good state. If the stream is in a good state, then a valid double value must have been entered and we can skip the rest of the loop body. However, if the stream is in a bad state then we need to deal with the consequences of this. If the stream is in a bad state then the user tried to enter something non-numeric at the prompt and the bad input must be dealt with.

    The first thing the loop body does is clear the bad/error state with a call to cin.clear. Without this, most operations using the stream will not function properly or at all. The next step the loop body does is to get rid of all the bad data currently in the stream by calling cin.ignore. If this was not done, then the bad data that caused the problem in the first place would remain in the input buffer and the subsequent attempt at extracting another double value from the stream would simply cause the cin stream to go into a bad state all over again and we'd wind up in a never ending loop. The remaining lines of the loop body re-prompt the user for data and attempt to extract data.

    There are other ways of writing that loop, one might be to test the extraction operation itself to see if it succeeded or not. This would eliminate a couple lines of code:
    Code:
    cout << "enter anything: ";
    while ( !(cin >> omgnumbers) )
    {
       cin.clear();
       cin.ignore(numeric_limits<streamsize>::max(), '\n');
       cout << "enter anything (choose a number to stop): ";
    }
    The basics still remain however... clear the error state followed by removing the bad data from the stream so we can re-prompt and attempt to process some good data.
    "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

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    I played around with the code a little but I could only understand so much, alot of what you put in I haven't learned yet (Ex.cin.clear, #include limits, while, cin.good etc.)
    The post above me does a really good job explaining how it works. I just wanted to add what numeric_limits is: basically a big class found in <limits> that will tell you the min() and max() of any type in the < angle brackets > to the right. It has more functions than that, but I only used it to get a big number and make sure that number was the right type. Streamsize is what type of number cin.ignore() expects, so I just used that. However you want to pick a number for it, even if you just type one, it should be fine.

    Offtopic: If your talking about my avatar, thats roll from megaman
    Shoot, I didn't get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to ignore "Enter" key
    By juice in forum C Programming
    Replies: 9
    Last Post: 12-02-2011, 11:02 AM
  2. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  3. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07:25 AM
  4. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 06:59 AM
  5. Replies: 7
    Last Post: 09-05-2002, 08:01 AM

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