Manipulator that skips until end-of-line : cin « Development « C++ Tutorial

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Development » cin 
5.1.16.Manipulator that skips until end-of-line
/* The following code example is taken from the book
 * "The C++ Standard Library - A Tutorial and Reference"
 * by Nicolai M. Josuttis, Addison-Wesley, 1999
 *
 * (C) Copyright Nicolai M. Josuttis 1999.
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
 */
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>

#include <limits>

template <class charT, class traits>
inline
std::basic_istream<charT,traits>&
ignoreLine (std::basic_istream<charT,traits>& strm)
{
    // skip until end-of-line
    strm.ignore(std::numeric_limits<int>::max(),strm.widen('\n'));

    // return stream for concatenation
    return strm;
}


int main()
{
    int i;
    std::cout << "read int and ignore rest of the line" << std::endl;
    std::cin >> i;

    // ignore the rest of the line
    std::cin >> ignoreLine;

    std::cout << "int: " << i << std::endl;

    std::cout << "read int and ignore two lines" << std::endl;
    std::cin >> i;

    // ignore two lines
    std::cin >> ignoreLine >> ignoreLine;

    std::cout << "int: " << i << std::endl;
}
read int and ignore rest of the line
123 qwe
int: 123
read int and ignore two lines
123wer
asdf
int: 123
5.1.cin
5.1.1.Read int value from keyboard
5.1.2.cin Handles Different Data Types
5.1.3.Use get() to read a string that contains spaces
5.1.4.Use the extraction operator >> with cin.get( ) to process an entire string.
5.1.5.cin with strings (cin.getline)
5.1.6.cin and atoi, atof functions
5.1.7.Using peek() and putback()
5.1.8.Concatenate put()
5.1.9.Testing error states.
5.1.10.Unformatted I/O using cin.read, cin.gcount and cout.write
5.1.11.Contrasting input of a string via cin and cin.get
5.1.12.Demonstrating member function width
5.1.13.Read char array from keyboard, get its length and concatenate two strings
5.1.14.Read a character from keyboard
5.1.15.Copy all standard input to standard output
5.1.16.Manipulator that skips until end-of-line
5.1.17.avoids buffer overflow with cin.width
5.1.18.Extends std::streambuf to create data buffer
5.1.19.Using member functions get, put and eof.
5.1.20.Contrasting input of a string with cin and cin.get.
5.1.21.Unformatted I/O with read, gcount and write.
5.1.22.Character input with member function getline.
5.1.23.Is it a bad input
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.