C++ Primer Plus(第六版)中文版编程练习答案

本文提供C++ Primer Plus第六版的中文版编程练习答案,涵盖从基础到进阶的各个章节。
摘要由CSDN通过智能技术生成

45 C++ Primer Plus(第六版)第四章 编程练习答案

1

#include<iostream>
#include<string>
#include<vector>
#include<array>
using namespace std;

int main()
{
   
	char firstname[20];
	char lastname[20];
	char grade;
	//enum grade { A,B,C,D,F };
	int age;
	cout << "What's your first name? ";
	cin.getline(firstname, 19);
	cout << "What's your last name? ";
	cin.getline(lastname, 19);
	cout << "What letter grade do you deserve? ";
	cin >> grade;
	cout << "What's your age? ";
	cin >> age;
	cout << "Name: " << firstname << ", " << lastname << endl;
	cout << "Grade: " << char(grade + 1) << endl;//向下调整成绩,即上调一个字母,grade+1
	cout << "Age: " << age << endl;

	system("pause");

	return 0;

}

2

// instr2.cpp -- reading more than one word with getline
#include <iostream>
#include<string>

int main()
{
   
    using namespace std;
    const int ArSize = 20;

    //char name[ArSize];
    //char dessert[ArSize];

    //cout << "Enter your name:\n";//abcdefghijklmnopqrs
    //cin.getline(name, ArSize);  // reads through newline
    //cout << "Enter your favorite dessert:\n";
    //cin.getline(dessert, ArSize);
    //cout << "I have some delicious " << dessert;
    //cout << " for you, " << name << ".\n";
    // cin.get();

    string name;
    string dessert;
    cout << "Enter your name:\n";
    getline(cin, name);//将一行输入读取到string对象中。
    //istream有类中,有处理double、int等基本数据类型的类方法,没有处理string对象的类方法。
    char arr[ArSize];
    cout << "Enter arr:";
    cin.getline(arr, 20);
    //cin >> name;//只能读取第一个单词,后续单词自动给到下一个读取
    cout << "Enter your favorite dessert:\n";
    getline(cin, dessert);//将一行输入读取到string对象中
    //cin >> dessert;//只能读第一个单词,后续单词自动给到下一个读取
    cout << "I have some delicious " << dessert;
    cout << " for you, " << name << ".\n";
    cout << "arr = " << arr << endl;

    system("pause");
    return 0; 
}

3

// instr2.cpp -- reading more than one word with getline
#include <iostream>
#include<cstring>

int main()
{
   
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值