Programming Principles and Practice Using C++ Notes2

第三章对象、类型和值

对象:用来保存一个指定类型值的一些内存单元。

类型:定义一组可能的值与一组运算(对于一个对象)。

值:根据一个类型来解释的内存中的一组比特。

#include <iostream>
using namespace std;
int main()
{
        cout << "Please enter your first name and age\n"<< endl;

        string first_name;
        int age;
            
        cin >> first_name; 
        cin >> age;
        cout << "Hello,"<< first_name << "(age" << age <<")\n"<< endl;

        return 0;
}

这里我曾犯了个错误。我是这么写的(想当然了),结果g++了一堆错误提示。

cin >> first_name >> endl;
cin >> age >> endl;

 “字符串+”意味着“连接”,也就是说:

当S1 和 S2是字符串时, S1+S2也是字符串,包含来自S1的字符后接来自S2的多个字符。

例如:S1的值为“Hello",S2的值为”World”,那么S1+S2的值为“Hello World”

 1 #include <iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6         cout << "Please enter your first ande second names:\n"<< endl;
 7     
 8         string s1;
 9         string s2;
10 
11         cin >> s1 >> s2;     //read two strings
12 
13         string name = s1 +''+ s2; //concatenate strings
14 
15         cout << "Look,"<< name <<'\n';
16 
17         return 0;
18 
19 }

g++报错提示:13: error: empty character constant

string name = s1 +' '+ s2; //单引号中间应有空格

 

转载于:https://www.cnblogs.com/Charons/p/11171520.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值