Thread: Problems with formatting text.

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    26

    Problems with formatting text.

    I am making a rather large windows program written in the basic Win32 API with C/C++. I want to have a short paragraph explaing the program and some basic legal stuff on the opening window. In order to do this I went to my trusty old bookcase, pulled out "Programming Windows" by Charles Petzold. He has a program in chapter 17 called "Justify" that I could modify to my own needs. I read the code and his explanation and everything seemed to make sense. I tried to implement(direct copy and paste out of the included e-book) and got 71 errors. I fixed alot of of them by re-typing the ' and the -> symbols for some reason. I am now getting 16 errors which make no sense at all. First a copy of my code
    Code:
    #include "StdAfx.h"
    
    void Justify (HDC hdc, PTSTR pText, RECT *prc, int iAlign)
    {
    	int xStart, yStart, cSpaceChars ;
    	PTSTR pBegin, pEnd ;
    	SIZE size ;
    	
    	yStart = prc->top ;
    	do // for each text line
    	{
    		cSpaceChars = 0 ; // initialize number of spaces in line
    		while (*pText == '\0') // skip over leading spaces
    			pText++ ;
    		pBegin = pText ; // set pointer to char at beginning of line
    		do // until the line is known
    		{
    			pEnd = pText ; // set pointer to char at end of line
    			// skip to next space
    			while (*pText != '\0' && *pText++ != '\0') ;
    				if (*pText == '\0')
    					break ;
    			// after each space encountered, calculate extents
    			cSpaceChars++ ;
    			GetTextExtentPoint32(hdc, pBegin, pText - pBegin - 1, &size) ;
    		}
    		while (size.cx < (prc->right − prc->left)){
    		cSpaceChars−− ; // discount last space at end of line
    		}
    		while (*(pEnd − 1) == ' ') // eliminate trailing spaces
    		{
    			pEnd−− ;
    			cSpaceChars−− ;
    		}
    		// if end of text and no space characters, set pEnd to end
    		if (*pText == '\0' || cSpaceChars <= 0){
    			pEnd = pText ;
    		}
    		GetTextExtentPoint32 (hdc, pBegin, pEnd − pBegin, &size) ;
    		switch (iAlign) // use alignment for xStart
    		{
    		case IDM_ALIGN_CENTER:
    			xStart = (prc->right + prc->left − size.cx) / 2 ;
    			break ;
    		}
    		// display the text
    		TextOut (hdc, xStart, yStart, pBegin, pEnd - pBegin) ;
    		// prepare for next line
    		SetTextJustification (hdc, 0, 0) ;
    		yStart += size.cy ;
    		pText = pEnd ;
    	}
    	while (*pText && yStart < prc->bottom - size.cy) ;
    }
    Here is a list of errors I am getting
    Code:
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(27) : error C2146: syntax error : missing ')' before identifier '−'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(27) : error C2059: syntax error : ')'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(27) : error C2143: syntax error : missing ';' before '{'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(28) : error C2065: 'cSpaceChars−−' : undeclared identifier
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(30) : error C2146: syntax error : missing ')' before identifier '−'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(30) : error C2059: syntax error : ')'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(31) : error C2143: syntax error : missing ';' before '{'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(32) : error C2065: 'pEnd−−' : undeclared identifier
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(39) : error C2146: syntax error : missing ')' before identifier '−'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(39) : error C2660: 'GetTextExtentPoint32W' : function does not take 3 arguments
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(39) : error C2059: syntax error : ')'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(43) : error C2146: syntax error : missing ')' before identifier '−'
    c:\documents and settings\administrator\my documents\my projects\dosage\justify.cpp(43) : error C2059: syntax error : ')'
    All of these errors seem to be caused by the use of - I have tried to re-type them and nothing(side question why does re-typing stuff matter?). Can anyone help? I am using Visual Studio 2005 Professional.

    Thank you very much.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > while (size.cx < (prc->right − prc->left))
    Well this while looks like it's trying to belong to the 'do' about 10 lines before it, AND the 'cSpaceChars−−' which follows it.

    A couple of blank lines between control structures (like indentation) sure helps to figure out what should belong to what.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  3. problems reading in from text
    By requiem in forum C++ Programming
    Replies: 5
    Last Post: 04-25-2003, 12:13 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM
  5. problems reading from a text file
    By korbitz in forum C Programming
    Replies: 4
    Last Post: 12-21-2001, 06:11 PM