天天看點

C++ Primer Plus 7.3

#include <iostream>

#include <string>

//#include <cstring>

struct box

{

char maker[40];

float height;

float width;

float length;

float volume;

};

void Set_struct(box *);

void Disp_struct(box);

void main()

{

using namespace std;

box ch;

cout<<"Please enter parameters of box: /n"<<"maker: ";

cin.getline(ch.maker,40);

cout<<"height: ";

cin>>ch.height;

while(cin.fail())

{

cout<<"Enter error, please enter correct number: ";

cin.clear();

while(cin.get()!='/n');

cin>>ch.height;

}

cout<<"width: ";

cin>>ch.width;

while(cin.fail())

{

cout<<"Enter error, please enter correct number: ";

cin.clear();

while(cin.get()!='/n');

cin>>ch.width;

}

cout<<"length: ";

cin>>ch.length;

while(cin.fail())

{

cout<<"Enter error, please enter correct number: ";

cin.clear();

while(cin.get()!='/n');

cin>>ch.length;

}

Set_struct(&ch);

Disp_struct(ch);

}

void Set_struct(box *ch)

{

ch->volume=ch->height*ch->width*ch->length;

}

void Disp_struct(box ch)

{

using namespace std;

cout<<"/nmake: "<<ch.maker<<endl;

cout<<"height: "<<ch.height<<endl;

cout<<"width: "<<ch.width<<endl;

cout<<"length: "<<ch.length<<endl;

cout<<"volume: "<<ch.volume<<endl;

}