Undeclared identifier issues

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 3189 Views - Last Post: 28 May 2011 - 08:03 PM Rate Topic: -----

#1 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Undeclared identifier issues

Posted 24 May 2011 - 12:29 PM

So I have an assignment to do this...

1. Find the center, (xAvg, yAvg), of the bounding circle, copy and paste the above code that does precisely this.

2. Find the radius, rMax, of the bounding circle. This will be the maximum of all the distances from
(xAvg, yAvg) to (xPts[i], yPts[i]). You will have to make your own code to do this. You can do a for loop similar
in format to up above.

3. Call the Circle() function to draw the center and circle, in purple. Just copy and paste the two Circle commands up above
that do this.

The only error I am getting is fr undeclared identifiers, but I do not see why. My brain just may be too fried to see it at the moment. Any help would be deeply appreciated. Thanks, here is my code.

 /*
circle.cpp

Draws a number of random points as very small circles
*/

#include <stdio.h>
#include <windows.h>

#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;

HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}


void Circle(int xCenter, int yCenter, int r, unsigned int color) {
    double d;
    int rSquared;
    int x, y;

    rSquared = r * r;

    x = 0;
    y = r;

    d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));

    while (y >= x) {
        PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
        PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
        PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
        PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
        PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
        PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
        PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
        PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
        if (d > 0) {                   //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
        }
        else {
            y--;
        }
        x++;
        d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
    }
}


void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int numPoints;                      // user input
    int *xPts, *yPts;                   // ptrs for dynamic array of x,y coordinates
    int i;                              // loop and point variables
    RECT rect;                          // rectangle for the output window
    int xMin, wdRect, yMin, htRect;     // min rectangle coords and rect width & height

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    htRect = rect.bottom - rect.top;
    yMin = -htRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
    numPoints = atoi(str);

    // allocate and initialize the arrays with random point values
    xPts = new int[numPoints];
    yPts = new int[numPoints];
    for (i = 0; i < numPoints; i++) {
        // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so 
        // bounding circle won't get too large
        xPts[i] = wdRect/4 - rand() % (wdRect/2);
        yPts[i] = htRect/4 - rand() % (htRect/2);
        Circle(xPts[i], yPts[i], 1, green);
    }
	
	// Find the avg of all the points.
	//This will be the bounding circle center.

	xSum = 0;
	ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
	xSum += xPts[i];
	ySum += yPts[i];
	}
	xAvg = xSum / numPoints;
	yAvg = ySum / numPoints;

	// loop here
	for (i = 0; i < numPoints; i++) {
	        rMax = 0;
	        int x = (xAvg -  xPts[i]);
			int y = (yAvg - yPts[i]);
			temp = (x * x) + (y * y);
	        tempDist = sqrt(temp);
 
	    if (rMax < tempDist){
	        rMax = tempDist;
	    }
	    }

	Circle(xAvg, yAvg, 1, purple);  // to draw the center

	Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle

	// draw the points 
	PGraphics->Draw();

    // draw the points
    PGraphics->Draw();

    // free up the allocated memory for the points
    delete[] xPts;
    delete[] yPts;
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Undeclared identifier issues

#2 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 12:34 PM

You have posted a non-compilable code, so what now, am I (are we) supposed to read the code line-by-line and try to see where the problem is? You gotta give more details ... like the line where the error occurs .. and generally if you get any errors post them exactly as they are thrown by the Compiler.

This post has been edited by sarmanu: 24 May 2011 - 12:41 PM

Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Undeclared identifier issues

Posted 24 May 2011 - 12:36 PM

You need to copy and paste the compiler errors EXACTLY as they appear.
Was This Post Helpful? 0
  • +
  • -

#4 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 12:47 PM

Sorry about that, I do not know why it did not occur to me to post the errors for you to see. Here they are...

