First of all, this is not schoolwork but rather me trying to read about c++ and through program examples and see if I can write my own. So help is much appreciated if you could as I am a new learner.

This program that I am writing is supposed to look through words in a paragraph and if it fits the equation it adds it to the string otherwise the program resets.

I have written a pseudo-code for this program. Now I am trying to implement what I wrote into c++ code form. I am new to programming and forgot how to implement what I wrote in the two while loops to c++ form: // Fill words into string; // read first character; and 2nd while loop: // Add the character to the string of the word; // Read next character; This is only one of the .cpp files not including main or header files. If you would like to see them to understand more of the program I can attach them as well.

#include "typeset.h"
#include <string>
#include <sstream>

using namespace std;

void typeset (int maxWidth, istream& documentIn)
{
  string line;
  string word;
  string w;
  // Collect Input;
  // Fill maxWidth into W;
  // Read Input;

     while (!documentIn.eof())
     {
       // Fill words into string;
      //   read first character;
	 while(!" ")
	  {
	  //  Add the character to the string of the word;
	  //  Read next character;
	  }
	if('\n')
	{
       //print out line and endl;
       cout << line << endl;
	}
       // Check if line is too long;
	      if (line+word+" " < w)
	      {
              line = line + word+ " ";
	      }
       // Output;
          else
          {
          //    Print line an endl;
          cout << line << endl << endl;
          //    Reset line to empty string;
          line = "";
          }
     }
}
]

Recommended Answers

All 5 Replies

Read words from a paragraph, compare them with some equation(what type of equation?) and then add it to a string if it matches?

Did I get you right?

Read words from a paragraph, compare them with some equation(what type of equation?) and then add it to a string if it matches?

Did I get you right?

Yes, you got it right.

Here is the problem stated directly from my c++ book for better understanding:

A basic operation in typesetting documents is line filling, the practice of putting as many words into a line as will actually fit without overflowing a pre-determined maximum width. This can be challanging when dealing with proportional fonts where each character is a different width, and even more so if we are expected to know how to hyphen-
ate words to make them fit better.

In this case, however, we will deal with the basic problem: design a function that, given an open input stream and a value W, reads a plain-text document from that stream and typesets each paragraph so each line begins with a non-space character, each pair of successive owrds in a line are separated by a single blank, none of the lines contain more than W characters, and all the lines except the last on in the paragraph are so nearly full that adding another blank plus the first word of the next line would have taken use over the W character max.

Paragraphs in the input and output will be separated by empty lines, and, for the purpose of this assignment, a "word" is any sequence of characters not contianing a blank or a line terminator.

For example, given a W==20 and an input stream containing the text:

'Mine is a long and a sad tale!' said the Mouse, turning to Alice,
and sighing.

'It IS a long tail, certainly,' said Alice, looking down with
wonder at the Mouse's tail; 'but why do you call it sad?'
the output would be
'Mine is a long and
a sad tale!' said
the Mouse, turning
to Alice, and
sighing.

'It IS a long tail,
certainly,' said
Alice, looking down
with wonder at the
Mouse's tail; 'but
why do you call it
sad?'


Given the same input stream, with W==30, the output would be

'Mine is a long and a sad
tale!' said the Mouse, turning
to Alice, and sighing.

'It IS a long tail,
certainly,' said Alice,
looking down with wonder at
the Mouse's tail; 'but why do
you call it sad?'

The Approach

To typeset a document, you must typeset each paragraph in the input, while printing a line between each paragraph. To typeset a paragraph you must fill the words from each line of the paragraph, then print the final line of the paragraph.

Filling the words from each line involves reading the words from a line of input and filling each one into an output line buffer. To fill a word into an output line buffer, you must first determine if that word and (if appropriate) a separating space force the line buffer over a width of W, and if so, flush the line buffer to output. Only then can you append the space (when appropriate) and word to the line buffer.

I'm sorry, but I don't understand what you're actually asking here. If you're learning from a book then try to go back a few pages, as the information needed to complete your lesson is probably there (:

If you're just wanting the code for this problem then trust me, you're not going to learn... try taking another look at your lessons first.

I have read all pages dealing with this example at least 3 times, but am still not getting how to implement my pseudo code into c++ code.

Here is what else I have done to my .cpp file.

#include "typeset.h"
#include <string>
#include <sstream>

using namespace std;

void typeset (int maxWidth, istream& documentIn)
{
  int w;
  int line;
  string word;
 
  // Collect Input;
   cout << "Enter a size witdth for w: ";
   cin w;

  // Fill maxWidth into W;
   int maxWidth [100];

  // Read Input;
    // If you entered a width size bigger than what the array can hold.
    if(w > maxWidth)
    {
      cout < "\nThe width is too big for this program!" << endl;
    }

    // Or Continue with the program
    else
    {
     while (!documentIn.eof())
     {
       // Fill words into string;
       // Can't figure out this code
       
       //   read first character;
       // Can't figure out this code

	 while(!" ")
	  {
	  //  Add the character to the string of the word;
           // Can't figure out this code

	  //  Read next character;
           // Can't figure out this code           
	  }
	if('\n')
	{
       //print out line and endl;
       cout << line << endl;
	}
       // Check if line is too long;
	      if (line+word+" " < w)
	      {
              line = line + word+ " ";
	      }
       // Output;
          else
          {
          //    Print line an endl;
          cout << line << endl << endl;
          //    Reset line to empty string;
          line = "";
          }
     }
}

Alright so I got the program to compile and work that you have gave me but I want to know how you can separate the paragraphs into 2 paragraphs?

Input EX:

'Mine is a long and a sad tale!' said the Mouse, turning to Alice,
and sighing.

'It IS a long tail, certainly,' said Alice, looking down with
wonder at the Mouse's tail; 'but why do you call it sad?'


Output EX:

'Mine is a long and
a sad tale!' said
the Mouse, turning
to Alice, and
sighing. 'It IS a
long tail,
certainly,' said
Alice, looking down
with wonder at the
Mouse's tail; 'but
why do you call it
sad?' 

#include <string>
#include <sstream>
#include <iostream>

using namespace std;

string getWord(istream& documentIn)
{
   string result;

   int ch;
   while (!documentIn.eof())
   {
       ch = documentIn.get();

       if (isspace(ch))
       {
         if (result.length() > 0) break;
       }
       else
       {
         result += (char)ch;
       }
   }

   return result;
}

void typeset (int maxWidth, istream& documentIn)
{
   string line;
   string word;

   while (!documentIn.eof())
   {
       string word = getWord(documentIn);

       if (word.length() > maxWidth)
       {
         cerr << "Error word " << word
         << " is longer than the max of " << maxWidth
         << " characters\n";
         return;
       }

       if (line.length() + word.length() + 1 <= maxWidth)
       {
         if (line.length() > 0) line.append(" ");
        line.append(word);
       }
       else
       {
         cout << line << endl;

         line = word;
       }
   }

   if (line.length() != 0)
   {
       cout << line << endl;
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.