C++继承了C绝大部分的语法,并进行了扩展。比如输入输出,C语言的scanf和printf,变成了C++的cout<<和cin>>。
现在以一个常用的学生信息的例子来说明C++的输入输出功能。
//学生信息的输入和输出功能
#include <iostream> //输入与输出头文件
using namespace std; //输入输出的命名空间,std::cin,std::cout
struct Student //学生结构体
{ char m_strName[20]; //姓名
char m_strID[12]; //编号
char m_cSex; //性别:'0':男 '1':女
char m_strMajor[20]; //专业
};
int main()
{ Student Student1; //结构名可直接作为类型名
cout<<"姓名:\n"; //从屏幕上输出信息.....左出右入
cin>>Student1.m_strName; //从键盘上输入信息
cout<<"编号:\n";
cin>>Student1.m_strID;
cout<<"性别('0':男,'1':女):\n";
cin>>Student1.m_cSex;
cout<<"专业:\n";
cin>>Student1.m_strMajor;
cout<<"该学生的信息为:\n";
cout<<"姓名:"<<Student1.m_strName<<" "<<"编号:"<<Student1.m_strID<<" "<<"性别:";
if(Student1.m_cSex == '0')
cout<<"男";
else
cout<<"女";
cout<<" ";
cout<<"专业:"<<Student1.m_strMa