c++学习(三)

#include <iostream>

using namespace std;
const int ArSize = 20;
struct CandyBar
       {
        char name[ArSize];
        double weight;
        int calorie;
        };
struct MyStruct
        {
            std::string name;
            int age;
        };
int main()
{
   CandyBar snack = { "zhou" ,3.2,580};
   cout<< snack.name<<endl;
   cout<< snack.weight<<endl;
   cout<< snack.calorie<<endl;
   MyStruct zhou;
   zhou.name = "zzm";//字符串这里就不用限定他的长度
   cout<<zhou.name<<endl;

   // cout<<snack.calorie<<endl;
 //   cout << "Hello World!" << endl;
    return 0;
}

注意字符串在结构体中的输入方式的变化

//exercise4_6
#include <iostream>

using namespace std;
const int ArSize = 20;
struct CandyBar
       {
        char name[ArSize];
        double weight;
        int calorie;
        };
struct MyStruct
        {
            std::string name;
            int age;
        };
int main()
{
   CandyBar snack ;
   cout<<"Please input the candy name:";
   cin.getline(snack.name,ArSize);//这里可能包含空格
   cout<<"Please input weighe: ";
   //(cin>>snack.weight).get();//防止下一行输入出现问题
   (cin>>snack.weight);//防止下一行输入出现问题
   cout<<"It's calorle : ";
 //  cin.getline(snack.calorie,1);//这里为什么就不可以?
   cin>>snack.calorie;

   cout<< snack.name<<endl;
   cout<< snack.weight<<endl;
   cout<< snack.calorie<<endl;
 //  MyStruct zhou;
   //zhou.name = "zzm";//字符串这里就不用限定他的长度
   //cout<<zhou.name<<endl;

   // cout<<snack.calorie<<endl;
 //   cout << "Hello World!" << endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值