1>------ Build started: Project: circle, Configuration: Debug Win32 ------
1>Compiling...
1>cl : Command line warning D9035 : option 'Wp64' has been deprecated and will be removed in a future release
1>circle.cpp
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(107) : error C2065: 'xSum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(108) : error C2065: 'ySum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(111) : error C2065: 'xSum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(112) : error C2065: 'ySum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(114) : error C2065: 'xAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(114) : error C2065: 'xSum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(115) : error C2065: 'yAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(115) : error C2065: 'ySum' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(119) : error C2065: 'rMax' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(120) : error C2065: 'xAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(121) : error C2065: 'yAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(122) : error C2065: 'temp' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(123) : error C2065: 'tempDist' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(123) : error C2065: 'temp' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(123) : error C3861: 'sqrt': identifier not found
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(125) : error C2065: 'rMax' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(125) : error C2065: 'tempDist' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(126) : error C2065: 'rMax' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(126) : error C2065: 'tempDist' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(130) : error C2065: 'xAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(130) : error C2065: 'yAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(132) : error C2065: 'xAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(132) : error C2065: 'yAvg' : undeclared identifier
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(132) : error C2065: 'rMax' : undeclared identifier
1>Build log was saved at "file://c:\Users\Stephanie\Desktop\GSP220\iLab 3\circle\Debug\BuildLog.htm"
1>circle - 24 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 12:51 PM

No idea what to say ... wrong copy-paste from here?

This post has been edited by sarmanu: 24 May 2011 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,210
  • Joined: 23-August 08

Re: Undeclared identifier issues

Posted 24 May 2011 - 12:53 PM

This is one of those assignments where the instructor provides a skeleton of code and the student is intended to fill it in to the specifications provided. DeVry, maybe? See it all the time here.

"undeclared identifier" is one of the SIMPLEST error messages to fix/diagnose. It's right there in English. An identifier is a variable name. Before you use a variable, you must declare its type along with its name. You've not done this for the variables mentioned in the errors.
Was This Post Helpful? 0
  • +
  • -

#7 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 01:04 PM

That is basically what I was figuring, I am feeling a bit silly right now, my head just does not want to do this today. I fixed those, and now I have only one error showing.

1>circle.cpp
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(129) : error C3861: 'sqrt': identifier not found
1>Build log was saved at "file://c:\Users\Stephanie\Desktop\GSP220\iLab 3\circle\Debug\BuildLog.htm"
1>circle - 1 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
 /*
circle.cpp

Draws a number of random points as very small circles
*/

#include <stdio.h>
#include <windows.h>

#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
int xSum;
int ySum;
int xAvg;
int yAvg;
int rMax;
int temp;
int tempDist;
HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}


void Circle(int xCenter, int yCenter, int r, unsigned int color) {
    double d;
    int rSquared;
    int x, y;

    rSquared = r * r;

    x = 0;
    y = r;

    d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));

    while (y >= x) {
        PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
        PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
        PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
        PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
        PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
        PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
        PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
        PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
        if (d > 0) {                   //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
        }
        else {
            y--;
        }
        x++;
        d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
    }
}


void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int numPoints;                      // user input
    int *xPts, *yPts;                   // ptrs for dynamic array of x,y coordinates
    int i;                              // loop and point variables
    RECT rect;                          // rectangle for the output window
    int xMin, wdRect, yMin, htRect;     // min rectangle coords and rect width & height

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    htRect = rect.bottom - rect.top;
    yMin = -htRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
    numPoints = atoi(str);

    // allocate and initialize the arrays with random point values
    xPts = new int[numPoints];
    yPts = new int[numPoints];
    for (i = 0; i < numPoints; i++) {
        // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so 
        // bounding circle won't get too large
        xPts[i] = wdRect/4 - rand() % (wdRect/2);
        yPts[i] = htRect/4 - rand() % (htRect/2);
        Circle(xPts[i], yPts[i], 1, green);
    }
	
	// Find the avg of all the points.
	//This will be the bounding circle center.

	xSum = 0;
	ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
	xSum += xPts[i];
	ySum += yPts[i];
	}
	xAvg = xSum / numPoints;
	yAvg = ySum / numPoints;

	// loop here
	for (i = 0; i < numPoints; i++) {
	        rMax = 0;
	        int x = (xAvg -  xPts[i]);
			int y = (yAvg - yPts[i]);
			temp = (x * x) + (y * y);
	        tempDist = sqrt(temp);
 
	    if (rMax < tempDist){
	        rMax = tempDist;
	    }
	    }

	Circle(xAvg, yAvg, 1, purple);  // to draw the center

	Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle

	// draw the points 
	PGraphics->Draw();

    // draw the points
    PGraphics->Draw();

    // free up the allocated memory for the points
    delete[] xPts;
    delete[] yPts;
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}


