Skip to content

Instantly share code, notes, and snippets.

@Jules-Baratoux
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jules-Baratoux/87b9eefd5b96984d4f06 to your computer and use it in GitHub Desktop.
Save Jules-Baratoux/87b9eefd5b96984d4f06 to your computer and use it in GitHub Desktop.
Homework #8 – Streams
John Smith 37 72.25 189.37
Sally Jones 26 60.18 107.88
Aaron Anderson 12 55.50 89.30
Janet Anderson 10 49.00 60.98
Brian Doe 28 76.11 238.00
Brett Daniels 48 69.01 189.74
#include <iostream>
using std::cout;
#include <fstream>
using std::ifstream;
using std::ios_base;
#include <sstream>
using std::stringstream;
#include <cstdlib>
#include <cassert>
#include "Person.h"
using namespace JulesBaratoux;
int main(int argc, const char *argv[])
{
Person::tests::operator_insert();
Person::tests::operator_extract();
// Open a file named “hw8-input.txt” using an fstream
ifstream file("hw8-input.txt", ios_base::in);
// If the file opened successfully:
if (file)
{
Person person;
stringstream stream;
// While the end of the file has not been reached:
// Extract the next person from the file
while (file >> person)
{
// Insert the person and a newline into a stringstream
stream << person << '\n';
}
// Output the contents of the stringstream to stdout
cout << stream.str();
}
return EXIT_SUCCESS;
}
#include <string>
using std::string;
#include <iostream>
using std::istream;
using std::ostream;
#include <sstream>
using std::stringstream;
#include <cassert>
#include "Person.h"
using namespace JulesBaratoux;
// ----------------------------------------------------------------------------
istream& JulesBaratoux::operator>>(istream& in, Person& person)
{
string firstName;
string lastName;
int ageYears;
double heightInches;
double weightPounds;
if ( (in >> firstName)
and (in >> lastName)
and (in >> ageYears)
and (in >> heightInches)
and (in >> weightPounds) )
{
person.firstName = firstName;
person.lastName = lastName;
person.ageYears = ageYears;
person.heightInches = heightInches;
person.weightPounds = weightPounds;
}
return in;
}
void Person::tests::operator_extract()
{
// Create and populate a stream with valid data
stringstream stream;
stream.str("John Smith 37 72.25 189.37");
// Create and populate an instance using the stream's data
Person person;
stream >> person;
// Test instance attributes correctness
assert( person.firstName == "John" );
assert( person.lastName == "Smith" );
assert( person.ageYears == 37 );
assert( person.heightInches == 72.25 );
assert( person.weightPounds == 189.37 );
// Repopulate a stream with invalid data and try to extract it to the instance
stream.str("42");
stream >> person;
// Test instance conservation
assert(person.firstName == "John");
}
// ----------------------------------------------------------------------------
ostream& JulesBaratoux::operator<<(ostream& out, const Person& person)
{
return out << person.firstName << delim
<< person.lastName << delim
<< person.ageYears << delim
<< person.heightInches << delim
<< person.weightPounds;
}
void Person::tests::operator_insert()
{
// Create and populate instance
Person person;
person.firstName = "John";
person.lastName = "Smith";
person.ageYears = 37 ;
person.heightInches = 72.25 ;
person.weightPounds = 189.37 ;
// Create and populate stream
stringstream stream;
stream << person;
// Test stream content
assert(stream.str() == "John Smith 37 72.25 189.37");
}
#pragma once
#include <iostream>
using std::istream;
using std::ostream;
#include <string>
using std::string;
namespace JulesBaratoux
{
static constexpr char delim = ' ';
struct Person
{
friend istream& operator>>(istream& in, Person& person);
friend ostream& operator<<(ostream& out, const Person& person);
struct tests
{
static void operator_insert();
static void operator_extract();
};
private:
string firstName;
string lastName;
int ageYears;
double heightInches;
double weightPounds;
friend tests;
};
istream& operator>>(istream& in, Person& person);
ostream& operator<<(ostream& out, const Person& person);
}
@Jules-Baratoux
Copy link
Author

John Smith 37 72.25 189.37
Sally Jones 26 60.18 107.88
Aaron Anderson 12 55.5 89.3
Janet Anderson 10 49 60.98
Brian Doe 28 76.11 238
Brett Daniels 48 69.01 189.74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment