4.11 编程练习

在这里插入图片描述
在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    const int ArSize = 20;
    char first[ArSize];
    char last[ArSize];
    char grade;
    int age;

    cout << "What is your first name?";
    cin.getline(first, 20);
    cout << "What is your last name?";
    cin.getline(last, 20);
    cout << "What letter grade do you deserve?";
    cin >> grade;
    cout << "What is your age?";
    cin >> age;
    cout << "Name: " << last << ", " << first << endl;
    cout << "Grade: " << char(grade + 1) << endl;
    cout << "Age: " << age << endl;

    return 0;
}

在这里插入图片描述
待解决问题:

  1. 关于输入带空格的问题?

2. [1]

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string name;
    string dessert;

    cout << "Enter your name:\n";
    getline(cin, name);
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";

    return 0;
}

在这里插入图片描述

3.

#include <iostream>

int main()
{
    using namespace std;

    const int ArSize = 20;
    char first[ArSize];
    char last[ArSize];

    cout << "Enter your first name:";
    cin.getline(first, ArSize);
    cout << "Enter your last name:";
    cin.getline(last, ArSize);
    cout << "Here's the information in a single string:" << last << ", " << first << endl;
    
    return 0;
}

在这里插入图片描述

#include <iostream>
#include <string>

int main()
{
    using namespace std;

    string first;
    string last;

    cout << "Enter your first name:";
    getline(cin, first);
    cout << "Enter your last name:";
    getline(cin, last);
    cout << "Here's the information in a single string:" << last << ", " << first << endl;

    return 0;
}

在这里插入图片描述

#include <iostream>

int main()
{
    using namespace std;

    struct CandyBar
    {
        char brand[20];
        double weight;
        int calorie;
    };

    CandyBar snack = {"Mocha Munch", weight: 2.3, .calorie = 350};      //brand: {'M', 'o', 'c', 'h', 'a', ' ', 'M', 'u', 'n', 'c', 'h'}.

    cout << "brand:" << snack.brand << endl;
    cout << "weight:" << snack.weight << endl;
    cout << "calorie:" << snack.calorie << endl;

    return 0;
}

在这里插入图片描述

6.

#include <iostream>

int main()
{
    using namespace std;

    struct CandyBar
    {
        char brand[20];
        double weight;
        int calorie;
    };

    CandyBar snack[3] =
            {
                {"Mocha Munch", 2.3, 350},
                         {"dabaitu",2.8, 300},
                         {"Alpine", 2.2, 320}
            };

    cout << "brand0:" << snack[0].brand << endl;
    cout << "weight0:" << snack[0].weight << endl;
    cout << "calorie0:" << snack[0].calorie << endl;

    cout << "brand1:" << snack[1].brand << endl;
    cout << "weight1:" << snack[1].weight << endl;
    cout << "calorie1:" << snack[1].calorie << endl;

    cout << "brand2:" << snack[2].brand << endl;
    cout << "weight2:" << snack[2].weight << endl;
    cout << "calorie2:" << snack[2].calorie << endl;

    return 0;
}

在这里插入图片描述

7.

#include <iostream>

int main()
{
    using namespace std;

    struct Pizza
    {
        char company[20];
        double diameter;
        double weight;
    };

    Pizza snack;

    cout << "Please enter the company of the pizza:";
    cin.getline(snack.company, 20);
    cout << "Please enter the diameter of the pizza:";
    cin >> snack.diameter;
    cout << "Please enter the weight of the pizza:";
    cin >> snack.weight;

    cout << "company: " << snack.company << endl;
    cout << "diameter: " << snack.diameter << endl;
    cout << "weight: " << snack.weight << endl;

    return 0;
}

在这里插入图片描述

待解决问题:
1.输出的company分成了两行?

#include <iostream>

int main()
{
    using namespace std;

    struct Pizza
    {
        char company[20];
        double diameter;
        double weight;
    };

    Pizza *ps = new Pizza;

    cout << "Please enter the diameter of the pizza:";
    cin >> (*ps).diameter;
    cin.get();
    cout << "Please enter the company of the pizza:";
    cin.getline(ps->company, 20);
    cout << "Please enter the weight of the pizza:";
    cin >> ps->weight;

    cout << "diameter: " << (*ps).diameter << endl;
    cout << "company: " << ps->company << endl;
    cout << "weight: " << ps->weight << endl;

    return 0;
}

在这里插入图片描述

9.

#include <iostream>

int main()
{
    using namespace std;

    struct CandyBar
    {
        char brand[20];
        double weight;
        int calorie;
    };

    CandyBar *ps = new CandyBar [3];			//CandyBar *ps = new CandyBar;这么写也能正常运行。

    cout << "Please enter the brand0 of the candy:";
    cin.getline(ps[0].brand,20);
    cout << "Please enter the weight0 of the candy:";
    cin >> ps[0].weight;
    cout << "Please enter the caorie0 of the candy:";
    cin >> ps[0].calorie;

    cout << "Please enter the brand1 of the candy:";
    cin.get();
    cin.getline(ps[1].brand,20);
    cout << "Please enter the weight1 of the candy:";
    cin >> ps[1].weight;
    cout << "Please enter the caorie1 of the candy:";
    cin >> ps[1].calorie;

    cout << "Please enter the brand2 of the candy:";
    cin.get();
    cin.getline(ps[2].brand,20);
    cout << "Please enter the weight2 of the candy:";
    cin >> ps[2].weight;
    cout << "Please enter the caorie2 of the candy:";
    cin >> ps[2].calorie;


    cout << "brand0:" << ps[0].brand << endl;
    cout << "weight0:" << ps[0].weight << endl;
    cout << "calorie0:" << ps[0].calorie << endl;

    cout << "brand1:" << ps[1].brand << endl;
    cout << "weight1:" << ps[1].weight << endl;
    cout << "calorie1:" << ps[1].calorie << endl;

    cout << "brand2:" << ps[2].brand << endl;
    cout << "weight2:" << ps[2].weight << endl;
    cout << "calorie2:" << ps[2].calorie << endl;

    return 0;
}

在这里插入图片描述

待解决问题:

  1. 声明的动态数组元素个数小于实际的输入个数?

参考资料:
[1] 4.3.3 string类I/O

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

淘淘图兔兔呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值