C++ Primer Plus 第四章 课后练习题

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

int main()
{
	string f_name;
	string l_name;
	char deserve;
	int age;
	cout << "what is your first name?";
	getline(cin,f_name);
	cout << "what is your last name?";
	getline(cin,l_name);
	cout << l_name; 
	cout << "what is your deserve?";
	cin >> deserve;
	cout << "what is your age?";
	cin >> age;
	deserve = deserve + 1;
	
	
	cout << "Name:" << l_name << "," << f_name <<endl;
	cout << "Grade:" << deserve<<endl;
	cout << "Age" << age <<endl;  
	
return 0;     
 } 

 运行结果:

  

以下为程序清单4.4

 

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

int main()
{	
	string name;
	string dessert;
	cout << "Enter your name:" <<endl;
	getline(cin,name);
	cout << "Enter your favorite dessert:" <<endl;
	getline(cin,dessert);
	cout << "I have some delicious " << dessert << "for you " <<name;
	
return 0;     
} 

运行结果:

#include<iostream>
using namespace std;
int main()
{	
	char f_name[20];
	char l_name[20];
	cout << "Enter your first name:";
	cin.getline(f_name,10); 
	cout << "Enter your last name:";
	cin.getline(l_name,10);
	cout << "Here's the information in a singal string:" << l_name << "," <<f_name;
	return 0;     
} 

运行结果:

#include<iostream>
#include<string>
using namespace std;
int main()
{	
	string f_name;
	string l_name;
	cout << "Enter your first name:";
	getline(cin,f_name);
	cout << "Enter your last name:";
	getline(cin,l_name);
	cout << "Here's the information in a singal string:" << l_name << "," <<f_name;
	return 0;     
} 

运行结果:

#include<iostream>
using namespace std;
struct 	CandyBar{
	string table;
	double weight;
	int carlis;
};

int main()
{	
	CandyBar snack {"Morcha Munch", 2.3,350};
	cout << snack.table << endl;
	cout << snack.weight << endl; 
	cout << snack.carlis << endl;
	return 0;    
} 

 运行结果:

#include <iostream>
using namespace std;
int main()
{   struct CandyBar
    {  
        char name[20];
        double weight;
        int calorie;
    };
    CandyBar snack[3] = {{"Mocha Munch",2.3,350},{"Mocha Munch",2.3,350},{"Mocha Munch",2.3,350}};
    for(int i = 0;i<3;++i)
    {
        cout << snack[i].name << endl;
        cout << snack[i].weight << endl;
        cout << snack[i].calorie << endl;
    }
    return 0;
}
  

#include<iostream>
#include<string> 
using namespace std;
struct pisha{
	string c_name;
	double weight;
	double diameter;
};

int main()
{	
	pisha PS;
	cout << "Enter the pisha's name:";
	getline(cin,PS.c_name);
	cout << "Enter the pisha's weight:";
	cin >> PS.weight;
	cout << "Enter the pisha's diameter:";
	cin >> PS.diameter;
	
	
	cout << "披萨品牌:" << PS.c_name <<endl;
	cout << "披萨重量:" << PS.weight <<endl;
	cout << "披萨直径:" << PS.diameter <<endl; 
	return 0;    
} 

运行结果:

#include<iostream>
#include<string> 
using namespace std;
struct pisha{
	string c_name;
	double weight;
	double diameter;
};

int main()
{	
	pisha *p = new pisha;
	cout << "Enter the pisha's diameter:";
 	cin >> p->diameter;
	cin.ignore();  //用该函数清除缓冲区内的数据
	cout << "Enter the pisha's name:";
	getline(cin,p->c_name);
	cout << "Enter the pisha's weight:";
	cin >> p->weight;

	cout << "披萨品牌:" << p->c_name  <<endl;
	cout << "披萨重量:" << p->weight <<endl;
	cout << "披萨直径:" << p->diameter <<endl; 
	return 0;    
} 

运行结果:

用户输入完披萨的直径后按回车结束输入,把“\n”留在了输入流里,而cin不会主动删除输入流内的换行符,这样换行符就被getline读取到,getline遇到换行符返回,因此程序不会等待用户输入。
解决的办法是手动清除换行符,在cin>>后加上 cin.ignore();

因此在使用getline函数之前要注意,输入流中是否有可能会有换行符,如果有则使用cin.ignore清除。

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

int main()
{
    struct CandyBar
    {
        string band;
        double weight;
        int calorie;
    };

    CandyBar *candy=new CandyBar[3];
    //初始化candy指针,使用new申请内存;
    //初始化candy所指向的数组 
    candy[0] = { "aa",5.6,1800 };
    candy[1] = { "bb",7.7,2010 };
    candy[2] = { "cc",4.7,1005 };

    for (int i = 0;i < 3;i++)
    {
        cout << "The information of candy " << i+1 << " is followed as below:\n";
        cout << "Band:" << candy[i].band << endl;
        cout << "Weight:" << candy[i].weight << endl;
        cout << "Calorie:" << candy[i].calorie << endl;
    }
    delete[] candy;
    //释放内存;
    return 0;
}

 运行结果:

 

#include <iostream>
#include <string>
#include <array>
using namespace std;
int main()
{	
	double sum=0;
	array<double,3> run_mark;
	for(int i=0;i<3;i++){
		cout << "请输入您的跑步成绩:"; 
		cin >> run_mark[i];
		sum += run_mark[i];
	}
	cout << "您的三次平均成绩为:"<< sum/3 ;
    return 0;
}

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值