C++ cin的用法,看这一篇就够了

1 C++输入输出综述

C++ IO首先建立在为Unix环境开发的原始库函数上;ANSI C正式承认这个库时,将其称为标准输入/输出包; IO相关类定义在头文件iostream和fstream,这些类不是正式语言定义的组成部分,cin,istream都不是关键字。

1.1 流和缓冲

(1)流简介:

  • C++程序将输入和输出看作字符流;对于输入来说,程序从输入流中抽取字符,对于输出来说,程序向输出流中插入字符;
  • 输入流可以来自键盘、存储设备或者其他程序;输出流可以输出至显示器、打印机、存储设备或者其他程序。
  • 流是程序与流源或流目的之间的中介,这样C++就可以对来源不同的字符做相同处理。

(2)管理输入:

  • 两个阶段:将流与程序绑定在一起,将流与源绑定在一起

(3)管理输出:

  • 两个阶段:将流与目的绑定在一起,将流与程序绑定在一起

(4)缓冲区简介

缓冲区就是一块存储空间,它是为了匹配程序处理速度和外设处理速度;比如程序一次处理1byte,但是磁盘一次读取512bytes;又或者程序一次处理1byte,可以1byte地从磁盘读取,但是由于硬件读取一次数据复杂且操作慢,因此使用缓冲区可以加快程序处理速度。
flushing the buffer:刷新缓冲区就是清空缓冲区地内容以备下次使用。

1.2 输入输出中比较重要的类

  • streambuf:提供缓冲区,有成员方法 填满缓冲区、获取缓冲区内容、刷新缓冲区、管理缓冲区
  • ios_base:表示流的一般属性 比如文件是否打开、是二进制流还是文本流等等
  • ios:基于ios_base,并且它包含了一个指针成员指向一个streambuf对象
  • ostream:继承自ios类并提供了输出方法
  • istream:继承自ios类并提供了输入方法
  • iostream:继承自ostream类和istream类

1.3 C++11 I/O新特性

  • ostream.h转换为ostream,将ostream类放置到std命名空间中
  • I/O类被重写,开发了I/O类模板包括basic_istream>和basic_ostream>。实现了char,wchar_t具体化;istream和ostream是char的具体化,cout输出字符流,wistream和wstream是wchar_t的具体化,wcout用于输出宽字符流。
  • ios基类中的一些独立与类型的信息被移动到ios_base类中,比如格式化常量ios::fixed变为ios_base::fixed,还新增了一些常量

1.4 包含iostream头文件时会自动创建八个流对象(4个用于窄字符流,4个用于宽字符流)

  • cin对象:对应标准输入流,默认情况下这个流与标准输入设备匹配(键盘);wcin对象用于wchar_t类型;
  • cout对象:对应标准输出流,默认情况下这个流与标准输出设备匹配(显示器),借助streambuf管理流;wcout对象用于wchar_t类型;
  • cerr对象:对应于标准错误流(可以用于显示错误信息),默认情况下这个流与标准输出设备匹配(显示器),这个流是不缓冲的;wcerr对象用于wchar_t类型;不受重定向的影响,即使重定向了输入输出流,错误信息还是打印到显示器上
  • clog对象:对应于标准错误流,默认情况下这个流与标准输出设备匹配(显示器),这个流是缓冲的;wclog对象用于wchar_t类型。不受重定向的影响,即使重定向了输入输出流,错误信息还是打印到显示器上

1.4 重定向

修改标准输入和标准输出关联的工具。

  • 输出到文件,而不是显示器
  • 允许用文件替换键盘输入
  • Windows允许通过键盘模拟文件尾:Ctrl+Z

2 cin的使用

C++在输入时将数据看作字符流。istream类提供了一个可以将istream对象(如cin)转换为bool值的函数,当cin出现在需要bool类型的地方(如在while循环测试条件中),该转换函数将被调用;如果读取成功,转换为true,如果读取失败,转换为false。

2.1 cin自动类型转换

