首页 > 其他分享 >字符数组和字符串的输入:cin,,getchar,cin.get,cin.geiline

字符数组和字符串的输入:cin,,getchar,cin.get,cin.geiline

时间:2023-09-26 13:11:07浏览次数:29  
标签:cout get int geiline cin 标识符 输入

 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     //cin.get输入字符
 6     //  //char c;
 7     /*while ((c = cin.get()) != EOF)
 8     {
 9         cout << c;
10     }*/
11     /*  while (cin.get(c))
12       {
13           cout << c;
14       }*/
15       //while (c = getchar())
16       //{
17       //    cout << c;
18       //}
19 
20     //输入字符串
21     //char a[10] = "Computer";
22     //a[9] = 10;
23     //cout << a << endl
24     //    << a[4] << endl;
25 
26 
27     //char str[10];
28     //cout << "enter a sentence: " << endl;
29     //while (cin >> str)//不断的重新给数组赋值
30     //{
31     //    cout << str << endl;//输出的是数组中被赋值的所有值,空格和回车均代表重新赋值,同时手动输入\0会被读取成字符而不是结束标志
32     //    cout << "str0 :" << str[0] << endl;
33 
34     //}
35     // 
36     //cin.get函数输入字符串,遇到终止字符,指针不移动
37     //cin.get(str, 10, 'o');//设置o为指定终止符(如果不设定则默认换行是指定终止符),输入o代表提前终止,即在str[9]的赋值之前终止,如果没有指定终止符则会把数组所有元素复制完后自动终止。空格和回车均会读取
38     //cout << str << endl;
39     //
40     //cin.getline函数输入字符串,会再在输入缓冲中保存换行符,而cin.get()会(当然,cin也会)。就是说,如果输入一行字符串,如果用cin.getline(),那么当内容输入到变量中后,输入缓冲中也不会有回车符,不会影响下一个输入函数的读取;
41     /*char a[10][10];
42     int n = 0;
43     cin >> n;
44     for (int i = 0; i < n; i++)
45     {
46         cin.get(a[i], 10);
47     }
48     for (int i = 0; i < n; i++)
49     {
50         cout << a[i] << endl;
51     }
52 */
53     char a[10][10];
54     int n = 0;
55     cin >> n;
56     cin.get();//56行 因为人们习惯性的换行换行作为终止标识符,cin>> n,后换行会把一个终止标识符读书输入缓冲区,而cin.getline会把这个终止标识符读进去(读一次后指针跳到终止标识符后),使得这个标识符占用了一个数组位,使得输入的数的数目比期望值少1
57     for (int i = 0; i < n; i++)
58     {
59         cin.getline(a[i], 10);//这里如果用cin.get,就会输入失败,因为cin会把指针停止在停止标识符哪里,则读后面的函数输入之前会反复读入停止标识符,则还没输入就停止了。参数10的意义是如果没遇到终止标识符就读完a[9】然后停止
60     }
61     for (int i = 0; i < n; i++)
62     {
63         //cout << "a[0] :" << a[0] << endl; 如果不要56行的cin.get();读掉终止标识符,a[0]的值就会是哪个终止标识符,为什么不用 cin.getline();因为cin.getline();一定要有参数,更麻烦
64         cout << a[i] << endl;
65     }
66 
67    
68 
69     return 0;
70 }

 

标签:cout,get,int,geiline,cin,标识符,输入
From: https://www.cnblogs.com/zaiyewujiang/p/17729857.html

相关文章

  • using wget utility to download files while keeping path structure
    Frommanwget:-x,--force-directories:[...]createahierarchyofdirectories,evenifonewouldnothavebeencreatedotherwise.E.g.wget-xhttp://fly.srk.fer.hr/robots.txtwillsavethedownloadedfiletofly.srk.fer.hr/robots.txt.  Togetthest......
  • [888] How to get the directory of the current Python file
    TogetthedirectoryofthecurrentPythonfile,youcanusetheos.pathmoduleincombinationwiththe__file__attribute.Here'showyoucandoit:importos#GetthedirectoryofthecurrentPythonfilecurrent_directory=os.path.dirname(os.pat......
  • urllib_ajax的get请求豆瓣电影第一页
    #get请求#获取豆瓣电影的第一页的数据并且保存起来importurllib.requesturl='https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&start=0&limit=20'headers={'User-Agent':'Mozilla/5.0(WindowsNT......
  • python.exe get-pip.py安装失败
    装pip要用get-pip.py来安装,但安装时还要下载whl文件,如果系统中没有设置国内镜像源,从国外下,会经常失败。原因:大部分失败都是因为网络问题才失败的。解决方法:使用国内源,在C:\Users\你的用户名\pip文件夹下,把以下内容保存到pip.ini里。[global]index-url=http://mir......
  • Teacher Forcing
    Teacherforcing是一种序列任务中的技术。它最初提出的动机是使得RNN的训练得以并行,加快模型训练。简单来说Teacherforcing就是将数据集中的标签作为模型输入。首先,以RNN为例,介绍Teacherforcing的技术细节。下图为RNN展开的计算图。如图所示,Teacherforcing在训练时和测试......
  • pyqt5-QTreeWidgetItem
    QTreeWidgetItem树节点项。QTreeWidgetItem(strings:Iterable[str],type:int=QTreeWidgetItem.Type)创建节点时,必须是Iterable[str],表示一行中各列的文本 1、子节点child(self,index:int)->QTreeWidgetItem获取某节点的某子节点childCount(self)->int获......
  • pyqt5-QTreeWidget
    QTreeWidget树组件。1、顶级项addTopLevelItem(self,item:QTreeWidgetItem)末尾添加单个顶级项addTopLevelItems(self,items:Iterable[QTreeWidgetItem])末尾批量添加顶级项insertTopLevelItem(self,index:int,item:QTreeWidgetItem)指定索引插入单个顶级项......
  • Docker - ERROR: failed to solve: golang:latest: error getting credentials - err:
    Dockerfile:FROMgolang:latestWORKDIR/appADD..RUNgoenv-wGOPROXY=https://goproxy.io,directRUNgogetRUNgobuild-oapp.CMD["/app/app"] zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$dockerbuild-ttest:v1.[+]Building1.3s(3/3)FINI......
  • git SSL certificate problem unable to get local issuer certificate
    gitSSLcertificateproblemunabletogetlocalissuercertificate这个问题是由于没有配置信任的服务器HTTPS验证。默认,cURL被设为不信任任何CAs,就是说,它不信任任何服务器验证。只需要执行下面命令就可以解决:gitconfig--globalhttp.sslVerifyfalse......
  • getattr()函数、setattr()函数
    getattr(object,name[,default])getattr(object,name[,default])getattr简介参数object–对象。name–字符串,对象属性。default–默认返回值,如果不提供该参数,在没有对应属性时,将触发AttributeError。返回值返回对象属性值。 例如classPerson(NamedTu......