Thread: cin.ignore()

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    cin.ignore()

    Hey guys I was sitting on Efnet irc and this kid came in saying that he needed to use cin.ignore() to block input after the first letter on a string(for a school assignment). I was just wondering if this is possible(it intriges(sp) me) and if so how would you go about it.
    Woop?

  2. #2
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Hmm, seems like you would need to ignore it after its been typed..

    odd..

    It intrigues(sp) me also.
    What is C++?

  3. #3
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    http://cplusplus.com/ref/iostream/istream/ignore.html
    Code:
    // istream ignore
    #include <iostream>
    using namespace std;
    
    int main () {
      char first, last;
    
      cout << "Enter your first and last names: ";
    
      first=cin.get();
      cin.ignore(256,' ');
    
      last=cin.get();
    
      cout << "Your initials are " << first << last;
    
      return 0;
    }
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  4. #4
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    If I wanted to have the option to have 3 names, how would I make it skip the last name, if the person has only 2 names?

    I tried to make it like this, but it only works where there are 3 names.
    Code:
    int d = 0;
    char a, b, c;
    
    while (d == 0)
    {
    cout << "Inset your name ";
    
    a = cin.get();
    cin.ignore(30, ' ');
    b = cin.get();
    cin.ignore(30, ' ');
    c = cin.get();
    
    cout << endl << "Your intials are: " << a << "." << b << "."<< c << ".\n\n";
    Last edited by stillwell; 10-04-2004 at 02:13 PM.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    explicitly ask the user if they want to enter a first name/last name/other name, and if the answer is yes, then get their input, otherwise, move on. Alternatively, input the name as a single string and parse the string into the first name/middle name/last name/honorary name/etc" based on spaces between the "names".

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would actually keep your as is, but then check to see if c is a newline character. If c is a '\n' then they only have two names.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    By the way, are you certain that you need to make multiple calls to ignore() like that?

  8. #8
    Registered User stillwell's Avatar
    Join Date
    Aug 2004
    Posts
    80
    I'm pretty new to programming, so I have no idea if I need to do it like this.

    I have a new problem. Now it works with 2 names, but not 3

    Here is the code
    Code:
    main()
    {
    int d = 0;
    char a, b, c;
    
    while (d == 0)
    {
    cout << "Inset your name ";
    
    a = cin.get();
    cin.ignore(30, ' ');
    b = cin.get();
    if (!(c = '\n'))
    {
    cin.ignore(30, ' ');
    c = cin.get();
    }
    
    cout << endl << "Your intials are: " << a << "." << b;
    
    if  (!(c = '\n'))
    cout << "."<< c << ".\n\n";
    else
    cout << ".\n\n";
    }
            return 0;
    }
    And how do I clear the char variables, when it returns in the while loop?
    Last edited by stillwell; 10-04-2004 at 02:45 PM.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Kinda, but no. Plus you don't need the infinite loop. Additionally, I was playing with it, ignore() is the most retardedly useless function ever.

    Code:
    #include <iostream>
    
    int main(void) {
      bool done = false;
      char a, b, c;
    
      while(!done) {
        std::cout << "Inset your name ";
    
        a = cin.get();
        std::cin.ignore(30, ' ');
        b = std::cin.get();
        std::cin.ignore(30, ' ');
        c = cin.get();
    
        if(c == '\n')
          std::cout << std::endl << "Your intials are: " << a << "." << b << std::endl;
        else
          std::cout << std::endl << "Your intials are: " << a << "." << b << "."<< c << std::endl;
    
        std::cout << "continue?";
        done = (std::cin.get() == 'y')?false:true;
      }
    
      return 0;
    }

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ah hah! I've learned when something seems useless, you are using it wrong.

    http://www.augustcouncil.com/~tgibso...al/iotips.html

    For those interested how to use ignore().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console window waiting on cin.ignore() or cin.ignore(2)
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 04:17 PM
  2. Can't seem to come to conclusion : cin.ignore()
    By SkyHi in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2005, 08:57 PM
  3. Why do we use cin.ignore()
    By himanch in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 01:52 PM
  4. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM