字符串型
作用:用于表示一串字符 两种风格: 1、C风格字符串 char 变量名[]="字符串值” 2、C++风格字符串:string 变量名 = “字符串值”
#include<iostream>
using namespace std;
int main()
{
char str1[] = "helloworld";
cout << str1 << endl;
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;
char flag = false;
cout << "请给flag赋值:";
cin >> flag;
cout << "整型变量flag=" << flag << endl;
system("pause");
return 0;
}
|