//golf.h
#ifndef GOLF_H_
#define GOLF_H_
const int Len = 40;
struct golf {
char fullname[Len];
int handicap;
};
void setgolf(golf& g, const char* name, int hc);
int setgolf(golf& g);
void handicap(golf& g, int hc);
void showgolf(const golf& g);
#endif
//golf.cpp
#include<iostream>
#include<cstring>
#include "golf.h"
void setgolf(golf& g, const char* name, int hc)
{
strcpy_s(g.fullname, name);
g.handicap = hc;
}
int setgolf(golf& g)
{
using namespace std;
cout << "Please enter user's name: ";
cin.getline( g.fullname, Len);
if (strcmp(g.fullname, "") == 0)
return 0;
else {
cout << "Please enter user's handicap: ";
cin >> g.handicap;
cin.get();
return 1;
}
}
void handicap(golf& g, int hc)
{
g.handicap = hc;
}
void showgolf(const golf& g)
{
using namespace std;
cout << "Fullname: " << g.fullname << endl;
cout << "Handicap: " << g.handicap << endl;
}
//main.cpp
#include<iostream>
#include "golf.h"
using namespace std;
int main()
{
golf g[3];
int i;
for (i = 0; i < 3; i++) {
if (setgolf(g[i]) == 0)
break;
}
for (int j = 0; j < i; j++) {
showgolf(g[j]);
}
cout << "\nReset the information of all players\n";
for (int j = 0; j < i; j++) {
handicap(g[j], 90);
showgolf(g[j]);
}
return 0;
}
#include<iostream>
#include<string>
using namespace std;
void strcount(const string str);
int main()
{
string input;
cout << "Enter a line: \n";
while (getline(cin, input)) {
if (input == "")
break;
strcount(input);
cout << "Enter next line (empty line to quit):\n";
}
cout << "Bye!\n";
return 0;
}
void strcount(const string str)
{
static int total = 0;
int count = 0;
count = str.size();
total += count;
cout << "\"" << str << "\" contains";
cout << count << " characters\n";
cout << total << " characters total\n";
}
#include<iostream>
#include<cstring>
#include<new>
using namespace std;
struct chaff {
char dross[20];
int slag;
};
int main()
{
char buffer[50] = "";
chaff* pt = new chaff[2];
chaff* dt = new(buffer) chaff[2];
for (int i = 0; i < 2; i++) {
strcpy_s(pt[i].dross, "i love you!");
pt[i].slag = 3;
strcpy_s(dt[i].dross, "you love me!");
dt[i].slag = 10;
}
for (int i = 0; i < 2; i++) {
cout << pt[i].dross << " " << pt[i].slag << endl;
cout << dt[i].dross << " " << dt[i].slag << endl;
}
delete[]pt;
return 0;
}
//sale.h
#ifndef SALE_H_
#define SALE_H_
namespace SALES {
const int QUARTERS = 4;
struct Sales {
double sales[QUARTERS];
double average;
double max;
double min;
};
void setSales(Sales& s, const double ar[], int n);
void setSales(Sales& s);
void showSales(const Sales& s);
}
#endif
//sale.cpp
#include<iostream>
#include "sale.h"
namespace SALES{
void setSales(Sales& s, const double ar[], int n)
{
double sum = 0.0;
if (n >= QUARTERS) {
for (int i = 0; i < QUARTERS; i++) {
s.sales[i] = ar[i];
sum += ar[i];
}
s.average = sum / QUARTERS;
}
else {
int i;
for (i = 0; i < n; i++) {
s.sales[i] = ar[i];
sum += ar[i];
}
while (i < QUARTERS)
s.sales[i++] = 0;
s.average = sum / n;
}
s.min = s.sales[0], s.max = s.sales[0];
for (int i = 1; i < QUARTERS; i++) {
if (s.sales[i] < s.min)
s.min = s.sales[i];
if (s.sales[i] > s.max)
s.max = s.sales[i];
}
}
void setSales(Sales& s)
{
using namespace std;
double sum = 0.0;
cout << "Enter 4 double numbers: ";
for (int i = 0; i < QUARTERS; i++) {
cin >> s.sales[i];
sum += s.sales[i];
}
s.average = sum / QUARTERS;
s.min = s.sales[0], s.max = s.sales[0];
for (int i = 1; i < QUARTERS; i++) {
if (s.sales[i] < s.min)
s.min = s.sales[i];
if (s.sales[i] > s.max)
s.max = s.sales[i];
}
}
void showSales(const Sales& s)
{
using namespace std;
for (int i = 0; i < QUARTERS; i++) {
cout << s.sales[i] << " ";
}
cout << endl;
cout << "Average: " << s.average << endl;
cout << "Max: " << s.max << endl;
cout << "Min: " << s.min << endl;
}
}
//main.cpp
#include<iostream>
#include "sale.h"
using namespace std;
int main()
{
using namespace SALES;
Sales s[2];
double arr[6] = { 25, 36.2, 45.3, 12.3, 75.2, 12.0 };
setSales(s[0]);
setSales(s[1], arr, 6);
for (int i = 0; i < 2; i++) {
showSales(s[i]);
cout << "---------------------\n";
}
return 0;
}