View Single Post
 
Old May 18th, 2016, 11:30 PM
phztfte1 phztfte1 is offline
Authorized User
Points: 384, Level: 6
Points: 384, Level: 6 Points: 384, Level: 6 Points: 384, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2014
Posts: 91
Thanks: 2
Thanked 1 Time in 1 Post
Default Chapter 15 Demystifying C++ I/O

Hi Marc,

I revised the output() method as you suggested and added an input() method. The input() method does not work but at least, it is a start.
1. The input() method does not read the bool value for chips correctly. Instead of reading false, it shows true.
2. The exercise raises a question: how do you get to read the next record?

Code:
include <iostream>
#include <sstream>
#include <string>
#include <fstream>

using namespace std;

class Muffin 
{
    public:
        string    getDescription() const;
        void      setDescription(const string& inDesc);
        
		int       getSize() const;
        void      setSize(int inSize);
        
		bool      getHasChocolateChips() const;
        void      setHasChocolateChips(bool inChips);

        void      output();
		void	  input();
    protected:
        string    mDesc;
        int       mSize;
        bool      mHasChocolateChips;
};

string Muffin::getDescription() const { return mDesc; }
void Muffin::setDescription(const string& inDesc) { mDesc = inDesc; }
int Muffin::getSize() const { return mSize; }
void Muffin::setSize(int inSize) { mSize = inSize; }
bool Muffin::getHasChocolateChips() const { return mHasChocolateChips; }
void Muffin::setHasChocolateChips(bool inChips) { mHasChocolateChips = inChips; }

void Muffin::output()
{
  printf("%s, Size is %d, %s\n", getDescription().c_str(), getSize(),    
     (getHasChocolateChips() ? "has chips" : "no chips"));

  ofstream outFile("Muffin.txt", ios::out | ios::app);
  if (!outFile) {
	  cerr << "File could not open!" << endl;
	  return;
  }
  outFile << getDescription() << '^' << getSize() << '^' << boolalpha << getHasChocolateChips() << '\n';
}


void Muffin::input()
{
	string description;
	int size;
	bool chips;

	ifstream inFile("Muffin.txt", ios::in);
	if (!inFile) {
		cerr << "File could not open!" << endl;
		return;
	}
		getline(inFile, description, '^');
		inFile >> size;
		inFile >> chips;
		cout << "inFile: " << description << " " << size << " " << boolalpha << chips << endl;
}


Muffin createMuffin(istringstream& inStream);

int main()
{
  cout << "First, let's create a muffin in code and output it." << endl;

  Muffin m;
  m.setDescription("Giant Blueberry Muffin");
  m.setSize(42);
  m.setHasChocolateChips(false);
  m.output();

  cout << "Now we'll create a muffin from a string stream" << endl;
  istringstream instream("My_Muffin 2 true");
  Muffin m2 = createMuffin(instream);
  m2.output();

  Muffin muff;
  muff.input();

  return 0;
}

Muffin createMuffin(istringstream& inStream)
{
  Muffin muffin;
  // Assume data is properly formatted:
  // Description size chips

  string description;
  int size;
  bool hasChips;

  // Read all three values. Note that chips is represented
  // by the strings "true" and "false"
  inStream >> description >> size >> boolalpha >> hasChips;
  muffin.setSize(size);
  muffin.setDescription(description);
  muffin.setHasChocolateChips(hasChips);

  return muffin;
}
Here are the contents of the text file.
Code:
Giant Blueberry Muffin^42^false
My_Muffin^2^true