1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
/*The CandyBar structure contains three members. The first member holds the brand name of a candy bar.
The second member holds the weight (which may have a fractional part) of the candy bar, and the third member holds the number of calories (an integer value) in the candy bar. Write a program that uses a function that takes as arguments a reference to CandyBar, a pointer-to-char, a double, and an int and uses the last
three values to set the corresponding members of the structure. The last three arguments should have default values of "Millennium Munch", 2.85, and 350.
Also, the program should use a function that takes a reference to a CandyBar as an argument and displays the contents of the structure.
Use const where appropriate.
*/
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;
const int max = 20;
struct CandyBar
{
char name[max];
double weight;
int calories;
};
void set_structure(CandyBar & cb, const char * str = "Millennium Munch", double w = 2.85, int c = 350);
void display(CandyBar & cb);
int main()
{
CandyBar crunch;
set_structure(crunch);
display(crunch);
const char * bar_name = "Yumness";
double grams = 3.5;
int cals = 400;
set_structure(crunch, bar_name, grams, cals);
display(crunch);
system("pause");
return 0;
}
void set_structure(CandyBar & cb, const char * str, double w, int c)
{
strncpy_s(cb.name, str, sizeof(str));
cb.weight = w;
cb.calories = c;
}
void display(CandyBar & cb)
{
cout << "Name: " << cb.name << endl;
cout << "Weight: " << cb.weight << endl;
cout << "Calories: " << cb.calories << endl;
}
|