istream类,定在文件为iostream头文件,为以下数据类型重载了>>抽取操作符,所重载的函数称为格式化输入函数。 signed char &、unsigned char &、char &、short &、unsigned short &、int &、unsigned int &、long &、unsigned long &、long long & (C++11)、unsigned long long & (C++11)、float &、double &、long double & 成员函数原型为:

  1. istream & operator>>(type &);//type为以上数据类型
  2. //这些函数被称为格式化输入函数,函数可以将输入的字符串转换为type格式

2.2 cin与指针

istream类,定在文件为iostream头文件,为以下指针类型重载了>>抽取操作符, signed char 、char 、unsigned char * 成员函数原型为:

  1. istream & operator>>(type &);//type为以上数据类型
  2. //函数使得cin的字符流存储到指针指向的存储地址。

举例:

  1. cout << "cin与指针******************************************************************************" << endl;
  2. char ceshi[20];
  3. cout << "请输入姓名:" << endl;
  4. cin >> ceshi;
  5. cout << "您的姓名是:" << ceshi << endl;

运行结果:

  1. cin与指针******************************************************************************
  2. 请输入姓名:
  3. Jasmine
  4. 您的姓名是:Jasmine

2.3 cin如何检查输入

cout略过空白符(空白、新行、tabs)直到它遇到非空白符。

对于一个变量,cin>>读取的是从非空白字符开始,到与目标类型不匹配的第一个字符之间的全部内容;不匹配的内容将被保存在输入缓冲流中,等待下一次输入。

2.3.1 非数字终止循环

当然这个(cin检查输入)也还有用武之地,可以被放在循环条件中来终止循环。(需要注意的是,循环终止后不再允许用户输入,即使调用cin>>也没用;如需要用户输入,则需要设置标识符为有效才可)如下例子:

举例:

  1. // File name: cinfish.cpp
  2. // Last modified Date: 2021年11月21日11点28分
  3. // Last Version: V1.0
  4. // Descriptions: 非数字输入终止循环
  5. // cinfish.cpp -- non-numeric input terminates loop
  6. #include <iostream>
  7. const int Max = 5;
  8. int main()
  9. {
  10. using namespace std;
  11. // get data
  12. double fish[Max];
  13. char x;
  14. cout << "Please enter the weights of your fish.\n";
  15. cout << "You may enter up to " << Max
  16. << " fish <q to terminate>.\n";
  17. cout << "fish #1: ";
  18. int i = 0;
  19. while (i < Max && cin >> fish[i]) {//在这里,如果输入为字符,那么第二个条件会为0,将不会再执行循环体的内容
  20. if (++i < Max)
  21. cout << "fish #" << i + 1 << ": ";
  22. }
  23. // calculate average
  24. double total = 0.0;
  25. for (int j = 0; j < i; j++)
  26. total += fish[j];
  27. // report results
  28. if (i == 0)
  29. cout << "No fish\n";
  30. else
  31. cout << total / i << " = average weight of "
  32. << i << " fish\n";
  33. cout << "Done.\n";
  34. //如果再需要输入的话
  35. cin.clear(); // reset input 如果没有这一句的话,后面就直接运行完成,不会请求用户输入
  36. //这一句是吃掉结束循环的那个字符
  37. cin.get();
  38. //这一句是吃掉那个结束字符后面的回车
  39. cin.get();
  40. cout << "请输入一个测试字符:";
  41. x = cin.get();
  42. cout << x<<endl;
  43. return 0;
  44. }

运行结果:

  1. Please enter the weights of your fish.
  2. You may enter up to 5 fish <q to terminate>.
  3. fish #1: 2.9
  4. fish #2: a
  5. 2.9 = average weight of 1 fish
  6. Done.
  7. 请输入一个测试字符:r
  8. r
  9. D:\Prj\C++\C++_Learning\cinfish_cin_Input_inspection\Debug\cinfish_cin_Input_inspection.exe (进程 14760)已退出,代码为 0
  10. 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
  11. 按任意键关闭此窗口. . .

2.3.2 用户输入检查

如果用户输入与变量类型不匹配,就提示用户输入类型匹配的数据并处理掉那些不匹配的数据。

代码:

  1. // File name: user_Input_inspection
  2. // Last modified Date: 2021年11月21日11点33分
  3. // Last Version: V1.0
  4. // Descriptions: 用户输入检查
  5. // cingolf.cpp -- non-numeric input skipped
  6. #include <iostream>
  7. const int Max = 5;
  8. int main()
  9. {
  10. using namespace std;
  11. // get data
  12. int golf[Max];
  13. cout << "Please enter your golf scores.\n";
  14. cout << "You must enter " << Max << " rounds.\n";
  15. int i;
  16. for (i = 0; i < Max; i++)
  17. {
  18. cout << "round #" << i + 1 << ": ";
  19. //**************************用户输入检查的核心部分开始**************************//
  20. while (!(cin >> golf[i])) {//如果输入类型不匹配,则执行循环体
  21. cin.clear(); // reset input设置标志位为有效
  22. while (cin.get() != '\n') //删除没有用的输入
  23. continue; // get rid of bad input
  24. cout << "Please enter a number: ";
  25. }
  26. //**************************用户输入检查的核心部分结束**************************//
  27. }
  28. // calculate average
  29. double total = 0.0;
  30. for (i = 0; i < Max; i++)
  31. total += golf[i];
  32. // report results
  33. cout << total / Max << " = average score "
  34. << Max << " rounds\n";
  35. return 0;
  36. }

运行结果:

  1. Please enter your golf scores.
  2. You must enter 5 rounds.
  3. round #1: 5
  4. round #2: 6
  5. round #3: 7
  6. round #4: 88
  7. round #5: a
  8. Please enter a number: 19
  9. 25 = average score 5 rounds
  10. D:\Prj\C++\C++_Learning\user_Input_inspection\Debug\user_Input_inspection.exe (进程 6568)已退出,代码为 0
  11. 要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
  12. 按任意键关闭此窗口. . .

2.4 流状态

流状态被定义为iostate,是由三个ios_base组成:eofbit, badbit, 和failbit,三者都为一位可以设置位1(设置)或0(清除)。当三者都为0时说明一切顺利。

成员 描述
eofbit 当读取文件到达文件尾时置为1
failbit 当输入流中的数据类型和目的数据类型不一致时置为1;I/O错误(读取一个不允许访问的只读磁盘)时置为1
badbit 在一些无法诊断的失败破坏流时置为1
goodbit 另一种说明0的方法
good() 如果流可以使用,则返回true,反之返回false
eof() 如果eofbit被设置,则返回true,反之返回false
bad() 如果badbit被设置,则返回true,反之返回false
fail() 如果failbit或badbit被设置,返回true,反之返回false
rdstate() 返回流状态
exceptions() 返回一个位掩码,指出哪些标记导致异常被引发
exceptions(iostate ex) 设置哪些状态将导致clear()引发异常
clear(iostate s) 将流状态设置为s,s的默认值为0;如果rdstate() & exceptions()) != 0,将抛出basic_ios::failure异常;将会设置s,但是其他的会被置0
setstate (iostate s) 调用clear(rdstate() | s)。这将设置与s中设置的位对应的流的状态位,其他流状态保持不变。

2.4.1 设置状态

setstate()的主要目的是提供一种修改状态的途径。例如,如果num是一个int,则下面的调用将可能导致operator>>(int &)使用setstate()设置failbit或eofbit。

  1. clear();//清除所有的位
  2. clear(eofbit);//清楚指定的位
  3. setstate(eofbit);//设置指定的位
  4. //等等

2.4.2 I/O和异常

exceptions()返回一个位字段,它包含3位,分别对应于eofbit, failbit, and badbit。

修改流状态后,clear()方法将当前的流状态与exceptions()返回的值进行比较;如果两者的某一对应位都被设置,则clear()将会引发ios_base::failure异常。

举例:

举例:

  1. cout << "流状态********************************************************************************" << endl;
  2. cout << "异常:" << endl;
  3. // have failbit cause an exception to be thrown
  4. cin.exceptions(ios_base::failbit);
  5. cout << "Enter numbers: ";
  6. int sum = 0;
  7. int input;
  8. try {
  9. while (cin >> input)
  10. {
  11. sum += input;
  12. }
  13. }
  14. catch (ios_base::failure& bf)
  15. {
  16. cout << bf.what() << endl;
  17. cout << "O! the horror!\n";
  18. }
  19. cout << "Last value entered = " << input << endl;
  20. cout << "Sum = " << sum << endl;

