C++自学日记day5——字符串型、布尔类型、数据的输入

字符串型

作用:用于表示一串字符
两种风格:
1、C风格字符串 char 变量名[]="字符串值”
2、C++风格字符串:string 变量名 = “字符串值”

#include<iostream>
using namespace std;
int main()
{
	//C风格字符串
	char  str1[] = "helloworld";
	cout << str1 << endl;

	//C++风格字符串 如果报错就 #include<string>
	string str2 = "helloworld2";
	cout << str2 << endl;

	system("pause");

	return 0;

}

布尔类型 bool

作用:布尔数据类型代表真或假的值
bool类型只有两个值
true 真 1
false 假 0
bool类型占1个字节大小

#include<iostream>
using namespace std;
int main()
{

	bool flag = true;
	cout << flag << endl;
	cout << sizeof(flag) << endl;
	flag = false;
	cout << flag << endl;


	system("pause");

	return 0;

}

数据的输入

作用:用于从键盘获取数据
关键字:cin
语法:cin >> 变量

#include<iostream>
using namespace std;
int main()
{
	//整型输入
	int a = 0;
	cout << "请给a赋值:";
	cin >> a;
	cout << "整型变量a=" << a << endl;

	//浮点型输入
	float f = 1.12;
	cout << "请给f赋值:";
	cin >> f;
	cout << "整型变量f=" << f << endl;
	
	//字符型输入
	char ch = 'a';
	cout << "请给ch赋值:";
	cin >> ch;
	cout << "整型变量ch=" << ch << endl;

	//字符串型输入
	char str = 'ab';
	cout << "请给str赋值:";
	cin >> str;
	cout << "整型变量str=" << str << endl;

	//布尔型输入
	//非0的值都代表真,都会返回1
	char flag = false;
	cout << "请给flag赋值:";
	cin >> flag;
	cout << "整型变量flag=" << flag << endl;

	system("pause");

	return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值