View PostJackOfAllTrades, on 24 May 2011 - 01:53 PM, said:

This is one of those assignments where the instructor provides a skeleton of code and the student is intended to fill it in to the specifications provided. DeVry, maybe? See it all the time here.

"undeclared identifier" is one of the SIMPLEST error messages to fix/diagnose. It's right there in English. An identifier is a variable name. Before you use a variable, you must declare its type along with its name. You've not done this for the variables mentioned in the errors.

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 01:40 PM

To use the sqrt() function you must include the file that defines the function, cmath in this case.

Also I would recommend that you always initialize your variables when you define them.

Jim

This post has been edited by jimblumberg: 24 May 2011 - 01:41 PM

Was This Post Helpful? 1
  • +
  • -

#9 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 01:45 PM

Okay, I have added cmath, and now I get a different series of errors. I am sorry to be such a bother.

Errors:
1>c:\users\stephanie\desktop\gsp220\ilab 3\circle\circle.cpp(129) : error C2668: 'sqrt' : ambiguous call to overloaded function
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(581): could be 'long double sqrt(long double)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(533): or 'float sqrt(float)'
1> c:\program files\microsoft visual studio 9.0\vc\include\math.h(128): or 'double sqrt(double)'
1> while trying to match the argument list '(int)'

   /*
circle.cpp

Draws a number of random points as very small circles
*/

#include <stdio.h>
#include <windows.h>
#include <cmath>
#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
int xSum = 0;
int ySum = 0;
int xAvg = 0;
int yAvg = 0;
int rMax = 0;
int temp = 0;
int tempDist = 0;
HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}


void Circle(int xCenter, int yCenter, int r, unsigned int color) {
    double d;
    int rSquared;
    int x, y;

    rSquared = r * r;

    x = 0;
    y = r;

    d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));

    while (y >= x) {
        PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
        PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
        PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
        PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
        PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
        PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
        PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
        PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
        if (d > 0) {                   //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
        }
        else {
            y--;
        }
        x++;
        d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
    }
}


void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int numPoints;                      // user input
    int *xPts, *yPts;                   // ptrs for dynamic array of x,y coordinates
    int i;                              // loop and point variables
    RECT rect;                          // rectangle for the output window
    int xMin, wdRect, yMin, htRect;     // min rectangle coords and rect width & height

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    htRect = rect.bottom - rect.top;
    yMin = -htRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
    numPoints = atoi(str);

    // allocate and initialize the arrays with random point values
    xPts = new int[numPoints];
    yPts = new int[numPoints];
    for (i = 0; i < numPoints; i++) {
        // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so 
        // bounding circle won't get too large
        xPts[i] = wdRect/4 - rand() % (wdRect/2);
        yPts[i] = htRect/4 - rand() % (htRect/2);
        Circle(xPts[i], yPts[i], 1, green);
    }
	
	// Find the avg of all the points.
	//This will be the bounding circle center.

	xSum = 0;
	ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
	xSum += xPts[i];
	ySum += yPts[i];
	}
	xAvg = xSum / numPoints;
	yAvg = ySum / numPoints;

	// loop here
	for (i = 0; i < numPoints; i++) {
	        rMax = 0;
	        int x = (xAvg -  xPts[i]);
			int y = (yAvg - yPts[i]);
			temp = (x * x) + (y * y);
	        tempDist = sqrt(temp);
 
	    if (rMax < tempDist){
	        rMax = tempDist;
	    }
	    }

	Circle(xAvg, yAvg, 1, purple);  // to draw the center

	Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle

	// draw the points 
	PGraphics->Draw();

    // draw the points
    PGraphics->Draw();

    // free up the allocated memory for the points
    delete[] xPts;
    delete[] yPts;
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}

 


View Postjimblumberg, on 24 May 2011 - 02:40 PM, said:

To use the sqrt() function you must include the file that defines the function, cmath in this case.

Also I would recommend that you always initialize your variables when you define them.

Jim

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 01:58 PM

What type of variable is temp? Did you Google sqrt() to see what type of arguments sqrt() uses?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 24 May 2011 - 02:09 PM

Ahh, that helps a fair bit, it now compiles and runs. I changed them to:
double temp = 0;
double tempDist = 0;

The issue is now, the program runs, but it does not run properly. The program is supposed to draw a bounding circle around all the points. It does for some, others it does not. Any idea what I am doing wrong here?

  /*
circle.cpp

Draws a number of random points as very small circles
*/

#include <stdio.h>
#include <windows.h>
#include <cmath>
#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
int xSum = 0;
int ySum = 0;
int xAvg = 0;
int yAvg = 0;
int rMax = 0;
double temp = 0;
double tempDist = 0;
HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}


void Circle(int xCenter, int yCenter, int r, unsigned int color) {
    double d;
    int rSquared;
    int x, y;

    rSquared = r * r;

    x = 0;
    y = r;

    d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));

    while (y >= x) {
        PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
        PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
        PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
        PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
        PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
        PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
        PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
        PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
        if (d > 0) {                   //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
        }
        else {
            y--;
        }
        x++;
        d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
    }
}


void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int numPoints;                      // user input
    int *xPts, *yPts;                   // ptrs for dynamic array of x,y coordinates
    int i;                              // loop and point variables
    RECT rect;                          // rectangle for the output window
    int xMin, wdRect, yMin, htRect;     // min rectangle coords and rect width & height

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    htRect = rect.bottom - rect.top;
    yMin = -htRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
    numPoints = atoi(str);

    // allocate and initialize the arrays with random point values
    xPts = new int[numPoints];
    yPts = new int[numPoints];
    for (i = 0; i < numPoints; i++) {
        // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so 
        // bounding circle won't get too large
        xPts[i] = wdRect/4 - rand() % (wdRect/2);
        yPts[i] = htRect/4 - rand() % (htRect/2);
        Circle(xPts[i], yPts[i], 1, green);
    }
	
	// Find the avg of all the points.
	//This will be the bounding circle center.

	xSum = 0;
	ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
	xSum += xPts[i];
	ySum += yPts[i];
	}
	xAvg = xSum / numPoints;
	yAvg = ySum / numPoints;

	// loop here
	for (i = 0; i < numPoints; i++) {
	        rMax = 0;
	        int x = (xAvg -  xPts[i]);
			int y = (yAvg - yPts[i]);
			temp = (x * x) + (y * y);
	        tempDist = sqrt(temp);
 
	    if (rMax < tempDist){
	        rMax = tempDist;
	    }
	    }

	Circle(xAvg, yAvg, 1, purple);  // to draw the center

	Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle

	// draw the points 
	PGraphics->Draw();

    // draw the points
    PGraphics->Draw();

    // free up the allocated memory for the points
    delete[] xPts;
    delete[] yPts;
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}




View Postjimblumberg, on 24 May 2011 - 02:58 PM, said:

What type of variable is temp? Did you Google sqrt() to see what type of arguments sqrt() uses?

Jim

Was This Post Helpful? 0
  • +
  • -

#12 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 25 May 2011 - 02:16 PM

Does anyone have any idea what may be causing this issue with my program?

Thank you for any help you can give.
Was This Post Helpful? 0
  • +
  • -

#13 b0ng01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 169
  • Joined: 16-July 10

Re: Undeclared identifier issues

