zoukankan      html  css  js  c++  java
  • 复合类型(一)

    // Test0921.cpp : 定义控制台应用程序的入口点。
    //
    #include <iostream>
    using namespace std;    
    void Fun1()
    {
        char str[6]={'h','e','l','l','o'};    
        //这一句编译的时候会报错“数组界限溢出”(因为数组没有空间存储\0):char str[5]="hello";
        //下面这一句不会报错,但是如果这样写会报错:char *str="hello";str[3]='m';
        str[3]='m';
        cout << "Hello world\n";
        cout <<str <<endl;
        //在C++中,任何两个由空白字符分隔的字符串常量都将会自动拼接成一个
        cout << "你好"  "中国"<<endl;
        //strlen返回字符串的长度(不包括\0)
        cout<< strlen(str)<<endl;
    }

    void Fun2()
    {
        //输入几个字符,空格,再输入几个字符,你会发现bug
        //输入几个字符,回车,再输入几个字符,正确运行
        char str1[10],str2[10];
        cout <<"input your name"<<endl;
        cin >>str1;
        cout <<endl<<"input your pwd"<<endl;
        cin >>str2;
        cout<<"your name:"<<str1<<",your pwd:"<<str2;
    }

    //使用cin.getline(),输入9个以内的字符,回车,再输入9个以内字符,程序运行正确发现并无Fun2中的bug,但是输入“123456798012345”或者超过9个字符又会有bug
    void Fun3()
    {
        char str1[10],str2[10];
        cout <<"input your name"<<endl;
        //getline读取整行,它使用通过回键输入的换行符确定输入结尾(也就是此方法会从输入流中读取字符直到回车换行或者指定的字符数)
        //getline有两个参数,第一个是用来存储输入字符的数组名称,第二个是要读取的字符数(如果这个参数是20,则函数最多只读取19个字符,然后再再添加一个\0)
        cin.getline(str1,10);
        cout <<endl<<"input your pwd"<<endl;
        cin.getline(str2,10);
        cout<<"your name:"<<str1<<",your pwd:"<<str2;
    }

    //混合输入字符串和数字
    void Fun4()
    {
        int age;
        char address[100];
        cout<<"please input your age:"<<endl;
        cin >>age;
        //如果没有下面这一句,会发现输入一个数字回车以后,还没输入地址,程序就结束了。原因分析如下:
        /*
        当cin读取年龄的时候,将回车键生成的换行符留在了输入队列(既输入流)中,后面的getline()看到换行符后,将认为    是一个空行,并将一个空字符串赋给name数组。解决方法是:在读取名字之前先读取并丢弃换行符。
        可以通过几种方法:
        单独使用没有参数的get()和使用接受一个char参数的get()
        cin>>age;
        cin.get();//or cin.get(ch)
        或者使用表达式cin>>age返回的cin对象将调用拼接起来:
        cin>>age.get()//or cin>>age.get(ch);
        */    
        cin.get();
        cout <<"please input your address:"<<endl;
        cin.getline(address,100);
        cout <<"your age is :"<<age<<";and your address is :"<<address<<endl;
    }


    //使用cin.get,输入9个以内的字符,回车,再输入9个以内字符,程序运行正确,但是输入“123456798012345”或者超过9个字符又会有bug
    void Fun5()
    {
        /*
        get()参数和getline()一样,但是读到行尾时不再读取并丢弃换行符,而是将其保留在输入队列中。假设连续两次调用get(),由于第一次调用后,换行符保留在输入队列,因此第二次调用时首先看到的字符就是这个换行符,这是get()认为到达了行为,没有发现任何读取内容,这样第二次调用get的输入不会显示的。但是如果在两次调用之间加上cin.get()就可读取下一个字符(即使是换行符),因此可用它来处理换行符。为读取下一行输入做好准备。
        */
        char str1[10],str2[10];
        cout <<"input your name"<<endl;
        cin.get(str1,10);
        cout <<endl<<"input your pwd"<<endl;    
        //因为上一次调用get并未丢弃换行,所以下面再次调用cin.get(str2,10)的时候看到的字符就是这个换行符,此时get认为到达了行尾,而没有发现任何可读取内容。这样一来get(数组名,数字)就不能跨过换行符
        //想要跨过换行符,可以使使用cin.get()这个重载函数,不带参数的cin.get()可以读取下一个字符(包括换行符)
        cin.get();
        cin.get(str2,10);
        cout<<"your name:"<<str1<<",your pwd:"<<str2;
    }

    int main()
    {
        //Fun1();
        //Fun2();    
        //Fun3();
        //Fun4();
        //Fun5();
        
        char str1[5],str2[5];
        cin.getline(str1,5);    
        //cin.clear();    
        cin.getline(str2,5);
        cout<<str1<<","<<str2<<endl;

    }
  • 相关阅读:
    lincode 题目记录5
    lintcode题目记录4
    lintcode 题目记录3
    lintcode 题目记录2
    剖析ASP.NET Core(Part 2)- AddMvc(译)
    剖析ASP.NET Core MVC(Part 1)- AddMvcCore(译)
    如何 RESTFul 你的服务(译)
    Windows + IIS 环境部署Asp.Net Core App
    Asp.Net Core 缓存的使用(译)
    [转载].NET Core 轻量级模板引擎 Mustachio
  • 原文地址:https://www.cnblogs.com/mxw09/p/1834839.html
Copyright © 2011-2022 走看看