zoukankan      html  css  js  c++  java
  • C++-标准输入输出

    1,cout

      1) 用来向标准输出打印。

      2) 如果参数是char*类型,则直接输出字符串。如果想要输出地址,则需要强制转换:

      <<static_cast<void*>(const_cast<char*>(pstr));

    2,cin

      1) 将标准输入读入变量,如果输入与变量声明类型不一致,则cin为false,可以用if判断,变量值不确定。

      double price; //输入asdf

      cout << price; //输出为0

      2) cin结束后在缓存中会遗留有' ',因此会影响后续的get/getline。未不影响后续使用可以调用cin.ignore()

    3, cout.get()和cout.getline()

      1) 都是用来从标准输入读入内容,可以控制读入,cin遇到回车符就会结束,并且用空格分隔变量。get和getline则不同,它们可以控制读入的长度和分隔符。

      2) 区别很小,都是用来读入流。同cin类似get不会从缓存中移除' ',getline则不同,可以清除。所以要么一直用cout/cin组合,要么一直用getline,getline,getline,不要用cin或者get后用get或getline。

    4, read()和write()也是cout的函数,和标准的cout/cin不同的是它们可以控制输出和输入字节数。

    5, 有个global函数getline挺好用的。

     1 #include <iostream>
     2 #include <string>
     3 #include <stdio.h>
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     //cout
     9     cout << "cout test started..." << endl;
    10     const char* str = "adfadf";
    11     cout << "str is: " << str << endl;
    12 
    13     //cin
    14     //read input seperated by space
    15     //if input is not the cin type, cin will be false
    16     cout << "cin test started..." << endl;
    17     string        sname;
    18     double        price;
    19     cout << "Please enter name and price: " << endl;
    20     cin >> sname;
    21     if(!cin)
    22         cout << "the name is incorrect. " << endl;
    23     cin >> price;
    24     if(!cin)
    25         cout << "the price is incorrect. " << endl;
    26     cout << "The name is: " << sname 
    27         << " and the price is: " << price << endl;
    28 
    29     //cout ignore one 
    
    30     cin.ignore();
    31 
    32     //get, address will fail to get because get left a 
     after first call
    33     // // cout << "Enter your name:";
    34     // char name[SIZE];
    35     // // cin.get(name,SIZE);
    36     // // cout << "name:" << name;
    37     // // cout.put(name[0]);
    38     // // cout << "
    Enter your address:";
    39     // char address[SIZE];
    40     // // cin.get(address,SIZE);
    41     // // cout << "address:" << address << endl;
    42 
    43     //getline, address will succeed to get
    44     const int SIZE = 15;
    45     cout << "Enter your name:";
    46     char name[SIZE];
    47     cin.getline(name,SIZE);
    48     cout << "name:" << name;
    49     cout << "
    Enter your address:";
    50     char address[SIZE];
    51     cin.getline(address,SIZE);
    52     cout << "address:" << address << endl;
    53 
    54     //global function getline
    55     cout << "global getline test start" << endl;
    56     string ss;
    57     getline(cin, ss);
    58     cout << "ss is: " << ss << endl;
    59 
    60     //read, write
    61     char inchar[10];
    62     cin.read(inchar, 3);
    63     cout.write(inchar, 10);
    64 
    65 }
  • 相关阅读:
    201521123028 《Java程序设计》第5周学习总结
    Markdown格式
    201521123028《Java程序设计》第4周学习总结
    201521123028 《Java程序设计》第3周学习总结
    Spring07 JDBC
    Spring06 Aop
    Mystring05 配置文件之间的关系
    Mybatais 13 二级缓存
    Mybatais 14 注释的配置
    Mybatais 12 一级缓存
  • 原文地址:https://www.cnblogs.com/dracohan/p/3803284.html
Copyright © 2011-2022 走看看