第一章 C++编程基础(如何撰写C++程序)

来源:互联网 发布:海康威视设备域名注册 编辑:程序博客网 时间:2024/06/15 09:06

P1.1  p6

 

读取用户姓名并存储,最后向用户打招呼。

 

#include<iostream>  // 1

#include<string>   //2

using namespace std;  //3

int main()

{

   string user_name;

   cout<<"please enter your first name:";

   cin>> user_name;

   cout<<'\n'

   cout<<"hello,"

      << user_name

      <<"welcome,and bye bye...";

 

return o;      

 

}

 

1.c++标准的的输入输出库名为iostream,其中包含相关整套的class,用于支持对终端和文件的输入与输出。

2.string class 用于存储数据。

3.using namespace std

4.字符常量分为两类:一类为可打印字符,如(英文,数字,标点符号),另一种为不可打印字符,如(换行符\n  制表符\t...)字符常量由一组单引号括住。

5.cin,cout的用法见代码。

 

 

习题1.4  在上题扩充,要求分别输入用户的姓和名,并打印用户的姓和名。

 

#include<iostream>

#include<string>

using namespace std;

int main()

 

{

   string user_first name,user_last name;

   cout<<hello,please enter your first name;

   cin>>user_first name;

   cout<<hello,please enter your last name;

   cin>>user_last name;

   cout<<hello\n

       <<user_first name

       <<

       <<user_last name;

 

       Return 0;

 

}

 

0 0
原创粉丝点击