c++ prime plus 第四章1~4

今天本该有一节体育课的,但是下雨了,所以就躲到图书馆看看我的c++。主要看的是指针的一些内容。

1.枚举量,定义符号常量。

2.指针,new和delete,指针和数组的关系。

3.数组的地址,是整个数组的地址。如果想打印字符串的地址,需要强制转换。

//c++ 4.8.3实验.cpp

#include <iostream>

using namespace std;

int main()
{
    const char * p = "Hello world!";
    cout << p << " at " << (int *) p << endl;
//输出指向字符串的地址
    int sentence2[4] = {12,45,8,5};
    int * p2 = sentence2;
    cout << p2 << endl;
    cout << &sentence2 << endl;
    cout << sentence2+1 << endl;
    cout << &sentence2+1 << endl;
    return 0;
}


//char sentence = "Hello world!";
//error: invalid conversion from 'const char*' to 'char'
//char sentence[15] = "Hello world!";
4.暂时忽略了结构体


//answer

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

int main()
{
    //char firstname[10] = "hell wrld";
    //cout << "What is your first name? " ;
    //cout << firstname << endl;   不能读一行

    char name[20] ;
    cout << "What is your name? " ;
    cin.getline (name, 20);

    char firstname[20] ;
    cout << "What is your first name? " ;
    cin.get(firstname, 20);
    cin.get();//get 和 getline 的不同使用

    //cout << "What is your last name? ";
    //string lastname ;
    //getline(cin, lastname);
    //strcat_s脱胎于strcat,用于两个字符串的链接,strcat不支持string类型

    char lastname[20] ;
    cout << "What is your last name? " ;
    cin.getline(lastname, 20);

    cout << "What letter grade do you deserve? ";
    char grade;
    cin >> grade;
    grade++;

    cout << "What is your age?";
    int age;
    cin >> age;

    cout << "Who do you love?";
    char hername[20];
    cin.get();
    cin.getline(hername,20);

    char allname[80] ;
    strcpy(allname, firstname);
    strcat(allname, " , ");
    strcat(allname, lastname);

    cout << "Name: " << lastname << " , " << firstname << endl;
    cout << allname << endl;
    cout << "Grade: " << grade << endl;
    cout << "Age: " << age << endl;
    cout << "lover: " << hername << endl;
    return 0;
}
这个没怎么用到指针,好气啊。感觉我看了好长时间,最后写题目没用到。。。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值