现在的位置: 首页 > 综合 > 正文

C++学习代码–cin.clear

2013年10月01日 ⁄ 综合 ⁄ 共 5607字 ⁄ 字号 评论关闭

示例1

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <cctype>

using namespace std;
using std::string;

void cinTest1() {
    int ival;
    // read cin and test only for EOF; loop is executed even if there are other IO failures
    while (cin >> ival, !cin.eof()) {
    //{
        cout << "now:" << cin.rdstate() << endl;
        cout << "data =>" << ival << endl;
        if (cin.bad())         // input stream is corrupted; bail out
            throw runtime_error("IO stream corrupted");
        if (cin.fail()) {                        // bad input
            cerr<< "bad data" << endl;        // warn the user
            //cin.clear(istream::failbit);         // reset the stream
            //cin.ignore();
            cin.clear();
            cout << "ok:" << cin.rdstate() << endl;
            continue;                            // get next input
        }
        // ok to process ival
    }
}

void cinTest2() {
    int ival;
    string line;
    string word;
    while (getline(cin, line)) {
       
        istringstream lstream(line);
        while (lstream >> word) {
            if (!isdigit(word[0])) {
                cerr<< "bad data: " << word << endl;
                continue;
            } else {
                istringstream wstream(word);
                wstream >> ival;
            }
        } // while small
    } // while large
}

int main() {
    cinTest2();
    return 0;
}

----------------------------------------------------------------------------------------------------

示例2

文件1:functions.cpp

#include <vector>
using std::vector;

void reset(const int *ip) {
    //*ip = 0; // changes the value of the object to which ip points
    ip = 0;
}

// 交換
void swap(int &v1, int &v2) {
    int tmp = 0;
    tmp = v1;
    v1 = v2;
    v2 = tmp;
}

// returns an iterator that refers to the first occurrence of value
// the reference parameter occurs contains a second return value
vector<int>::const_iterator find_val(
    vector<int>::const_iterator beg,             // first element
    vector<int>::const_iterator end,             // one past last element
    int value,                                    // the value we want
    vector<int>::size_type &occurs
    )              // number of times it occurs
{
    // res_iter will hold first occurrence, if any
    vector<int>::const_iterator res_iter = end;
    occurs = 0; // set occurrence count parameter
    for ( ; beg != end; ++beg)
        if (*beg == value) {
            // remember first occurrence of value
            if (res_iter == end)
               res_iter = beg;
            ++occurs; // increment occurrence count
        }
    return res_iter;  // count returned implicitly in occurs
}

文件2:Sales_item.h

#ifndef SALES_ITEM_H
#define SALES_ITEM_H

#include <string>

using namespace std;

class Sales_item {
    public:
        Sales_item(): units_sold(10), revenue(500.0) { }
        double avg_price() const;
        bool same_isbn(const Sales_item &rhs) const
            { return isbn == rhs.isbn; }
    private:
        string isbn;
        unsigned units_sold;
        double revenue;
};

#endif

 

文件3:Sales_item.cpp

#include "Sales_item.h"

double Sales_item::avg_price() const {
    if (units_sold) {
        return revenue/units_sold;
    } else {
        return 0;
    }
}

 

文件4:test.cpp

#include <iostream>
#include <cstdlib>
#include <vector>
#include "Sales_item.h"

using std::vector;
using namespace std;

extern void reset(const int *);
extern void swap(int &, int &);
extern vector<int>::const_iterator find_val(
    vector<int>::const_iterator,             // first element
    vector<int>::const_iterator,             // one past last element
    int,                                    // the value we want
    vector<int>::size_type &);

void showCinSts() {
    cout << cin.rdstate() << endl;
    if (cin.bad()) {
        cout << "bad" << endl;
    }
    if (cin.fail()) {
        cout << "fail" << endl;
    }
    if (cin.eof()) {
        cout << "eof" << endl;
    }
    if (cin.good()) {
        cout << "good" << endl;
    }
}

void test() {
    const int value = 1;
    const int *p = &value;
   
   
    cout << "値:" << *p << endl;
    reset(p);
    cout << "値:" << *p << endl;
    p = NULL;
   
    int i = 10;
    int j = 20;
    cout << "値i:" << i << '/t' << "値j:" << j << endl;
    swap(i, j);
    cout << "値i:" << i << '/t' << "値j:" << j << endl;
   
    vector<int> ivec;
    vector<int>::const_iterator it;
    vector<int>::size_type ctr = 0;
   
    int word = 0;
   
   
    cout << "Input the numbers for the vactor:" << endl;
    while (cin >> word) {
        cout << word << endl;
        ivec.push_back(word);     // append word to text
    }
   
    //cout << cin.eof() << endl;
    /*
    ivec.push_back(1);
    ivec.push_back(2);
    ivec.push_back(3);
    */
    istream::iostate old_state = cin.rdstate();
   
    showCinSts();
    cin.clear();
    //fflush(stdin);
    //cin.sync();
    //cin.setstate(istream::eofbit);
    //cin.clear(istream::failbit);
    //cin.clear(istream::eofbit);
   
    showCinSts();
   
    //cout << cin.eof() << endl;
   
    int find = 0;
    cout << "Input a number to search:" << endl;
    cin >> find;
   
    cin.setstate(old_state);
    cout << cin.rdstate() << endl;
   
    typedef vector<int>::const_iterator (*fndVal)(
    vector<int>::const_iterator,             // first element
    vector<int>::const_iterator,             // one past last element
    int,                                    // the value we want
    vector<int>::size_type &);
   
    fndVal pF = find_val;
   
    it = (*pF)(ivec.begin(), ivec.end(), find, ctr);
    pF = NULL;
   
    int output = *it;

    cout << "The occurrence of " << find << " is:" << ctr << endl;
    cout << "The first occurrence is " << output << endl;
   
    cin.clear();
    int ival = 0;
    /*
    // read cin and test only for EOF; loop is executed even if there are other IO failures
    while (cin >> ival, !cin.eof()) {
        if (cin.bad())         // input stream is corrupted; bail out
            throw runtime_error("IO stream corrupted");
        if (cin.fail()) {                        // bad input
            cerr << "bad data, try again" << endl;        // warn the user
            //cin.clear(istream::failbit);         // reset the stream
            cout << cin.rdstate() << endl;
            //cin.clear();
            //cout << cin.rdstate() << endl;
            // get next input
        }
       
        // ok to process ival
    }
    */

}

int main() {
    Sales_item item;
    cout << item.avg_price() << endl;
   
    test();
   
    return 0;
}

 

抱歉!评论已关闭.