运行结果:

  1. 流状态********************************************************************************
  2. 异常:*****************************************
  3. Enter numbers: 99 88 q
  4. ios_base::failbit set: iostream stream error
  5. O! the horror!
  6. Last value entered = 88
  7. Sum = 187

2.4.3 解除输入挂起

上述例子中引发了异常,那么在执行上述代码之后,后面的cin被挂起。

根本原因在于,在设置流状态之后有个很重要的结果:流被关闭了,错误的输入被存储在输入缓冲区,直到所有的位被cleared之后才能重新打开流。

因此,在引发类型不匹配异常后需要以下两步:

  1. 1.重置流状态
  2. 2.清空输入缓冲区

举例:接着2.4.2的例子

  1. cout << "解除输入挂起:***********************************" << endl;
  2. //cin >> sum;//将失败因为输入被挂起了
  3. //执行下述代码后,就可以重新输入
  4. if (cin.fail() && !cin.eof()) //排除掉是读到end-of-file的可能
  5. {
  6. cout << "输入类型不匹配问题,正在重置cin~" << endl;
  7. cin.clear(); // reset stream state
  8. while (!isspace(cin.get())) //or while (cin.get() != '\n')
  9. continue; //删除输入流中滞留的数据
  10. cout << "重置cin完成~" << endl;
  11. }
  12. else // else bail out
  13. {
  14. cout << "I cannot go on!\n";
  15. exit(1);
  16. }
  17. cout << "Now enter a new number: ";
  18. cin >> input; // will work now
  19. cout << "您输入的数据为:" << input << endl;

运行结果:

  1. 解除输入挂起:***********************************
  2. 输入类型不匹配问题,正在重置cin~
  3. 重置cin完成~
  4. Now enter a new number: 99
  5. 您输入的数据为:99

2.4.4 文件尾EOF

文件尾 EOF 如果输入来自于文件:使用文件尾(EOF)判断文件是否读完。 当检测到EOF之后:cin将eofbit和failbit都设置为1;同时,将设置cin对象中的一个指示EOF条件的标记,设置这个标记后,cin将不读取输入,直到cin.clear()。 EOF被定义为值为-1,因此不能将EOF赋给char类型,因为char类型没有符号,需要使用int类型接收EOF。

举例:Windows允许通过键盘模拟文件尾:Ctrl+Z

  1. cout << "文件尾 EOF**************************************" << endl;
  2. cout << "请输入字符串,EOF结尾~:" ;
  3. int count = 0;
  4. int chx;//不能使用char接受,因为EOF被设置为-1
  5. while ((chx = cin.get()) != EOF) // test for end-of-file
  6. {
  7. cout.put(char(chx));
  8. ++count;
  9. if (count == 5)
  10. break;
  11. }
  12. cout << endl << count << " characters read\n";
  13. cin.clear();
  14. while (cin.get() != '\n');

运行结果:

  1. 文件尾 EOF**************************************
  2. 请输入字符串,EOF结尾~:
  3. klaisjjsdj^Z
  4. klai
  5. 5 characters read

2.5 面向字符的输入

2.5.1 cin.get(char &)

  • 读取一个字符并存储到实参中。
  • 是一个非格式化函数,不会越过空白、新行、tab字符输入。
  • 返回值为cin,因此可以级联输入。
  • 注意事项:istream类提供了一个可以将istream对象(如cin)转换为bool值的函数,当cin出现在需要bool类型的地方(如在while循环测试条件中),该转换函数将被调用;如果读取成功,转换为true,如果读取失败,转换为false。

原型:

  1. basic_istream<charT,traits>& get(char &);

举例:

  1. cout << "cin.get(char)*****************************************************************************" << endl;
  2. int ct = 0;
  3. char ch;
  4. cout << "请输入内容:" << endl;
  5. while (cin.get() != '\n');
  6. cin.get(ch);
  7. while (ch != '\n')
  8. {
  9. cout << ch;
  10. ct++;
  11. cin.get(ch);
  12. }
  13. cout << "您输入了"<<ct <<"字母"<< endl;

