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
|
#include <iostream>
#include <fstream>
using namespace std;
void print(const char name[], const int& age, const int weight, ostream& out = cout);
int main(void)
{
ofstream outFile;
const int SIZE = 128;
int age, weight;
char name[SIZE];
cout << "Please enter your name: ";
cin.getline(name, SIZE);
cout << "Enter your age: ";
cin >> age; cin.ignore(80, '\n');
cout << "Enter your weight: ";
cin >> weight; cin.ignore(80, '\n');
print(name, age, weight);
outFile.open("output.txt");
if (outFile.is_open())
{
print(name, age, weight, outFile);
outFile.close();
}
else
cout << "Couldn't open \"output.txt\"\n";
cout << "\n\nAll done, hit enter to quit.";
cin.get();
return 0;
}
void print(const char name[], const int& age, const int weight, ostream& out)
{
if (out == cout)
out << endl;
out << "Your name is " << name << endl
<< "You are " << age << " years old" << endl
<< "You weight " << weight << " tons.";
}
|