Posted 25 May 2011 - 05:12 PM

oroberi when you make a post that asks for help it is often very useful to break your code down into the smallest segment possible that reproduces your problem. This is a test case, when you do that it makes it much easier for other people to figure out what is wrong with your code.

All of that was to say, short of someone taking the time to read your code line by line and mentally debug it. We would need the code with all of your included header files to effectively diagnose every issue with your code.
Was This Post Helpful? 0
  • +
  • -

#14 oroberi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 38
  • Joined: 18-October 09

Re: Undeclared identifier issues

Posted 28 May 2011 - 07:16 PM

B0ng01 I understand that, the issue with that is, that I really have no idea why the program is not drawing the circle around all the points all of the time.

The program is supposed to draw a bounding circle around all the points. It does for some, others, such as 4 random points, it does not. Any idea what I am doing wrong here?

   /*
circle.cpp

Draws a number of random points as very small circles
*/

#include <stdio.h>
#include <windows.h>
#include <cmath>
#include "resource.h"

// your path for this include may vary
#include "GraphicsFramework.h"

// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
int xSum = 0;
int ySum = 0;
int xAvg = 0;
int yAvg = 0;
int rMax = 0;
double temp = 0;
double tempDist = 0;
HWND HOutput = 0;  // handle to the output control
HWND HDialog = 0;

// function to get the absolute value of an integer
int Abs(int x) {
    if (x < 0)  return -x;
    else        return x;
}

// function to get the sign (+1 or -1) of an integer
int Sign(int x) {
    if (x < 0)  return -1;
    else        return 1;
}


void Circle(int xCenter, int yCenter, int r, unsigned int color) {
    double d;
    int rSquared;
    int x, y;

    rSquared = r * r;

    x = 0;
    y = r;

    d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));

    while (y >= x) {
        PGraphics->AddPoint(xCenter + x, yCenter + y, color); //draws circular arc for approx. 45-90 degrees
        PGraphics->AddPoint(xCenter - x, yCenter + y, color); //draws arc for approx. 90-135 degrees
        PGraphics->AddPoint(xCenter + x, yCenter - y, color); //draws arc for approx. 270-315 degrees
        PGraphics->AddPoint(xCenter - x, yCenter - y, color); //draws arc for approx. 225-270 degrees
        PGraphics->AddPoint(xCenter + y, yCenter + x, color); //draws arc for approx. 0-45 degrees
        PGraphics->AddPoint(xCenter - y, yCenter + x, color); //draws arc for approx. 135-180 degrees
        PGraphics->AddPoint(xCenter + y, yCenter - x, color); //draws arc for approx. 315-360 degrees
        PGraphics->AddPoint(xCenter - y, yCenter - x, color); //draws arc for approx. 180-225 degrees
        if (d > 0) {                   //if (x,y)lies inside the circle of radius r centered at (-1,0.5)
        }
        else {
            y--;
        }
        x++;
        d = rSquared - ((x + 1) * (x + 1) + (y - 0.5) * (y - 0.5));
    }
}