运行结果:

  1. cin.get(char)*****************************************************************************
  2. 请输入内容:
  3. liaiajj ssdsmaka
  4. liaiajj ssdsmaka
  5. 您输入了16字母

2.5.2 cin.get(void)

本函数主要是为了兼容C语言,通过包含iostream(而不是stdio.h),并用cin.get()替换所有的getchar(),用cin.put(ch)替换所有的putchar(ch),以此来将C程序转换为C++程序。

函数原型:

  1. int get(void);

cin.get(void)特点

  • 读取一个字符并返回
  • 是一个非格式化函数,不会越过空白、新行、tab字符输入
  • 返回值为int,因此不支持级联输入
  • 注意事项:当输入失败时返回EOF(EOF可能不是正整数,因此如果要检查EOF不能使用char接收返回值。)

举例:

  1. cout << "cin.get()********************************************************************************" << endl;
  2. cout << "吃掉回车符cin.get()**********************************" << endl;
  3. char test1[30];
  4. cout << "输入字符串1:";
  5. cin >> test1;
  6. while (cin.get() != '\n');//只有加上了这句,ch才可以正常读取,否则ch会读取为输入test1后的回车符。
  7. cout << "输入单个字符:***************************************"<<endl;
  8. ch = cin.get();
  9. while (cin.get() != '\n');
  10. cout << "您输入的字符为:";
  11. cout.put(ch);
  12. cout << endl;

运行结果:

  1. cin.get()********************************************************************************
  2. 吃掉回车符cin.get()**********************************
  3. 输入字符串1kia
  4. 输入单个字符:***************************************
  5. a
  6. 您输入的字符为:a

2.5.3 cin.get(ch)与cin.get()

属性 cin.get(ch) ch = cin.get()
传递输入字符的方式 赋给参数ch ch = cin.get()
用于字符输入时函数的返回值 istream对象(执行bool转换为true) int类型的字符编码
到达EOF时函数的返回值 istream对象(执行bool转换为false) EOF

2.6 面向行的输入

2.6.1 getline() 、get()与ignore()

getline() 、get()函数原型:

  1. istream & get(char *, int, char);//VS2022说没有与参数列表匹配的重载函数
  2. istream & get(char *, int);
  3. istream & getline(char *, int, char);//VS2020说没有与参数列表匹配的重载函数
  4. istream & getline(char *, int);

说明:第一个参数指定读取数据的存储位置首地址;第二个参数等于大于最大可读取字符总数+1(留一个位置给’\0’);第三个参数是一个分隔符,只有两个参数的函数的分隔符为换行符。

注意:getline() 与get()最大的不同在于getline() 会从输入流中抽取并丢弃分隔符(换行符),但是get()会将分隔符留在输入流中但作为下一个读取的开始。

ignore()函数原型:

  1. istream & ignore(int = 1, int = EOF);

说明:第一个参数指定最大读取字符数量,第二个参数指定间隔符;该函数至多丢弃最大字符数量的字符,遇到间隔符则终止丢弃。

