a Quick Question about cin.ignore()

This is a discussion on a Quick Question about cin.ignore() within the C++ Programming forums, part of the General Programming Boards category; Code: char this = 0; cin>> this; cin.ignore ( this, '\n' ); 1. cin.ignore(this,'\n') checks "this" to find first '

  1. #1
    Flo
    Flo is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    15

    Question a Quick Question about cin.ignore()

    Code:
    char this = 0;
    cin>> this;
    cin.ignore ( this, '\n' );
    1. cin.ignore(this,'\n') checks "this" to find first '\0'(null character) and ignore everyting
    after that, in adition ignores '\n'(new line) after it ignored the first part.
    2. cin.ignore(this,'\n') checks "this" to find last null character etc.as above.
    or
    3. cin.ignore(this,'\n') checks "this" to find how much can it hold and ignore everyting after
    that size, in adition ignores '\n'(new line) after it ignored the first part.

    the reason for asking such thing is that id like to replace every space with a null character
    so that i could use a single input(from user) to input for more then one cin>>
    (lets say i type "bla1 bla2"(bla1(space)bla2) so that bla2 could be auto inserted in 2nd
    cin>>) but still cin.ignore(this,'\n') to ignore everyting after "this" when il use it.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Since the first argument to ignore is the number of characters to ignore, and this == 0, cin.ignore(this,'\n') will do nothing.

    You explanation of what you want to do is not really clear, eg, what do you want to "cin" to?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Flo
    Flo is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for ( ; ; )
        {
            char inputforcase = 0;
            cin>> inputforcase;
            cin.ignore(inputforcase, '\n');
    
            switch ( inputforcase )
            {
                case '1' :
                {
                    cout<<"bla \n";
                    cin.get();
                }
                    break;
            }
        }
    }
    then why first code acts diferit then this code below if u input 111
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        for ( ; ; )
        {
            char inputforcase = 0;
            cin>> inputforcase;
            cin.ignore(0, '\n');
    
            switch ( inputforcase )
            {
                case '1' :
                {
                    cout<<"bla \n";
                    cin.get();
                }
                    break;
            }
        }
    }

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Flo View Post
    char inputforcase = 0;
    cin>> inputforcase;
    cin.ignore(inputforcase, '\n');
    If you entered "111" with that loop, the first '1' goes into "inputforcase". The ASCII integer value for the character '1' is 49:

    ASCII Table / Extended ASCII Codes

    So, inputforcase == 49. Now, cin.ignore(inputforcase,'\n') will follow this rule:

    The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first.
    Meaning it will ignore the next 49 characters unless one of them is a newline, in which case it will stop after the newline.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Flo
    Flo is offline
    Registered User
    Join Date
    Jun 2010
    Posts
    15
    now that i can understand with ease, ty ty

    /edit
    and nvm about the other thing that i was thinking ..
    Last edited by Flo; 06-29-2010 at 10:08 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

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