void DrawStuff() {
    COLORREF green = RGB(0, 255, 0);     // green color to draw with
    COLORREF purple = RGB(255, 0, 255);  // purple color to draw with

    char str[32];                       // string to store user input
    int numPoints;                      // user input
    int *xPts, *yPts;                   // ptrs for dynamic array of x,y coordinates
    int i;                              // loop and point variables
    RECT rect;                          // rectangle for the output window
    int xMin, wdRect, yMin, htRect;     // min rectangle coords and rect width & height

    // clear the scene and add an axis
    PGraphics->ClearScene(RGB(0, 0, 0));
    PGraphics->AddAxis(RGB(150, 150, 150), 10);

    // get the rectangle info for this window
    GetClientRect(HOutput, &rect);
    wdRect = rect.right - rect.left;
    xMin = -wdRect / 2;
    htRect = rect.bottom - rect.top;
    yMin = -htRect / 2;

    // get the user input from the edit boxes and 
    // convert string input to integer
    GetDlgItemText(HDialog, IDC_EDIT_NUMPOINTS, str, 32);
    numPoints = atoi(str);

    // allocate and initialize the arrays with random point values
    xPts = new int[numPoints];
    yPts = new int[numPoints];
    for (i = 0; i < numPoints; i++) {
        // keep points within range -wd/4..+wd/4, -ht/4..+ht/4 so 
        // bounding circle won't get too large
        xPts[i] = wdRect/4 - rand() % (wdRect/2);
        yPts[i] = htRect/4 - rand() % (htRect/2);
        Circle(xPts[i], yPts[i], 1, green);
    }
	
	// Find the avg of all the points.
	//This will be the bounding circle center.

	xSum = 0;
	ySum = 0;
	for (i = 0; i < numPoints; i++)
	{
	xSum += xPts[i];
	ySum += yPts[i];
	}
	xAvg = xSum / numPoints;
	yAvg = ySum / numPoints;

	// loop here
	for (i = 0; i < numPoints; i++) {
	        rMax = 0;
	        int x = (xAvg -  xPts[i]);
			int y = (yAvg - yPts[i]);
			temp = (x * x) + (y * y);
	        tempDist = sqrt(temp);
 
	    if (rMax < tempDist){
	        rMax = tempDist;
	    }
	    }

	Circle(xAvg, yAvg, 1, purple);  // to draw the center

	Circle(xAvg, yAvg, rMax, purple); // to draw the bounding circle

	// draw the points 
	PGraphics->Draw();

    // draw the points
    PGraphics->Draw();

    // free up the allocated memory for the points
    delete[] xPts;
    delete[] yPts;
}

/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd, 
    UINT message, 
    WPARAM wParam, 
    LPARAM lParam)
{
    switch(message)
    {
    case WM_INITDIALOG:
        // dialog is initializing - store the picture box handle in a global variable for later
        HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);        

        // instantiate and initialize our graphics framework object
        PGraphics = new GraphicsFramework(HOutput);

        break;

    case WM_COMMAND:
        switch(LOWORD(wParam))
        {
            case IDC_BTN_DRAW:
                // draw button was pressed
                DrawStuff();
                break;
            case IDC_BTN_CLEAR:
                // clear button was pressed so clear the scene and draw the empty scene
                PGraphics->ClearScene(RGB(0, 0, 0));
                PGraphics->Draw();
                break;
            case IDCANCEL:
                // user is quitting so release the GraphicsFramework object and quit
                delete PGraphics;
                PostQuitMessage(0);
                break;
        }
                  
    }
    return FALSE;
}

// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
    // create the main window
    // store its handle in a global if needed
    HDialog = CreateDialog (GetModuleHandle(NULL), 
        MAKEINTRESOURCE(IDD_DIALOG1), 
        0, 
        DialogProc);

    // make the dialog visible
    ShowWindow(HDialog, SW_SHOW);

    // standard windows message loop
    MSG  msg;
    int status;
    while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
    {
        if (status == -1)
            return -1;
        // avoid processing messages for the dialog
        if (!IsDialogMessage (HDialog, & msg))
        {
            TranslateMessage ( & msg );
            DispatchMessage ( & msg );
        }
    }

    return (int)(msg.wParam);
}




View Postb0ng01, on 25 May 2011 - 06:12 PM, said:

oroberi when you make a post that asks for help it is often very useful to break your code down into the smallest segment possible that reproduces your problem. This is a test case, when you do that it makes it much easier for other people to figure out what is wrong with your code.

All of that was to say, short of someone taking the time to read your code line by line and mentally debug it. We would need the code with all of your included header files to effectively diagnose every issue with your code.

Was This Post Helpful? 0
  • +
  • -

#15 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Undeclared identifier issues

Posted 28 May 2011 - 07:25 PM

Fire up your debugger and trace through your code. Before turning to us for help, there are a number of investigations you should be doing. Have you used a debugger? Have you inserted output statements and observed what your program is doing?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2