举例:

  1. cout << "面向行的输入*****************************************************************************" << endl;
  2. cout << "getline() 、get()与ignore()*********************" << endl;
  3. /*5 cin面向行的输入*/
  4. /*方法1 cin.getline()*/
  5. cout << "cin.getline()*****************************************" << endl;
  6. const int ArSize = 20;
  7. char name[ArSize];
  8. char dessert[ArSize];
  9. char name1[ArSize];
  10. char dessert2[ArSize];
  11. cout << "getline() test" << "\n";
  12. cout << "Enter your name:";
  13. cin.getline(name, ArSize);//cin.getline()会主动吃掉字符串输入最后的回车符,因此不需加上get()吃掉回车符
  14. cout << "Enter your favorite dessert:";
  15. cin.getline(dessert, ArSize);
  16. cout << "I have some delicious " << dessert;
  17. cout << " for you, " << name << ".\n" << endl;
  18. /*方法2 cin.get()*/
  19. cout << "cin.get()*********************************************" << endl;
  20. cout << "get() test" << "\n";
  21. cout << "Enter your name:";
  22. cin.get(name, ArSize).get();//cin.get()不会主动吃掉字符串输入最后的回车符,因此需要加上get()吃掉回车符
  23. cout << "Enter your favorite dessert:";
  24. cin.get(dessert, ArSize).get();
  25. cout << "I have some delicious " << dessert;
  26. cout << " for you, " << name << ".\n" << endl;
  27. /*ignore()*/
  28. cout << "ignore()**********************************" << endl;
  29. cout << "请输入内容(字符串+空格+字符串):";
  30. cin.get(ch);
  31. cout << "输入字符为:" << ch << endl;
  32. cout << "丢弃字符前,下一个字符为:" << char(cin.peek()) << endl;//peek()为查看下一个字符但是不抽取,依然留在输入流中
  33. if (ch != ' ')
  34. cin.ignore(9, ' '); // discard rest of line
  35. cout << "丢弃字符后,下一个字符为:" << char(cin.peek()) << endl;
  36. while (cin.get() != '\n');

运行结果:

  1. 面向行的输入*****************************************************************************
  2. getline() get()与ignore()*********************
  3. cin.getline()*****************************************
  4. getline() test
  5. Enter your name:lili
  6. Enter your favorite dessert:cakes
  7. I have some delicious cakes for you, lili.
  8. cin.get()*********************************************
  9. get() test
  10. Enter your name:lalla
  11. Enter your favorite dessert:lla
  12. I have some delicious lla for you, lalla.
  13. ignore()**********************************
  14. 请输入内容(字符串+空格+字符串):liaia llaajs
  15. 输入字符为:l
  16. 丢弃字符前,下一个字符为:i
  17. 丢弃字符后,下一个字符为:l

2.6.2 意外字符串输入

方法 行为
getline(char *,int) 如果没有读取任何字符(换行符视为读取了一个字符),则设置failbit
如果读取了最大数目的字符,且行中还有其他字符(非换行符),则设置failbit
get(char *,int) 如果没有读取字符,则设置failbit

get()首先查看字符数量,其次查看EOF,最后查看下一个字符是否为换行符

2.7 其他istream方法

2.7.1 read()

原型:

  1. istream & read(char *,int);//第一个参数指定存储位置,第二个参数指定读取多少数量的字符

功能:读取指定数量的字符并将其存储到指定的位置,不添加’\0’。

应用场景:不应用于键盘输入,而是应用于ostream write()函数的文件输入和输出。

注意事项:返回值为istream,允许级联使用。

2.7.2 peek()

原型:

  1. char peek();

作用:返回输入流的下一个输入字符,但是不抽取,peek()完之后该字符还在输入流中且位置不变。

2.7.3 gcount()

原型:

  1. int gcount();

作用:计算上次非格式化抽取方法抽取的字符数量并返回。

注意事项:gcount()比strlen()要快哟。

2.7.4 putback()

原型:

  1. istream &, putback(char &);

作用:向输入流中插入一个字符,这个被插入的字符将会成为下一个输入的第一个字符。

注意事项:gcount()比strlen()要快哟。

2.7.5 举例

  1. cout << "其他istream方法************************************************************************************" << endl;
  2. char name2[SLEN];
  3. char title[SLEN];
  4. cout << "Enter your name: ";
  5. cin.get(name2, SLEN);
  6. count = cin.gcount();//使用gcount()
  7. cout << "刚刚读取了:" << count << "字符" << endl;
  8. if (cin.peek() != '\n')
  9. cout << "Sorry, we only have enough room for "<< name2 << endl;
  10. eatline();//吃掉这行中剩下的字符
  11. cout << "Dear " << name2 << ", enter your title: ";
  12. cin.get(title, SLEN);
  13. ch = '#';
  14. cin.putback(ch);
  15. cin.get(ch);
  16. cout << ch << " is next input character.\n";
  17. eatline();
  18. cout << " Name: " << name2 << "\nTitle: " << title << endl;

