zoukankan      html  css  js  c++  java
  • 关于C++中的cin用法

    输入原理:

    程序的输入都建有一个缓冲区,即输入缓冲区。一次输入过程是这样的,当一次键盘输入结束时会将输入的数据存入输入缓冲区,而cin函数直接从输入缓冲区中取数据。正因为cin函数是直接从缓冲区取数据的,所以有时候当缓冲区中有残留数据时,cin函数会直接取得这些残留数据而不会请求键盘输入

    #1:

     #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     char str[8];
     6     cin.getline(str, 5);
     7     cout << str << endl;
     8     cin.getline(str, 5);
     9     cout << str << endl;    
    10     return 0;  
    11 }

    测试:

    12345678 (回车)

    输出结果:

    1234

    &

    测试:

    1(空格)2345678 (回车)

    输出结果:

    1(空格)23

    &

    测试:

    12 (回车)

    (输出)

    12

    (第二次输入)

    34(回车)

    (输出)

    34

    【分析】

    No1:之所以第一次输入完后直接程序就结束了,而不是进行第二次输入,是因为第一次多输入的数据还残留在缓存区中,第二次输入就直接从缓存区中提取而不会请求键盘输入.

    No2:cin.getline他的结束符是回车当你输入的字符当中有空格也会当做是你输入的字符缓存在内存当中。

    No3:只有当你输入的字符小于或等于你定义的char类型数组的时候才会让你第二次输入,如果大于则属于第一种情况

    以下探讨几种常见的输入方式:

    . cin>>

    该操作符是根据后面变量的类型读取数据。

    输入结束条件 :遇到Enter、Space、Tab键。

    对结束符的处理 :丢弃缓冲区中使得输入结束的结束符(Enter、Space、Tab)

    #2

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5    char str1[10], str2[10];
     6    cin>>str1;
     7    cin>>str2;
     8    cout<<str1<<endl;
     9    cout<<str2<<endl;
    10    return 0;
    11 }

    测试:

    abcd efgh

    输出:

    abcd

    efgh

    【分析】第一次读取字符串时遇到空格则停止了,将abcd读入str1,并舍弃了空格,将后面的字符串给了第二个字符串。这证明了cin读入数据遇到空格结束;并且丢弃空格符;缓冲区有残留数据,读入操作直接从缓冲区中取数据。

    二.cin.get(数组名,长度,结束符)

    其中结束符为可选参数,读入的字符个数最多为(长度-1)个,结束符规定结束字符串读取的字符,默认为ENTER

    若要读取字符,直接cin.get(char ch)或ch=cin.get()即可

    读取字符的情况:

    输入结束条件:Enter键

    对结束符处理:不丢弃缓冲区中的Enter

    cin.get() 与 cin.get(char ch)用于读取字符,他们的使用是相似的,

    即:ch=cin.get() 与 cin.get(ch)是等价的。

    #3

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5   char c1, c2;
     6    cin.get(c1);
     7    cin.get(c2);
     8     cout<<c1<<" "<<c2<<endl; // 打印两个字符
     9     cout<<(int)c1<<" "<<(int)c2<<endl; // 打印这两个字符的ASCII值
    10     return 0;
    11 }

    测试一输入:

    a[Enter]

    输出:

    a

    97 10

    【分析】会发现只执行了一次从键盘输入,显然第一个字符变量取的’a', 第二个变量取的是Enter(ASCII值为10),这是因为该函数不丢弃上次输入结束时的Enter字符,所以第一次输入结束时缓冲区中残留的是上次输入结束时的Enter字符!

    测试二输入:

    a b[Enter]

    输出:

    a

    97 32

    【分析】显然第一个字符变量取的’a', 第二个变量取的是Space(ASCII值为32)。原因同上,没有丢弃Space字符。

    读取字符串的情况:

    输入结束条件:默认Enter键(因此可接受空格,Tab键),可在第三个参数上自定义结束符

    对结束符处理:丢弃缓冲区中的Enter

    #4

     1 #include <iostream>
     2 using namespace std;
     3 int main ()
     4 {
     5   char ch, a[20];
     6   cin.get(a, 5 , 'd');
     7   cin>>ch;
     8   cout<<a<<endl;
     9   cout<<(int)ch<<endl;
    10   return 0;
    11 }

    测试一输入:

    12345[Enter]

    输出:

    1234

    53

    【分析】第一次输入超长,字符串按长度取了"1234",而’5′仍残留在缓冲区中,所以第二次输入字符没有从键盘读入,而是直接取了’5′,所以打印的ASCII值是53(’5′的ASCII值)。

    测试二输入:

    12d45[Enter]

    输出:

    12

    d

    【分析】第二次输出为d,说明自定义结束符时不丢弃缓冲区中的结束符

    三.cin.getline()

    cin.getline(数组名,长度,结束符) 大体与 cin.get(数组名,长度,结束符)类似。

    区别在于:

    cin.get()当输入的字符串超长时,不会引起cin函数的错误,后面的cin操作会继续执行,只是直接从缓冲区中取数据。但是cin.getline()当输入超长时,会引起cin函数的错误,后面的cin操作将不再执行。

    #5

     1 #include <iostream>
     2 using namespace std;
     3 int main ()
     4 {
     5   char ch, a[20];
     6   cin.getline(a, 5);
     7   cin>>ch;
     8   cout<<a<<endl;
     9   cout<<(int)ch<<endl;
    10   return 0;
    11 }

    测试输入:

    12345[Enter]

    输出:

    1234

    -52

    【分析】与cin.get()的例子比较会发现,这里的ch并没有读取缓冲区中的5,而是返回了-52,这里其实cin>>ch语句没有执行,是因为cin出错了!cin的错误处理下次介绍。

     

    检查输入

    cin会检查输入格式,输入与预期格式不符时,会返回false.
     
    1 cout << "Enter numbers: ";  
    2 int sum = 0;  
    3 int input;  
    4 while (cin >> input)  
    5     sum += input;  
    6 cout << "Last value entered = " << input << endl;  
    7 cout << "Sum = " << sum << endl;  

     

    上面的检查可以放入try、catch语句中。

    1 try{  
    2     while (cin >> input)  
    3         sum += input;  
    4 }catch(ios_base::failure &f){  
    5     cout<<f.what()<<endl;  
    6 }  

    字符串输入:getline()、get()、ignore()

    getline()读取整行,读取指定数目的字符或遇到指定字符(默认换行符)时停止读取。
    get()与getline()相同,接受参数也相同,均在读取到指定数目字符或换行符时停止读取。但get()将换行符停在输入流中,即下一个函数读到的首个字符将是换行符。
    ignore()接受的参数也同getline(),忽略掉指定数目的字符或到换行符。
     
     1 char input[Limit];  
     2 cout << "Enter a string for getline() processing:
    ";  
     3 cin.getline(input, Limit, '#');  
     4 cout << "Here is your input:
    ";  
     5 cout << input << "
    Done with phase 1
    ";  
     6 char ch;  
     7 cin.get(ch);  
     8 cout << "The next input character is " << ch << endl;  
     9 if (ch != '
    ')  
    10     cin.ignore(Limit, '
    ');    // 忽略接此一行余下内容  
    11 cout << "Enter a string for get() processing:
    ";  
    12 cin.get(input, Limit, '#');  
    13 cout << "Here is your input:
    ";  
    14 cout << input << "
    Done with phase 2
    ";  
    15 cin.get(ch);  
    16 cout << "The next input character is " << ch << endl;  
     
         
     
    例子中使用getline(),接下来的get()读到字符为3,忽略掉#;而get()之后,#在流中,所以读到字符为#。

    read()

    类似write()方法,读取指定数组字符。
    1 char score[20];  
    2 cin.read(score,20);

    putback()

    putback()将一个字符插入到输入字符中,被插入的字符是下一条语句读到的第一个字符。
     
     1 char ch;  
     2    while(cin.get(ch))          // terminates on EOF  
     3    {  
     4        if (ch != '#')  
     5            cout << ch;  
     6        else  
     7        {  
     8            cin.putback(ch);    // reinsert character  
     9            break;  
    10        }  
    11    }  
    12 cin.get(ch);  
    13 cout << endl << ch << " is next input character.
    ";  

    peek()

    返回输入流中的下一个字符,但只查看不抽取。
    1 char input[100];  
    2 char ch;  
    3 int i=0;  
    4 while((ch=cin.peek())!='.'&&ch!='
    ')  
    5     cin.get(input[i++]);  
    6 input[i]='';  

     程序遇到句号或换行符循环停止。句点或换行符仍停留在输入流中。

    可见,使用peek的效果相当于先用get()读取一个字符,再用putback()将字符放入输入流中。
     
    1. char ch;  
    2.    while(cin.get(ch))          // terminates on EOF  
    3.    {  
    4.        if (ch != '#')  
    5.            cout << ch;  
    6.        else  
    7.        {  
    8.            cin.putback(ch);    // reinsert character  
    9.            break;  
    10.        }  
    11.    }  
    12. cin.get(ch);  
    13. cout << endl << ch << " is next input character. ";  
  • 相关阅读:
    CD4051
    sbit和sfr的定义
    EEPROM与FLASH的区别
    九LWIP学习笔记之最后的战役
    八LWIP学习笔记之用户编程接口(NETCONN)
    七LWIP学习笔记之传输控制协议(TCP)
    六LWIP学习笔记之用户数据报协议(UDP)
    java实现二叉查找树
    线程的锁对象
    MAP
  • 原文地址:https://www.cnblogs.com/RCJL/p/9075969.html
Copyright © 2011-2022 走看看