I am experimenting with a piece of code I found on the internet (I'm still relatively new to c++), but my visual c++ express edition and visual c++ 2005 ide's are not compiling it giving some errors I can't understand fully.

this is the code

/****************************************************
 * File: BasicCalculator.h
 * A calculator program built using MFC dialog box
 ****************************************************/

#include "stdafx.h"
#include <afxwin.h>
#include <strstream>
#include <iomanip>
#include <cmath>
#include "BasicCalculator_ids.h"
#include "BasicCalculator.h"
#include <fstream>    
#include <String.h>
#include <cstdlib>
#include <stdio.h>

CCalculatorDialog::CCalculatorDialog() 
    : CDialog( "Calculator" )
{
   clear();
}

// clear result and entry
void CCalculatorDialog::clear()
{
   m_fResult = 0.0;
   m_opCode = NONE;

   clearEntry();
}

// clear entry (number being input)
void CCalculatorDialog::clearEntry()
{
   m_szEntry[ 0 ] = '\0';
   m_nEntryLen = 0;
   m_bHavePoint = false;
}

// user enters a character
void CCalculatorDialog::enter( char ch )
{
   m_szEntry[ m_nEntryLen++ ] = ch;
   m_szEntry[ m_nEntryLen ] = '\0';
   showEntry();
}

// process any digit button the user enters
afx_msg void CCalculatorDialog::HandleDigitPressed( UINT digit )
{
   if ( m_nEntryLen < MAX_DIGITS )
   {
      // convert message id to character
      enter( digit - IDC_ZERO + '0' );
   }
}

// the user has entered a decimal point
afx_msg void CCalculatorDialog::HandleDecPointPressed()      
{
   if ( !m_bHavePoint && m_nEntryLen < MAX_DIGITS )
   {
      m_bHavePoint = true; // allow only one point per number
      enter( '.' );
   }
}

// the user has pressed the clear all data button
afx_msg void CCalculatorDialog::HandleClearPressed()      
{
   clear();
   showEntry();
}

// the user has pressed the clear just the data being entered button
afx_msg void CCalculatorDialog::HandleClearEntryPressed() 
{
   clearEntry();
   showEntry();
   m_opCode = EQ;
   doOp();
}

// the user has pressed the Multiplication  button
afx_msg void CCalculatorDialog::HandleMultPressed()       
{
   doOp();
   m_opCode = MPY;
}

// the user has pressed the Division  button
afx_msg void CCalculatorDialog::HandleDivPressed()        
{
   doOp();
   m_opCode = DIV;
}

// the user has pressed the Minus  button
afx_msg void CCalculatorDialog::HandleMinusPressed()      
{
   doOp() ;
   m_opCode = SUB ;
}

// the user has pressed the Addition button
afx_msg void CCalculatorDialog::HandlePlusPressed()       
{
   doOp() ;
   m_opCode = ADD ;
}

// the user has pressed the +/- button
afx_msg void CCalculatorDialog::HandlePMPressed()         
{   
   m_fResult = -1.0 * m_fResult ;
   showResult();
   m_opCode = EQ;
}

// the user has pressed the Equal button
afx_msg void CCalculatorDialog::HandleEqualPressed()         
{
   doOp();
   m_opCode = EQ;
}

// the user has pressed the Exit button
afx_msg void CCalculatorDialog::HandleExitPressed() {
   if( AfxMessageBox( "Do you really want to exit?", MB_YESNO ) == IDYES )
	   OnOK() ;
}

afx_msg void CCalculatorDialog::HandleMyHelpPressed() {
   WinExec( "c:\\Program Files\\Internet Explorer\\IExplore.exe  http://127.0.0.1/myCalcHelp/help.aspx" , SW_SHOW )   ;
} 

afx_msg void CCalculatorDialog::HandleMyAboutPressed()  {
   WinExec( "c:\\Program Files\\Internet Explorer\\IExplore.exe  http://127.0.0.1/myCalcHelp/index.aspx" , SW_SHOW )   ;
}

// do the previous infix operator, if any
   void CCalculatorDialog::doOp() {
      fFirstNumber = atof( m_szEntry );
      fTemp = m_fResult ;

      switch ( m_opCode )   {
         case MPY:
            m_fResult *= fFirstNumber;
         break;

         case DIV: 
	    if( fFirstNumber == 0.0 )
	       AfxMessageBox( "You cannot divide by zero.", MB_OK )  ;
	    else {
               m_fResult /= fFirstNumber ;
            }
         break;

         case SUB:
            m_fResult -= fFirstNumber;
         break;

         case ADD:
            m_fResult += fFirstNumber;
         break;

         case EQ:
         case NONE:
            if ( m_nEntryLen > 0 )  // if user entered a new number,
               m_fResult = fFirstNumber;  // make it the left operand
      }

      m_opCode = NONE;
      clearEntry();

      showResult();
   }

   void CCalculatorDialog::showResult()  {
      static char m_szString[ MAX_DIGITS + 1 ];	  
      static ostrstream m_oStream( m_szString, MAX_DIGITS );

      m_oStream.seekp( 0 );
      m_oStream << setprecision( MAX_DIGITS );
      m_oStream << m_fResult << ends;
      CEdit *pDisplay = ( CEdit * ) GetDlgItem( IDC_DISPLAY );
      pDisplay->SetWindowText( m_szString );
   }

   void CCalculatorDialog::showEntry()   {
      CEdit *pDisplay = ( CEdit * ) GetDlgItem( IDC_DISPLAY );
      if ( pDisplay )
         pDisplay->SetWindowText( m_szEntry );
   }

   BEGIN_MESSAGE_MAP( CCalculatorDialog, CDialog )
      ON_COMMAND_RANGE( IDC_ZERO, IDC_NINE, HandleDigitPressed )
      ON_COMMAND( IDC_DECPT,   HandleDecPointPressed )
      ON_COMMAND( IDC_CE,      HandleClearEntryPressed )
      ON_COMMAND( IDC_CLR,     HandleClearPressed )
      ON_COMMAND( IDC_MULT,    HandleMultPressed )
      ON_COMMAND( IDC_DIV,     HandleDivPressed )
      ON_COMMAND( IDC_MINUS,   HandleMinusPressed )
      ON_COMMAND( IDC_PLUS,    HandlePlusPressed )
      ON_COMMAND( IDC_PM,      HandlePMPressed )
      ON_COMMAND( IDC_EQUAL,   HandleEqualPressed )
      ON_COMMAND( IDC_EXIT,    HandleExitPressed )
      ON_COMMAND( IDC_MYHELP,  HandleMyHelpPressed )
      ON_COMMAND( IDC_MYABOUT, HandleMyAboutPressed )
   END_MESSAGE_MAP()


   // dialog-based application
   class CCalculatorApp : public CWinApp {
      public:
         BOOL InitInstance()    {
            CCalculatorDialog calculatorDialog;
            calculatorDialog.DoModal();   // run dialog
            return FALSE;                 // finished
         }
   } calculator;

O.K, so when I build this code, I get the following errors:

------ Rebuild All started: Project: BasicCalculator, Configuration: Debug Win32 ------
Deleting intermediate and output files for project 'BasicCalculator', configuration 'Debug|Win32'
Compiling...
BasicCalculator.cpp
c:\ooc++\win32apicalc\basiccalculator.cpp(182) : error C2146: syntax error : missing ';' before identifier 'm_oStream'
c:\ooc++\win32apicalc\basiccalculator.cpp(182) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
c:\ooc++\win32apicalc\basiccalculator.cpp(182) : error C3861: 'm_oStream': identifier not found
c:\ooc++\win32apicalc\basiccalculator.cpp(184) : error C2065: 'm_oStream' : undeclared identifier
c:\ooc++\win32apicalc\basiccalculator.cpp(184) : error C2228: left of '.seekp' must have class/struct/union
        type is ''unknown-type''
c:\ooc++\win32apicalc\basiccalculator.cpp(185) : error C2065: 'm_oStream' : undeclared identifier
c:\ooc++\win32apicalc\basiccalculator.cpp(185) : error C3861: 'setprecision': identifier not found
c:\ooc++\win32apicalc\basiccalculator.cpp(186) : error C2065: 'm_oStream' : undeclared identifier
c:\ooc++\win32apicalc\basiccalculator.cpp(186) : error C2065: 'ends' : undeclared identifier
Build log was saved at "file://c:\OOC++\win32apicalc\Debug\BuildLog.htm""
BasicCalculator - 9 error(s), 0 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

Could anyone help me with this?

PS: I've modified the code a bit already because it had almost 70 errors before!

>>A calculator program built using MFC dialog box
You can not use the express version of those compilers to compile that code because those compilers do not support MFC. You have to buy $$$$ one of the other versions of the compiler.

Thanks for your reply, but I have Visual C++ 2005 (professional) on the computer, but it won't compile it for me anyway, so that's when I tried it on the 2008 express edition( which didn't work). Is there anything radically wrong with the code or is it the program?

line 182 uses stream that is defined in <sstream> header file.

Thanks for your reply, but I have Visual C++ 2005 (professional) on the computer, but it won't compile it for me anyway, so that's when I tried it on the 2008 express edition( which didn't work). Is there anything radically wrong with the code or is it the program?

Are you just missing ...

static std::ostrstream m_oStream( m_szString, MAX_DIGITS );
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.