#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;
}