运行结果:

  1. 其他istream方法************************************************************************************
  2. Enter your name: laa
  3. 刚刚读取了:3字符
  4. Dear laa, enter your title: saa
  5. # is next input character.
  6. Name: laa
  7. Title: saa

2.8 面向string类的输入

为什么不沿用iostream库的getline() and cout方法呢?原因是string类是后来提出的,iostream库中没有处理string类的机制。

  1. cout << "面向string类的输入*****************************************************************************" << endl;
  2. cout << "请输入字符串:" << endl;
  3. getline(cin, str1);//getline()是string类的一个友元函数
  4. cout << "str1 = " << str1 << endl<<endl;//cout是string类的一个友元函数

运行结果:

  1. 面向string类的输入*****************************************************************************
  2. 请输入字符串:
  3. iiaa
  4. str1 = iiaa

3 完整测试代码

  1. /*
  2. Project name : _6cin_usage
  3. Last modified Date: 2022年4月26日17点39分
  4. Last Version: V1.0
  5. Descriptions: cin的常用用法
  6. */
  7. #include <iostream>
  8. #include<string>
  9. const int SLEN = 10;
  10. inline void eatline() { while (std::cin.get() != '\n') continue; }
  11. const int Max = 5;
  12. int main()
  13. {
  14. using namespace std;//将命名空间std的所有名字都引用了
  15. cout << "cin与指针******************************************************************************" << endl;
  16. char ceshi[20];
  17. cout << "请输入姓名:" ;
  18. cin >> ceshi;
  19. cout << "您的姓名是:" << ceshi << endl;
  20. cout << "cin如何检查输入************************************************************************" << endl;
  21. int elevation;
  22. char getduo;
  23. cout << "请输入整数:" ;
  24. cin >> elevation;
  25. cout << "您输入了:" << elevation << endl;
  26. cout << "请输入单个字符:"<<endl;
  27. cin >> getduo;
  28. cout << "您输入的单个字符为:" << getduo<<endl;
  29. cout << "流状态********************************************************************************" << endl;
  30. cout << "异常:*****************************************" << endl;
  31. // have failbit cause an exception to be thrown
  32. cin.exceptions(ios_base::failbit);
  33. cout << "Enter numbers: ";
  34. int sum = 0;
  35. int input;
  36. try {
  37. while (cin >> input)
  38. {
  39. sum += input;
  40. }
  41. }
  42. catch (ios_base::failure& bf)
  43. {
  44. cout << bf.what() << endl;
  45. cout << "O! the horror!\n";
  46. }
  47. cout << "Last value entered = " << input << endl;
  48. cout << "Sum = " << sum << endl;
  49. cout << "解除输入挂起:***********************************" << endl;
  50. //cin >> sum;//将失败因为输入被挂起了
  51. //执行下述代码后,就可以重新输入
  52. if (cin.fail() && !cin.eof()) //排除掉是读到end-of-file的可能
  53. {
  54. cout << "输入类型不匹配问题,正在重置cin~" << endl;
  55. cin.clear(); // reset stream state
  56. while (!isspace(cin.get())) //or while (cin.get() != '\n')
  57. continue; //删除输入流中滞留的数据
  58. cout << "重置cin完成~" << endl;
  59. }
  60. else // else bail out
  61. {
  62. cout << "I cannot go on!\n";
  63. exit(1);
  64. }
  65. cout << "Now enter a new number: ";
  66. cin >> input; // will work now
  67. cout << "您输入的数据为:" << input << endl;
  68. cout << "文件尾 EOF**************************************" << endl;
  69. cout << "请输入字符串,EOF结尾~:" ;
  70. int count = 0;
  71. int chx;//不能使用char接受,因为EOF被设置为-1
  72. while ((chx = cin.get()) != EOF) // test for end-of-file
  73. {
  74. cout.put(char(chx));
  75. ++count;
  76. if (count == 5)
  77. break;
  78. }
  79. cout << endl << count << " characters read\n";
  80. cin.clear();
  81. while (cin.get() != '\n');
  82. char ch;
  83. cout << "cin.get(char)*****************************************************************************" << endl;
  84. int ct = 0;
  85. cout << "请输入内容:";
  86. cin.get(ch);
  87. while (ch != '\n')
  88. {
  89. cout << ch;
  90. ct++;
  91. cin.get(ch);
  92. }
  93. cout <<endl<< "您输入了"<<ct <<"字母"<< endl;
  94. cout << "cin.get()********************************************************************************" << endl;
  95. cout << "吃掉回车符cin.get()**********************************" << endl;
  96. char test1[30];
  97. cout << "输入字符串1:";
  98. cin >> test1;
  99. while (cin.get() != '\n');//只有加上了这句,ch才可以正常读取,否则ch会读取为输入test1后的回车符。
  100. cout << "输入单个字符:***************************************"<<endl;
  101. ch = cin.get();
  102. while (cin.get() != '\n');
  103. cout << "您输入的字符为:";
  104. cout.put(ch);
  105. cout << endl;
  106. cout << "面向行的输入*****************************************************************************" << endl;
  107. cout << "getline() 、get()与ignore()*********************" << endl;
  108. /*5 cin面向行的输入*/
  109. /*方法1 cin.getline()*/
  110. cout << "cin.getline()*****************************************" << endl;
  111. const int ArSize = 20;
  112. char name[ArSize];
  113. char dessert[ArSize];
  114. char name1[ArSize];
  115. char dessert2[ArSize];
  116. cout << "getline() test" << "\n";
  117. cout << "Enter your name:";
  118. cin.getline(name, ArSize);//cin.getline()会主动吃掉字符串输入最后的回车符,因此不需加上get()吃掉回车符
  119. cout << "Enter your favorite dessert:";
  120. cin.getline(dessert, ArSize);
  121. cout << "I have some delicious " << dessert;
  122. cout << " for you, " << name << ".\n" << endl;
  123. /*方法2 cin.get()*/
  124. cout << "cin.get()*********************************************" << endl;
  125. cout << "get() test" << "\n";
  126. cout << "Enter your name:";
  127. cin.get(name, ArSize).get();//cin.get()不会主动吃掉字符串输入最后的回车符,因此需要加上get()吃掉回车符
  128. cout << "Enter your favorite dessert:";
  129. cin.get(dessert, ArSize).get();
  130. cout << "I have some delicious " << dessert;
  131. cout << " for you, " << name << ".\n" << endl;
  132. /*ignore()*/
  133. cout << "ignore()**********************************" << endl;
  134. cout << "请输入内容(字符串+空格+字符串):";
  135. cin.get(ch);
  136. cout << "输入字符为:" << ch << endl;
  137. cout << "丢弃字符前,下一个字符为:" << char(cin.peek()) << endl;
  138. if (ch != ' ')
  139. cin.ignore(9, ' '); // discard rest of line
  140. cout << "丢弃字符后,下一个字符为:" << char(cin.peek()) << endl;
  141. while (cin.get() != '\n');
  142. cout << "其他istream方法************************************************************************************" << endl;
  143. char name2[SLEN];
  144. char title[SLEN];
  145. cout << "Enter your name: ";
  146. cin.get(name2, SLEN);
  147. count = cin.gcount();//使用gcount()
  148. cout << "刚刚读取了:" << count << "字符" << endl;
  149. if (cin.peek() != '\n')
  150. cout << "Sorry, we only have enough room for "<< name2 << endl;
  151. eatline();//吃掉这行中剩下的字符
  152. cout << "Dear " << name2 << ", enter your title: ";
  153. cin.get(title, SLEN);
  154. ch = '#';
  155. cin.putback(ch);
  156. cin.get(ch);
  157. cout << ch << " is next input character.\n";
  158. eatline();
  159. cout << " Name: " << name2 << "\nTitle: " << title << endl;
  160. cout << "面向string类的输入*****************************************************************************" << endl;
  161. string str1;
  162. cout << "请输入字符串:" << endl;
  163. getline(cin, str1);//getline()是string类的一个友元函数
  164. cout << "str1 = " << str1 << endl << endl;//cout是string类的一个友元函数
  165. return 0;
  166. }