c++ primer plus(二)std

本文详细介绍了C++中标准输入输出流cin和cout的基本用法,包括如何使用cin读取不同类型的数据,如整数和字符串,以及cin读取字符串时的一些限制;同时,文章还解释了如何使用cout进行格式化输出,包括改变输出整数的进制格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   

1. using namespace std;

名字空间既是引用哪个名字空间的函数

 

2. 输入 cin 

cin得到int变量:

int nTemp;

cin>> nTemp;

 

cin获取字符串:

char name[20]

cin >> name;

cin 获取字符串有问题:

a. 只能得到一个单词,若输入lei fist则 得到lei,

b. 会跳过空格。若输人"   ddd",则得到"ddd".  

c.  应该使用cin.getline:   cin.getline(name,20);

 

3. cout输出

a. 输出字符串

char flower[10] = "rose";

cout << flower<<"s arered\n";

flower是个指向一个字符的地址,cout会从该字符开始打印,直到遇到空字符为止。


b. 输出整数

通过dec hex oct改变输出整数的类型。

int chest = 42;

int waist = 0x42;

int inseam = 042; //8进制

int main()

{

    usingnamespacestd;

    intchest = 42;

    intwaist = 42;

    intinseam = 42;

 

    cout<< dec << "Monsieur cuts a striking figure!"<< endl;

    cout<< "chest = "<< chest <<" (decimal for 42)"<< endl;

    cout<< hex;        //manipulator for changing number base;

    cout<< "waist = "<< waist <<" (hexadecimal for 42)"<< endl;

    cout<< oct;        //manipulator for changing number base;

    cout<< "inseam = "<< inseam <<" (octal for 42)"<< endl;

    return0;

}

结果:

Monsieur cuts a striking figure!

chest = 42 (decimal for 42)

waist = 2a (hexadecimal for 42)

inseam = 52 (octal for 42)

 

c.  这三个换行是一样的:

cout <<  endl;

cout<<  '\n'

cout << "\n"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值